Skip to content

Commit 456dc9a

Browse files
authored
Add methods for checking request headers (#7)
* Add requestedWithHeaders method * Rename requestedWithHeaders to requestedWithExactHeaders * Add requestedWithHeaders tests * fix tests and implementation of requestedWithHeaders * add requestedWithExactHeaders and requestedWithHeaders to README * Update readme * Update exact headers match messages * 1.1.0 * Revert chai truncate change in requestedWith tests * Add missing test scenario to requestedWithHeaders * Install linting deps * Set linting config * Make linting changes * Fix bug in requestedWithHeaders method * Lint new test files * Fix requestedWith * Rename requestedWith to requestedWithExactBody * Update requestedWithExactBody tests * Finalise headers methods * Refactor requestedWitHeaders tests * Update readme
1 parent 1ef2e54 commit 456dc9a

7 files changed

+586
-31
lines changed

README.md

+113-9
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ nockedRequest.on('request', function(req, interceptor, body) {
1717
you can write code that expresses what you really mean:
1818

1919
```javascript
20-
return expect(nock('http://some-url')).to.have.been.requestedWith({ hello: 'world' });
20+
return expect(nock('http://some-url')).to.have.been.requestedWith({
21+
hello: 'world'
22+
});
2123
```
2224

23-
2425
## Installation
25-
```npm install chai-nock```
26+
27+
`npm install chai-nock`
2628

2729
Then add to your test setup:
2830

@@ -35,21 +37,47 @@ chai.use(chaiNock);
3537

3638
## Assertions
3739

38-
#### requested
40+
### `requested`
41+
42+
Asserts that a request has been made to the nock.
43+
3944
```javascript
4045
expect(nock).to.have.been.requested;
4146
expect(nock).not.to.have.been.requested;
4247
```
4348

44-
#### requestedWith(body)
49+
#### Example
50+
4551
```javascript
46-
expect(nock).to.have.been.requestedWith(body)
47-
expect(nock).not.to.have.been.requestedWith(body)
52+
const { expect } = require('chai');
53+
const nock = require('nock');
54+
const request = require('request-promise-native');
55+
56+
describe('example', () => {
57+
it('should make a request to bbc.co.uk', function() {
58+
const requestNock = nock('http://bbc.co.uk')
59+
.get('/')
60+
.reply(200);
61+
62+
request({
63+
uri: 'http://bbc.co.uk',
64+
});
65+
66+
return expect(requestNock).to.have.been.requested;
67+
});
68+
});
4869
```
4970

50-
## Examples
71+
### `requestedWith(body)`
72+
73+
Asserts that a request has been made to the nock with a body that exactly matches the object provided.
74+
75+
```javascript
76+
expect(nock).to.have.been.requestedWith(body);
77+
expect(nock).not.to.have.been.requestedWith(body);
78+
```
5179

52-
Using Chai's `expect`:
80+
#### Example
5381

5482
```javascript
5583
const { expect } = require('chai');
@@ -75,5 +103,81 @@ describe('example', () => {
75103
});
76104
```
77105

106+
### `requestedWithHeaders(headers)`
107+
108+
Asserts that a request has been made to the nock with headers that exactly match the object provided.
109+
110+
```javascript
111+
expect(nock).to.have.been.requestedWithHeaders(headers);
112+
expect(nock).not.to.have.been.requestedWithHeaders(headers);
113+
```
114+
115+
#### Example
116+
117+
```javascript
118+
const { expect } = require('chai');
119+
const nock = require('nock');
120+
const request = require('request-promise-native');
121+
122+
describe('example', () => {
123+
it('should make a request to bbc.co.uk with exactly correct headers', function() {
124+
const requestNock = nock('http://bbc.co.uk')
125+
.get('/')
126+
.reply(200);
127+
128+
request({
129+
json: true,
130+
uri: 'http://bbc.co.uk',
131+
headers: {
132+
myHeader: 'myHeaderValue'
133+
}
134+
});
135+
136+
return expect(requestNock).to.have.been.requestedWithHeaders({
137+
host: 'bbc.co.uk',
138+
accept: 'application/json',
139+
myHeader: 'myHeaderValue'
140+
});
141+
});
142+
});
143+
```
144+
145+
_Note: request-promise-native library adds `host` and `accept` headers from the `uri` and `json` options provided so we need to include them in our exact headers object_
146+
147+
### `requestedWithHeadersMatch(headers)`
78148

149+
Asserts that a request has been made to the nock with headers that contain the key/value pairs in the object provided.
79150

151+
```javascript
152+
expect(nock).to.have.been.requestedWithHeadersMatch(headers);
153+
expect(nock).not.to.have.been.requestedWithHeadersMatch(headers);
154+
```
155+
156+
#### Example
157+
158+
```javascript
159+
const { expect } = require('chai');
160+
const nock = require('nock');
161+
const request = require('request-promise-native');
162+
163+
describe('example', () => {
164+
it('should make a request to bbc.co.uk the correct myHeader value in the headers', function() {
165+
const requestNock = nock('http://bbc.co.uk')
166+
.get('/')
167+
.reply(200);
168+
169+
request({
170+
json: true,
171+
uri: 'http://bbc.co.uk',
172+
headers: {
173+
myHeader: 'myHeaderValue',
174+
otherHeader: 'otherHeaderValue'
175+
}
176+
});
177+
178+
return expect(requestNock).to.have.been.requestedWithHeadersMatch({
179+
myHeader: 'myHeaderValue'
180+
});
181+
});
182+
});
183+
```

index.js

+83-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable no-underscore-dangle */
1+
/* eslint-disable no-underscore-dangle, func-names */
22

33
const equal = require('deep-equal');
44

@@ -9,22 +9,28 @@ module.exports = chai => {
99
function promisfyNockInterceptor(nock) {
1010
return new Promise((resolve, reject) => {
1111
let body;
12+
let headers;
1213

1314
const timeout = setTimeout(() => {
1415
reject(new Error('The request has not been recieved by Nock'));
1516
}, MAX_TIMEOUT);
1617

17-
nock.once('request', (req, interceptor, reqBody) => {
18-
try {
19-
body = JSON.parse(reqBody);
20-
} catch (err) {
21-
body = reqBody;
22-
}
23-
});
18+
nock.once(
19+
'request',
20+
({ headers: requestHeaders }, interceptor, reqBody) => {
21+
try {
22+
headers = requestHeaders;
23+
24+
body = JSON.parse(reqBody);
25+
} catch (err) {
26+
body = reqBody;
27+
}
28+
},
29+
);
2430

2531
nock.once('replied', () => {
2632
clearTimeout(timeout);
27-
resolve(body);
33+
resolve({ body, headers });
2834
});
2935

3036
nock.on('error', err => {
@@ -44,8 +50,9 @@ module.exports = chai => {
4450
}
4551
}
4652

47-
Assertion.addProperty('requested', () => {
53+
Assertion.addProperty('requested', function() {
4854
isNock(this._obj);
55+
4956
const assert = value => {
5057
this.assert(
5158
value,
@@ -60,25 +67,84 @@ module.exports = chai => {
6067
);
6168
});
6269

63-
Assertion.addMethod('requestedWith', arg => {
70+
Assertion.addMethod('requestedWith', function(arg) {
71+
isNock(this._obj);
72+
73+
return promisfyNockInterceptor(this._obj).then(
74+
({ body }) => {
75+
if (equal(body, arg)) {
76+
return this.assert(
77+
true,
78+
null,
79+
'expected Nock to have not been requested with exact body #{exp}',
80+
arg,
81+
);
82+
}
83+
return this.assert(
84+
false,
85+
'expected Nock to have been requested with exact body #{exp}, but was requested with body #{act}',
86+
'expected Nock to have not been requested with exact body #{exp}',
87+
arg,
88+
body,
89+
);
90+
},
91+
() =>
92+
this.assert(
93+
false,
94+
'expected Nock to have been requested, but it was never called',
95+
),
96+
);
97+
});
98+
99+
Assertion.addMethod('requestedWithHeaders', function(arg) {
100+
isNock(this._obj);
101+
102+
return promisfyNockInterceptor(this._obj).then(
103+
({ headers }) => {
104+
if (equal(headers, arg)) {
105+
return this.assert(
106+
true,
107+
null,
108+
'expected Nock to have not been requested with headers #{exp}',
109+
arg,
110+
);
111+
}
112+
return this.assert(
113+
false,
114+
'expected Nock to have been requested with headers #{exp}, but was requested with headers #{act}',
115+
'expected Nock to have not been requested with headers #{exp}',
116+
arg,
117+
headers,
118+
);
119+
},
120+
() =>
121+
this.assert(
122+
false,
123+
'expected Nock to have been requested, but it was never called',
124+
),
125+
);
126+
});
127+
128+
Assertion.addMethod('requestedWithHeadersMatch', function(arg) {
64129
isNock(this._obj);
65130

66131
return promisfyNockInterceptor(this._obj).then(
67-
nockRequest => {
68-
if (equal(nockRequest, arg)) {
132+
({ headers }) => {
133+
const mergedHeaders = Object.assign({}, headers, arg);
134+
if (equal(headers, mergedHeaders)) {
69135
return this.assert(
70136
true,
71137
null,
72-
'expected Nock to have not been requested with #{exp}',
138+
'expected Nock to have not been requested with headers #{exp}',
73139
arg,
74140
);
75141
}
76142
return this.assert(
77143
false,
78-
'expected Nock to have been requested with #{exp}, but was requested with #{act}',
79-
'expected Nock to have not been requested with #{exp}',
144+
'expected Nock to have been requested with headers #{exp}, but was requested with headers #{act}',
145+
'expected Nock to have not been requested with headers #{exp}',
80146
arg,
81-
nockRequest,
147+
headers,
82148
);
83149
},
84150
() =>

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "chai-nock",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Extends Chai with assertions for the Nock Http mocking framework",
55
"main": "index.js",
66
"scripts": {
77
"test": "jest test",
8+
"test:watch": "jest test --watch",
89
"style": "eslint ."
910
},
1011
"keywords": [

test/requestedWith.test.js renamed to test/requestedWithExactBody.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { expect, use } = require('chai');
22
const nock = require('nock');
33
const request = require('request-promise-native');
44

5-
const chaiNock = require('../');
5+
const chaiNock = require('..');
66

77
use(chaiNock);
88

@@ -85,7 +85,7 @@ describe('requestedWith() assertions', () => {
8585
.then(() => done.fail('Should have thrown an error'))
8686
.catch(err => {
8787
expect(err.message).to.equal(
88-
'expected Nock to have been requested with { test: 2 }, but was requested with { test: 1 }',
88+
'expected Nock to have been requested with exact body { test: 2 }, but was requested with body { test: 1 }',
8989
);
9090
done();
9191
});
@@ -166,7 +166,7 @@ describe('requestedWith() assertions', () => {
166166
.then(() => done.fail('Should have thrown an error'))
167167
.catch(err => {
168168
expect(err.message).to.equal(
169-
'expected Nock to have not been requested with { test: 12345 }',
169+
'expected Nock to have not been requested with exact body { test: 12345 }',
170170
);
171171
done();
172172
});

0 commit comments

Comments
 (0)