Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request body is not preserved #15

Closed
mushketyk opened this issue Sep 12, 2018 · 4 comments
Closed

Request body is not preserved #15

mushketyk opened this issue Sep 12, 2018 · 4 comments

Comments

@mushketyk
Copy link

I am trying to verify if a POST request was performed with a correct request body. Here is my test setup:

describe('Client', () => {
  const server = new ServerMock({ host: 'localhost', port: 9001 })

  beforeEach(function(done) {
    server.start(done)
  })

  afterEach(function(done) {
    server.stop(done)
  })

   it('test', async done => {
    server.on({
      method: 'POST',
      path: '/path',
      reply: {
        status: 200,
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify({})
      }
    })

    // Perfrom POST request
    axios.post(...)

    const requests = server.requests({ method: 'POST', path: '/path' })
    // This assert passes
    expect(requests.length).toEqual(1)
    // This assert fails since "requests[0].body" is undefined
    expect(requests[0].body).toEqual({
      id: 5
    })
    done()
  })

})

Despite the fact that body is sent with the request (checked with Wireshark) it is not preserved on a request object.

@hannupekka
Copy link

hannupekka commented Nov 5, 2018

In my case request has content-type header with value application/json; charset=utf-8 so https://github.com/spreaker/node-mock-http-server/blob/master/src/server.js#L74 skips parsing request body.

There is a PR which should resolve this: #16

@pracucci
Copy link
Collaborator

@hannupekka I've just released version 1.1.0 which includes the PR #16 and should solve your issue. May you try again and let me know, please?

@hannupekka
Copy link

Seems to work now, thanks!

@pracucci
Copy link
Collaborator

Thanks for the reply. I'm then closing this issue, but feel free to re-open it in case the PR #16 doesn't fully solve your use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants