URL Shortener Microservice Test Fails

I have tested my code for the URL shortener using different URLs (Wrong and Correct URLs) and they all work well. However, on submission of my code link to FCC, it gives an error.

see the test that gives the error below.

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' }
// tests completed

At first, I thought it was case sensitive and the error output had to be exactly { error: 'invalid url' }. I tried setting my JSON error output to all lower case and the FCC error continued.

I made the output { error: 'invalid URL' } and { error: 'Invalid URL' } still no success.

During my URL validation, I had set 2 error messages.

  1. If URL does not follow the http://example.com format, you get { error: 'invalid URL' }
  2. if the URL hostname does not map to a valid address, you get { error: 'invalid Hostname' }

I thought this was the cause of the FCC Test Failure and had set both errors to output { error: 'invalid url' } yet the test still failed.

I will appreciate it if I am advised on a way forward on this issue.

My project link

solution: Glitch :・゚✧

My browser information:

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

Challenge: URL Shortener Microservice

Link to the challenge:

When I run the fCC tests against your project, I get this error

frame-runner.js:98 TypeError: Cannot read property 'toLowerCase' of undefined
    at eval

in the browser console, which means the tests are getting an empty response. That’s probably because you’re sending a status of 400, which the tests interpret as failure. So you would need to remove that despite it making sense for a real app.

I can’t get glitch to fork and run right now, but you should log all your inputs and responses to your isValidUrl function in your URL controller because I believe it has the same problems I discussed here. The fCC tests don’t send a bare URL; they have some URL parameters tacked on as well that dns.lookup() cowardly refuses to handle.

1 Like

I did not even remember that the browser has a console. I guess I have been trying to fix this issue for days now and I am already out of ideas.

I have taken out the status code (400) associated with the isValidUrl in my controller. However, the error is still showing up with pride.

I already set short_url to store itself on the database. If it does not exist (as in the case of a first POST request to shorten a URL) it creates itself and still gets stored on the database.

I am upcoming so, even if I make series of logs, I may end up not being able to decipher which of the information will be useful to fix my problem. I did a few console.log() though, particularly in the isValidUrl function. This feels like where the issue might be coming from. Nothing colourful yet after me Logging

I am still struggling with the issue.

From the browser error that you shared and to my understanding, I can interpret it to mean that there is a missing return value and hence the undefined. I am not sure if I can tell where this is coming from.

Thank you for your response.

Let me know if you find any other corrections that I can make, to get me running.

Like I said, it’s your URL validation that is failing. Add logging to your the validation part of your route:

const isValidUrl = (req, res, next) => {
  console.log(req.body);
  console.log(req.params);
  console.log(req.query);

  try {
    const validUrl = new URL(req.body.url);
    if (validUrl.origin === "null") {
      console.log('null URL');
      console.log({ error: "invalid url" });
      return res.json({ error: "invalid url" });
    } else {
      console.log('try dns.lookup()');
      dns.lookup(validUrl.hostname, (err, addr, family) => {
        if (err || !addr) {
          console.log(`failed dns.lookup() on URL: ${req.body.url}`)
          console.log({ error: "invalid hostname" });
          return res.json({ error: "invalid hostname" });
        }
        console.log(`no dns.lookup() errors, ${req.body.url} must be valid`);
        next();
      });
    }
  } catch (err) {
    console.log(err);
    console.log('caught error');
    console.log({error: "invalid url"});
    return res.json({error: "invalid url"});
  }
};

and you get this output after running the fCC tests:

{ url: 'ftp:/john-doe.org' }
{}
{}
try dns.lookup()
no dns.lookup() errors, ftp:/john-doe.org must be valid

but that URL is not valid.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.