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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the scope.request event emitter to include the request body #1062

Merged
merged 1 commit into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,7 @@ nock.removeInterceptor(interceptor);

A scope emits the following events:

* `emit('request', function(req, interceptor))`;
* `emit('request', function(req, interceptor, body))`;
* `emit('replied', function(req, interceptor))`;

## Global no match event
Expand Down
2 changes: 1 addition & 1 deletion lib/request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
interceptor.req = req;
req.headers = req.getHeaders ? req.getHeaders() : req._headers;

interceptor.scope.emit('request', req, interceptor);
interceptor.scope.emit('request', req, interceptor, requestBody);

if (typeof interceptor.errorMessage !== 'undefined') {
interceptor.interceptionCounter++;
Expand Down
36 changes: 36 additions & 0 deletions tests/test_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

var nock = require('../.');
var http = require('http');
var Buffer = require('buffer').Buffer;
var querystring = require('querystring');
var test = require('tap').test;

test('emits request and replied events', function(t) {
Expand All @@ -22,6 +24,40 @@ test('emits request and replied events', function(t) {
http.get('http://eventland/please');
});

test('emits request and request body', function(t) {
var data = querystring.stringify({
example: 123,
});

var scope = nock('http://eventland')
.post('/please')
.reply(200);

scope.on('request', function (req, interceptor, body) {
t.equal(req.path, '/please');
t.equal(interceptor.interceptionCounter, 0);
t.equal(body, data);
scope.on('replied', function (req, interceptor) {
t.equal(req.path, '/please');
t.equal(interceptor.interceptionCounter, 1);
t.end();
});
});

var req = http.request({
hostname: 'eventland',
method: 'POST',
path: '/please',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data)
}
});

req.write(data);
req.end();
});

test('emits no match when no match and no mock', function(t) {
nock.emitter.once('no match', function(req) {
t.end();
Expand Down