Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 633 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
node.js: Mock http request and response

#1
Is there convenient way to mock the HTTP Request and Response objects for unit testing middlewares?
Reply

#2
i'm using nodejutsu mock:

[To see links please register here]


Maybe this is what you are looking for.
Reply

#3
I wrote a library to mock out the responses of requests made via standard HTTP or via the request model:

[To see links please register here]

Reply

#4
It looks like both

[To see links please register here]

and

[To see links please register here]

can be used to create mock `http.ServerRequest` and `http.ServerResponse` objects.
Reply

#5
From the tag, it looks like this question is about Express. In that case, [supertest](

[To see links please register here]

) is very good:

var request = require('supertest')
, express = require('express');

var app = express();

app.get('/user', function(req, res){
res.send(201, { name: 'tobi' });
});

request(app)
.get('/user')
.expect('Content-Type', /json/)
.expect('Content-Length', '20')
.expect(201)
.end(function(err, res){
if (err) throw err;
});

For general Node use, [Flatiron Nock](

[To see links please register here]

) looks like a good option:

var nock = require('nock');
var example = nock('http://example.com')
.get('/foo')
.reply(200, { foo: 'bar' });

var http = require('http');
var options = {
host: 'example.com',
port: 80,
path: '/foo',
method: 'GET'
}
var req = http.request(options, function(res) {
res.on('data', function(chunk) {
console.log('BODY: ' + chunk);
});
});

req.on('error', function(e) {
console.log('error: ' + e);
});

req.end();

Output:

BODY: {"foo":"bar"}
Reply

#6
Check out

[To see links please register here]

or `npm install hmock`...any feedback welcome! The solution has worked well for me thus far.
Reply

#7
[Mockery](

[To see links please register here]

) looks great for this.

Essentially it hijacks `require` calls, and returns a different object/function stub that you specify.
Reply

#8
I encourage you to use [motty][1]. why do we need an another code?


[1]:

[To see links please register here]

Reply

#9
Belatedly, if you're looking only to unit test the handler, you could inject `express.Request` and `express.Response` objects into the request function, or in this case, your middleware.
One that seemed to provide the minimal methods while keeping it simple was, for me, [@jest-mock/express](

[To see links please register here]

)

If you're using `supertest` or `nock`, you're doing an integration test which can couple multiple tests. I'd also look into how it works internally because it is going be a pain to debug once it stops working.


```javascript
it('should call next',
async () => {
const req = getMockReq({
headers: {
'user-agent': 'Chrome',
},
path: '/path',
})
const{res, next, clearMockRes} = getMockRes({})
await middleware(req, res, next)

expect(res.send).not.toHaveBeenCalledWith()
expect(next).toHaveBeenCalled()
})
```
Reply

#10
I *do not* recommend this solution for real world usage, but this is the hack I used just to avoid exceptions in an experiment.

const mock_response_obj = {
statusCode: null,
setHeader: () => {},
writeHead: () => {},
end: () => {},
}

Obviously extend it as needed. Sharing in case anyone else is looking for a simple stopgap.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through