Url shortener (Backend) - can't pass url validation test

I am stuck for hours, I can’t see what is wrong with my code anymore. I can’t pass the URL validation test.

This is the response my app gives if and invalid URL is passed:
{"error":"Invalid URL"}

Your project link(s)
solution: boilerplate-project-urlshortener - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36

Challenge: URL Shortener Microservice

Link to the challenge:

doesn’t it fail because

{ error: 'invalid url' } !== {"error":"Invalid URL"}

meaning that test is checking if your value is deeply equal to { error: 'invalid url' }

I may be wrong but try changing invaild error message to { error: 'invalid url' } and see if it works
oh because I forgot the code I am talking about is this

dns.lookup(urlObject.hostname, async (err, address, family) => {
      // check if url is valid
      if (err) {
        res.status(401).json({
          "error": 'Invalid URL' // <-----
        });
      } else {
        try {
         ...
 }
        } catch (err) {
          ...
        }
      }
    });
  } catch (err) {
    res.status(401).json({
      error: 'Invalid URL'  // <-----
    });
  }
});

good luck

Thank you for answer. I have changed the response to this { error: 'invalid url' } but the test still fails. According to this example they provided, the response looks like this {"error":"Invalid URL"}.

So after I tried a few different response formats, I believe that my code just does not pass the tests. If I was able to access the tests and see what an invalid URL looks like, according to the test, I think I could find the reason why it fails.

You can access the tests. They’re on github and there’s several links in the forums. These are in the main curriculum repo. The tests lowercase the messages, so case is irrelevant. You can always log the inputs to a route yourself and see what the input is with something like

  console.log(`req.body: ${JSON.stringify(req.body)}`);
  console.log(`req.params: ${JSON.stringify(req.params)}`);
  console.log(`req.query: ${JSON.stringify(req.query)}`);

In this case, you just need to open your browser console while running the tests and you’ll see the problem: a 401 error. Your code here (with my extra logging)

    dns.lookup(urlObject.hostname, async (err, address, family) => {
      // check if url is valid
      if (err) {
        console.error(err);
        console.log('invalid URL');
        return res.status(401).json({
          error: 'invalid url'
        });

sends the 401. 4xx errors are server errors and usually cause the clients to think there is a problem with the response, which is what is happening here (4xx causes fetch to fail essentially). You don’t actually need a status for the tests to pass since it is not mentioned in the spec.

2 Likes

Thank you very much! I did not know status could cause trouble to these tests… Thank you for letting me know that the tests are available, as well.