URL Shortener Microservice - url validation

Hi,

I’m unable to pass the last test on URL Shortener Microservice and I can’t understand why. The requirement is:

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' }

When I test myself by manually providing valid and invalid urls, everything works as expected.
When I run FCC test, I fail. What puzzles me is that judging by the console output, my code works as expected too.
I feel like I’m missing something obvious.

Here is my middleware responsible for url validation. It pretty much just checks for the existence of a protocol. Any protocol:

//link validation regex
const protocol_regex = /^\D{3,5}:\/\//;

//link validation middleware

const validate_url = (req,res,next)=>{
  console.log("------request------------------")
  console.log(req.body.url)
  console.log("------validate------------------")
  console.log("protocol: " + protocol_regex.test(req.body.url));
  console.log("------validate------------------")
  if(
    protocol_regex.test(req.body.url)
  ) {
    console.log("probs a url")
  } else{
    return res.status(400).json({
      "error":'invalid url'
    })
  }
  next();
}

If this is not enough to figure out what I’m doing wrong, here is the source code:

Any feedback is appreciated.
Thanks

1 Like

Hey I was facing the same issue.Manual testing by me gives the expected result correctly but it fails to pass the freecodecamp test cases.I don’t know what’s the issue here.If you have already found the problem do let me know.
Thanks

Hi,

I have figured it out some time ago. It was something ridiculous. As far as I remember, I kept on failing one of the tests because I was returning
res.status(200),json()`

I passed the tests when I removed status from the return statement, even though it’s against best practices.

However, I just can’t 100% confirm that was the reason I was failing the url validation test.

Could you share your replit? I’m happy to have a look at your code

Thanks

1 Like

Hey the issue is resolved now.I was facing this exact same issue because of setting the status code.When I removed the status and simply returned the json object the test cases passed.
I was stuck in this for so many hours but your reply really helped.Thanks a lot.

1 Like

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