Git Product home page Git Product logo

node-mock-http-server's People

Contributors

dhensby avatar epalumbo avatar lele85 avatar loicsimon avatar nautigsam avatar pracucci avatar teorossi82 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-mock-http-server's Issues

v1.4.3+ results in ESOCKETTIMEDOUT error

I've recently upgraded a project using v1.4.2 to v.1.4.4 and found that tests I'm using the mock server in are failing with ESOCKETTIMEDOUT errors. If I roll back to 1.4.2 the issue goes away.

It appears that the body-parser.text() function on line 203 of server.js is causing a problem, if this is commented out then the tests pass as expected.

edit: The content type is multipart/form-data

[question] Handler overrides

Hi, is there any way to override a handler using the "on(...)" method? I saw that handlers are pushed into an array, and then, if a new handler for the same kind of request is pushed, the previous one takes precedence.
If this array push is changed to a "unshift" operation the new handlers will be found first when filtering to handle a mocked request. What do you think?

Expecting calls to the same path in the same test

I have a scenario where I am setting up expectations to call an external service twice (mocking that in my tests using mock-https-server), one which returns an empty array and one that returns a response.

something like this:

  expectGetResourceByParamX (queryParams, statusCode, body) {
    this.server.on({
      method: 'GET',
      path: '/v1/resource',
      filter: (req) => {
       // filter code
        return true
      },
      reply: {
        status: statusCode,
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify(body)
      }
    })
  }

  expectGetResourceByParamY (queryParams, statusCode, body) {
    this.server.on({
      method: 'GET',
      path: '/v1/resource',
      filter: (req) => {
       // filter code
        return true
      },
      reply: {
        status: statusCode,
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify(body)
      }
    })
  }

The only difference is what query params I use on that same exact path, however when setting up the test this seems to cause an issue since the handlers have the same path and the requests that come in are processed by the wrong handler. I looked at the library code but can't see an obvious place where this can be fixed.

Any ideas on how this sort of problem can be solved (if it can be) using this library or is this some sort of limitation of the library.

Thank You.

requests method uses OR logic to filter by method and path

The documentation gives an example that implies that the requests should be filtered using AND logic:

// Returns all GET requests to /resource
server.requests({ method: "GET", path: "/resource" });

However the implementation of requests method uses "OR" logic to filter requests by method and path

Request body is not preserved

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.

XML support

Please add possibility to mock text/xml content-type

resetHandlers() doesn't clean requests array

We are bullet proofing an API which calls another API. This second API is mocked by using mock-http-server.
We thought that by calling resetHandlers() and recreating an handler before each unit test, it would also clear previously received requests but it doesn't.

Is there any cleanRequests() function?
Wouldn't it be great to clean the requests array when we use resetHandlers() (see below) ?

    this.resetHandlers = function() {
        requests = [];
        handlers = [];
    };

BTW, we don't want to stop/start a server before each unit test. It takes a bit of time to stop the server.

Thank you.

Add the ability to set a random port number

For my tests I don't like to choose a port number myself. I prefer to let the system to assign me a random port number. Since mock-http-server uses http.Server under the hood, I am able to set port number as 0, but I can not get the assigned port number back. It would be great that the server instance expose this information.

Querystring support?

I find it hard to believe it, but it seems that the querystring of the request is totally ignored, and paths with querystrings are not matched. Given this code:

server.on({
    method: 'GET',
    path: '/test?foo=bar',
    reply: {
        status: 200,
        headers: {"content-type": "application/json"},
        body: JSON.stringify({what: 'params'})
    }
});

server.on({
    method: 'GET',
    path: '/test',
    reply: {
        status: 200,
        headers: {"content-type": "application/json"},
        body: JSON.stringify({what: 'no-params'})
    }
});

And then

❯ curl http://localhost:8888/test
{"what":"no-params"}
~ 
❯ curl http://localhost:8888/test?foo=bar
{"what":"no-params"}

Am I missing something?

How to get body of an HTTP request?

The documentation provides a way to get requests sent to a mock server, e.g.:

// Returns all requests
server.requests();

But it does not specify how to get a body of a request.

Could you update documentation and provide examples how to get more information about a request, e.g. body, headers, etc.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.