Open Source

URL: we need some space

For lab 6 of DPS909, we were tasked with fixing a bug in the open source project, Brave and then write tests to confirm the fix. This bug caused file addresses with a space to bring up the result of the user’s default search engine instead of the contents of the address. We’re given the following scenarios:

  1. "dog"
  2. " dog "
  3. "dog cat"
  4. " dog cat "
  5. "https://www.google.ca/search?q=dog"
  6. " https://www.google.ca/search?q=dog "
  7. "https://www.google.ca/search?q=dog cat"
  8. " https://www.google.ca/search?q=dog cat "
  9. "file:///Users/humphd/repos/browser-laptop/dog cat.txt"
  10. " file:///Users/humphd/repos/browser-laptop/dog cat.txt "

The first four cases worked as any other browser, running the given text through the user’s default search engine. Scenarios 5-6 also ran like any other browser, navigating to a google search of “dog”.

working5-6

Scenarios 7-8 on the other hand ran the address though the default search engine.

working7-8Scenario 9-10 had similar results of other web browsers, displaying the contents of the file with chrome being the only one that does not require “file///” in the beginning.

working9-10

It seems the Brave is capable of folding the spaces that are in either end of the address but spaces caught anywhere in the middle will cause the URL to be seen as invalid and sent to the default search engine.

To fix this I have looked to see how other browsers handled these scenarios and I have noticed that they replace and space in the middle of a URL with “%20”. I have added the following to replicate this process.

let str = input.trim().replace(/\s([^\s]+)$/, ‘%20’)

It worked without a hitch.

working7-8

When writing the tests, I only needed to create two tests (one for each of the failed scenarios). And just like that, it was done.

 

 

Leave a comment