URL Shortener Tests Failing

Hi There,

Can someone please review my code and tell me what am I doing wrong with the url shortener microservice project the code is functioning as required when I manually test it in the browser but freecodecamp tests the last two are failing for some reason and I can’t figure out what’s wrong with it.

the code is on repl.it here

Thank you for the help in advance!

The last test is failing because you are not returning the exact error message expected.

@RandellDawson if by not the exact error message you’re referring to upper and lower case of the letters I thought that might be the issue and I actually copy and pasted the way it is but it didn’t work the reason why I had it different was because that’s how it was in the working example link provided thought that would make it pass but it didn’t either way I’ve changed it to res.json({ error: 'invalid url'}) which is how it is listed in the test itself

I think it’s accepting an invalid URL, is it not? The URL used for the test is ftp:/john-doe.org

const parsedUrl = new URL('ftp:/john-doe.org').host;

dns.lookup(parsedUrl, (err, addresses) =>
  console.log(!!addresses)); // true

You can look at the tests as well.

1 Like

Yes, it is doing that too. When that gets fixed, the correct JSON response needs to be corrected too.

1 Like

@lasjorg Thanks! I see that now I figured it was too easy to just check a valid URL using URL module the way I did I guess as far as the module is concerned ftp:/john-doe.org is a valid url and for the purpose of this project I would have to make sure it’s only http & https… I completely missed that thanks for pointing it out.

No problem.

It’s honestly one thing I wouldn’t mind seeing clarified a bit in the requirements. I feel like what constitutes an invalid URL is maybe a bit too vague (or at least it’s a lot easier to know when you see the tests).

In the context of the hint given about using dns.lookup I think the way you did it makes perfect sense. Construct a URL, get the host property, pass it to lookup.

I’m also not sure using john-doe.org for the test is a great idea either seeing as it a registered domain name.


So I added a regex check inside isValidUrl function to make sure urls are only http and https and that seems to do the trick with 4th test but 3rd test of redirecting short_url to original_url is still failing for some reason even though visiting those urls manually is working

When I submit your project and look at the network tab I get a CORS error and the URL for the GET looks like this


If I submit my own project I see something like this for the GET


I didn’t really look at your code to figure out what is happening. If you log out url.longUrl what do you get?

Anyway. It’s late for me so I’m off, maybe someone else will help.

Thanks for the help @lasjorg @RandellDawson I figured it out woohoo!! the way I was saving shortUrl was wrong as I was concatenating the the fullUrl to it on save so when test was running it was adding the full url again to it which is why that get request had the wrong url

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