URL Shortener Question

Hi @skaparate!

I am having the same issue on my project, it works but not passing on the test…
Can you please see what is going on? I would be glad. :grinning_face_with_smiling_eyes:

juliano988/boilerplate-project-urlshortener: A boilerplate for a freeCodeCamp project. (github.com)

I have moved your post to a new thread.

3 Likes

Hello!

Your problem is how you’re validating the URL.

The first problem is that you expect that every URL end with / which would invalidate valid ones:

app.post('/api/shorturl/new', function(req, res) {
    if ((/^(http|https):\/\/.+/).test(req.body.link)) {

Test your validator in isolation for the following (not exhaustive, but valid) URLs:

[
    'http://freecodecamp.org',
    'http://freecodecamp.org/',
    'http://www.freecodecamp.org',
    'http://www.freecodecamp.org/',
    'https://freecodecamp.org',
    'https://freecodecamp.org/',
    'https://www.freecodecamp.org',
    'https://www.freecodecamp.org/',
    'http://freecodecamp.org/path',
    'http://freecodecamp.org/path/',
    'http://freecodecamp.org/path?a=1',
    'http://freecodecamp.org/path?a=1&b=2'
]

All of those are valid, plus you should also expect other protocols, like ftp: ftp://domain.tld/etc.

If you don’t want to come with your own solution, your could use the URL API of nodejs: https://nodejs.org/api/url.html.

1 Like

Hi again @skaparate!

I think that is not actually the problem, I have tried a lot of different ways to validate the URL but the problem still persists…

And just to make sure, I have searched on the internet this project to check if they were going to pass or not, but no one did…
The only one which actually passed was the example one.

At this point I really don’t know what’s else can I do to pass.

Are you sure that is nothing wrong with the test? This project shouldn’t be that hard.

URL Shortener Microservice | freeCodeCamp.org (juliano988.repl.co)

juliano988/boilerplate-project-urlshortener: A boilerplate for a freeCodeCamp project. (github.com)

Other people have passed this project, so I don’t think the tests are seriously flawed. The same fix was needed in the other topic you found.

2 Likes

I’m not saying that’s the only problem, but it’s the main one :slight_smile:.

One of the tests sends the following valid URL: https://timestamp-microservice.freecodecamp.rocks/api/timestamp/1606674081372. Your project responds with the following (bottom right of the image, says error: "invalid url"):

The same happens with another test using a valid URL, so, the main problem is how you’re validating the URLs :slight_smile:.

1 Like

Im having a similar problem as the OP. Id like to reference the advice I found on this thread, which unfortunately did not work for me:

Ive tested all of these, and it is redirecting as I would expect, yet the project is still not passing the test:

When you visit /api/shorturl/<short_url> , you will be redirected to the original URL.

repl link

I believe there is a section after this which deals with software testing… Since Ive not advanced to the next section yet, Im not sure how to interpret this test, in order to resolve the problem:

async (getUserInput) => {
  const url = getUserInput('url');
  const urlVariable = Date.now();
  let shortenedUrlVariable;
  const postResponse = await fetch(url + '/api/shorturl/new/', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: `url=https://timestamp-microservice.freecodecamp.rocks/api/timestamp/${urlVariable}`
  });
  if (postResponse.ok) {
    const { short_url } = await postResponse.json();
    shortenedUrlVariable = short_url;
  } else {
    throw new Error(`${postResponse.status} ${postResponse.statusText}`);
  }
  const getResponse = await fetch(
    url + '/api/shorturl/' + shortenedUrlVariable
  );
  if (getResponse) {
    const { redirected, url } = getResponse;
    assert.isTrue(redirected);
    assert.strictEqual(
      url,
      `https://timestamp-microservice.freecodecamp.rocks/api/timestamp/${urlVariable}`
    );
  } else {
    throw new Error(`${getResponse.status} ${getResponse.statusText}`);
  }
};

Your project passes for me. Maybe you fixed before I tested?

1 Like

negative, I did not change anything.

To be clear then, it still doesn’t pass the tests? If so, it doesn’t pass any tests or just the one that you posted in your last message?

Could you try using a different browser and/or disabling all plugins/extensions from your browser?

Yes sir, It passes. I was replying to your question asking if i had changed anything about it since you tested it.

I was trying to get it work for hours but then i saw this:

1 Like