Setting status of request matters?

In the URL-Shortner project for validating the url I was using the code
if(!url.protocol.includes(‘http’)) return res.status(400).send({“error”: ‘invalid url’})
and it was failing all the time

until I thought to delete the .status(400) and the test passed like a charm

Is there any specific reason for it?? [ it had me setting on it for 2 days ]

The reason is this test:

async (getUserInput) => {
  const url = getUserInput('url');
  const res = await fetch(url + '/api/shorturl', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: `url=ftp:/`
  if (res.ok) {
    const { error } = await res.json();
    assert.strictEqual(error.toLowerCase(), 'invalid url');
  } else {
    throw new Error(`${res.status} ${res.statusText}`);

The part that caused your problem is the res.ok which is only true if the response status is in the 200’s. I had the same problem. If you look at the list of 400 error codes, asking for an invalid URL doesn’t really fit any of them as it’s a valid request and successfully gets a valid, albeit negative, response.

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