URL Shortener Microservice: doesn't pass

HI.

I’m doing the URL Shortener Microservice challenge.

The verification system tells me that I fail the fourth test, the one that says:

If you pass an invalid URL that doesn’t follow the valid http://www.example.com format, the JSON response will contain { error: 'invalid url' }

The application I wrote locally checks the validity of the URL; in particular the check is carried out with the following function:

const isValidUrl = (url) => {
  try {
    new URL(url);
    return true;
  } catch (err) {
    return false;
  }
};

Investigating the problem on the developer tools of my browser I seem to understand that the URL that should be rejected is https://r.stripe.com/b, which instead my application accepts and records in the database. The following node session demonstrates that the url is formally valid:

user$ node
Welcome to Node.js v18.16.0.
Type ".help" for more information.
> let a = new URL('https://r.stripe.com/b')
undefined
> a
URL {
  href: 'https://r.stripe.com/b',
  origin: 'https://r.stripe.com',
  protocol: 'https:',
  username: '',
  password: '',
  host: 'r.stripe.com',
  hostname: 'r.stripe.com',
  port: '',
  pathname: '/b',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}
  1. Could you tell me if my analysis is correct?
  2. What are the criteria that the application must use to consider a URL as valid or invalid?

Thank you.

Open the browser dev tools and go to the network tab. Now submit and look at the request/response. You can see what is sent as valid/invalid URLs in the payload.

Hint: the invalid URL used will not cause your try/catch to throw.


I would suggest you use a library or the suggested dns method instead of trying to roll your own test.