spreaker / node-mock-http-server Goto Github PK
View Code? Open in Web Editor NEWMock an HTTP server with JS
License: MIT License
Mock an HTTP server with JS
License: MIT License
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
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?
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.
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
It is sometimes useful to verify that no unexpected requests are triggered while exercising a test case. Hence I suggest to add a method which simply returns all unmatched requests.
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.
Please add possibility to mock text/xml content-type
Please make it usable with typescript.
what if I want 401, 302 or any custom error?
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.
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.
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?
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.