vlucas / frisby

Frisby is a REST API testing framework built on Jest that makes testing API endpoints easy, fast, and fun.

Home Page:http://frisbyjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to set headers and rawBody into Post request

oleksandr-maliarenko opened this issue · comments

Hi!

Trying to write a simple test for endpoint that accepts only rawBody + headers needed.
How can I do this?
Things I tried:

  1. { request: { headers: { 'Content-Type': 'application/json' }, body: { email: 'a@b', slug: 'test' }, rawBody: true, } }

  2. { request: { headers: { 'Content-Type': 'application/json' }, body: { email: 'a@b', slug: 'test' }, json: true, } }

  3. tried to separate body and headers as 2 objects;

  4. without headers

  5. only object with parameters;

  6. only object with parameters + {json: true} or {rawBody: true};

  7. with .setup();

But in return I got whether
"FetchError: request to http://some_url/api/v2/endpoint failed, reason: Parse Error" or
"400 Error"

Please, help :)

@Viterzgir666

Does following code work as you expected ?

const frisby = require('frisby');

it('test', () => {
  return frisby.setup({
    request: {
      headers: {
        'Content-Type': 'application/json'
      }
    }
  }, true)
    .post(URL, {
      body: { email: 'a@b', slug: 'test' }
    })
    .expect('status', 200);
});

@H1Gdev

Thanks for help,
but, unfortunately, no :(

Got this error:
FetchError: request to http://kalkomey-ams.dev.digicode.ua/api/v2/forgot_password failed, reason: Parse Error at ClientRequest.<anonymous> (node_modules/node-fetch/lib/index.js:1444:11)

Can it be some kind of bug on the server?
I have a very similar test, where only endpoint is different and there's 1 additional parameter, but that's it - and it works well.

Also, I have this test in Postman - it works there too.

@Viterzgir666

Can you paste request header and body for Postman ?

@H1Gdev
Sure, here it is:

Headers:
Content-Type: application/json
Body:
raw is checked, JSON(application/json) is selected in dropdown for body
{ "email": "test@test.com", "slug": "nevada" }

@Viterzgir666

The request header sent by frisby is as follows.(Above test code)

POST / HTTP/1.1
Content-Type: application/json
Accept: */*
Content-Length: 29
User-Agent: node-fetch/1.0 (+https://github.com/bitinn/node-fetch)
Accept-Encoding: gzip,deflate
Connection: close

Please try to setup User-Agent on frisby.

const frisby = require('frisby');

it('test', () => {
  return frisby.setup({
    request: {
      headers: {
        'Content-Type': 'application/json',
        'User-Agent': USER_AGENT // What is User-Agent ?
      }
    }
  }, true)
    .post(URL, {
      body: { email: 'a@b', slug: 'test' }
    })
    .expect('status', 200);
});

@Viterzgir666

From error reason Parse Error at ClientRequest.<anonymous>, there may be a mistake in Request JavaScript object.

@H1Gdev
Tried with User-Agent - the same error appears. Tried with Chrome browser, Android+Chrome, Android Application - Billboard.

Tried to stringify body, then response is changed to a huge html site response with logs. Title is this:
ActionDispatch::ParamsParser::ParseError 822: unexpected token at 'email=test%40test.com&slug=nevada'

If I set email to incorrect one - I recieve CORRECT response from the server:
FAILURE Status: 401 JSON: { "error": "WRONG_EMAIL" }

It just doesn't work.
There's simple body JSON object, header requires only 'Content-Type'.

I already wrote tests with Frisby. There's nothing special with this request. It just doesn't work and I don't understand why.

@Viterzgir666

Has your FetchError issue been solved ?

@Viterzgir666

Execute following code.

const frisby = require('frisby');

it('test', () => {
  return frisby.setup({
    request: {
      headers: {
        'Content-Type': 'application/json',
        'User-Agent': 'frisby/2.1.1 (+https://github.com/vlucas/frisby)'
      },
      timeout: 10000
    }
  }, true)
    .post(URL, {
      body: { email: 'test@test.com', slug: 'nevada' }
    });
}, 10000);

And send following HTTP request.

POST / HTTP/1.1
Content-Type: application/json
User-Agent: frisby/2.1.1 (+https://github.com/vlucas/frisby)
Accept: */*
Content-Length: 41
Accept-Encoding: gzip,deflate
Connection: close

{"email":"test@test.com","slug":"nevada"}

How are you execute frisby ?
Please paste code that becomes FetchError error.

@H1Gdev

it('test', () => {
  return frisby.setup({
    request: {
      headers: {
        'Content-Type': 'application/json',
        'User-Agent': 'frisby/2.1.1 (+https://github.com/vlucas/frisby)'
      },
      timeout: 10000
    }
  }, true)
    .post(URL, {
      body: { email: 'test@test.com', slug: 'nevada' }
    })
    .expect('status', 200);
}, 10000);

this code returns me

FetchError: request to http://base_url/api/v2/forgot_password failed, reason: Parse Error at ClientRequest.<anonymous> (node_modules/node-fetch/lib/index.js:1444:11)

If I change email in body into invalid one - I'll got correct 400 error with description that email is incorrect. But valid request returns FetchError.

Sorry, missclick :/

  ● test

    FetchError: request to http://base_url/api/v2/forgot_password failed, reason: getaddrinfo ENOTFOUND base_url base_url:80

      at ClientRequest.<anonymous> (node_modules/node-fetch/lib/index.js:1444:11)

@H1Gdev I'm just not allowed to share my URL :(

Can I pm it to you somehow?

@Viterzgir666

Don't worry.
It is just a note.

@Viterzgir666

This HTTP request sent by frisby is generally fine.
However it's not what this server expected.

It is successful in Postman.
So send the same request as Postman.
Please tell me actual exact HTTP request header and body in Postman.

@H1Gdev
https://www.getpostman.com/collections/3fcc25f40ce9e6bb3b05

sorry, the url is still wrong, but everything else is the same as I use for request.

@Viterzgir666

Postman sends following HTTP request.

POST /api/v2/forgot_password HTTP/1.1
Connection: keep-alive
Content-Length: 65
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36
Cache-Control: no-cache
Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
Postman-Token: 0451f717-fe0b-e151-1aab-bcd3ec97ff17
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: ja,en-US;q=0.9,en;q=0.8

{
      "email": "test@test.com",
      "slug": "nevada"
    }

@H1Gdev

I know :)
Do you recommend me to add all those header parameters?
I already tried with User-Agent and Content-Type, though

@Viterzgir666

Do you recommend me to add all those header parameters?

Yes.
At first, add all those header parameters.

I do not know specifications of your server.

Instead of having to use setup to send those headers, another alternative is to use fetch directly:

it('test', () => {
  return frisby.fetch(URL, {
      method: 'post',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ email: 'a@b', slug: 'test' }),
    })
    .expect('status', 200);
});

Does it work when you do this?

API is broken for now - can't test.
But thanks for the reply.

Closing because it is not irrelevant anymore.
Don't have this error in other projects so it is fine, I guess :)