Skip to content

Commit d4a63af

Browse files
rimitimikelax
authored andcommitted
Release v3.1.0 (#474)
* Removed ambiguously misappropriated cultural references from readme * Add post() examples to the README I have to look up how to do a POST request every time, for JSON and x-www-form-urlencoded. Many of the existing examples duplicate themselves, so I figured we could change some of them to demonstrate the different ways to send a POST request without changing the flow too much. * Add a .host() method to set a host other than 127.0.0.1 This allows you to set the server hostname before making a request. If no hostname is set then it defaults to 127.0.0.1 * Always pass on errors if no response * Upgrade the superagent node module to resolve security vulnerabilities and fix the __proto__ property deprecation * chore(.travis.yml) node versions updated * Remove unused dependency in Readme.MD snippet * chore(package.json) version bumped * chore(package-lock.json) locker file include * doc(History.md) changelog updated * chore(.travis.yml) node v4 remove
1 parent 199506d commit d4a63af

File tree

8 files changed

+2347
-30
lines changed

8 files changed

+2347
-30
lines changed

.travis.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
22
node_js:
3-
- "6"
4-
- "5"
5-
- "4"
3+
- 6
4+
- 8
5+
- 9

History.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
3.1.0 / 2018-04-24
2+
===================
3+
4+
* PR-473 - Remove unused dependency in Readme (thanks @pedro-otero)
5+
* PR-472 - Update travis node versions (thanks @rimiti)
6+
* PR-470 - Upgrade the superagent node module to resolve security vulnerabilities & fix the __proto__ property deprecation (thanks @levioza)
7+
* PR-446 - Fix bug, always pass on errors if no response (thanks @bkeepers)
8+
* PR-418 - Add post() examples to the README (thanks @kevinburke)
9+
* PR-297 - Add a .host() method to set a host other than 127.0.0.1 (thanks @mikec)
10+
* PR-275 - Removed ambiguously misappropriated cultural references from readme (thanks @reallistic)
11+
112
3.0.0 / 2017-01-29
213
===================
314

Readme.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const express = require('express');
3232
const app = express();
3333

3434
app.get('/user', function(req, res) {
35-
res.status(200).json({ name: 'tobi' });
35+
res.status(200).json({ name: 'john' });
3636
});
3737

3838
request(app)
@@ -68,10 +68,11 @@ you do not add a status code expect (i.e. `.expect(302)`).
6868
order to fail the test case, you will need to rethrow or pass `err` to `done()`, as follows:
6969

7070
```js
71-
describe('GET /users', function() {
72-
it('respond with json', function(done) {
71+
describe('POST /users', function() {
72+
it('responds with json', function(done) {
7373
request(app)
74-
.get('/users')
74+
.post('/users')
75+
.send({name: 'john'})
7576
.set('Accept', 'application/json')
7677
.expect(200)
7778
.end(function(err, res) {
@@ -86,7 +87,7 @@ You can also use promises
8687

8788
```js
8889
describe('GET /users', function() {
89-
it('respond with json', function() {
90+
it('responds with json', function() {
9091
return request(app)
9192
.get('/users')
9293
.set('Accept', 'application/json')
@@ -102,18 +103,19 @@ describe('GET /users', function() {
102103
to modify the response body or headers before executing an assertion.
103104

104105
```js
105-
describe('GET /user', function() {
106-
it('user.name should be an case-insensitive match for "tobi"', function(done) {
106+
describe('POST /user', function() {
107+
it('user.name should be an case-insensitive match for "john"', function(done) {
107108
request(app)
108-
.get('/user')
109+
.post('/user')
110+
.send('name=john') // x-www-form-urlencoded upload
109111
.set('Accept', 'application/json')
110112
.expect(function(res) {
111113
res.body.id = 'some fixed id';
112114
res.body.name = res.body.name.toUpperCase();
113115
})
114116
.expect(200, {
115117
id: 'some fixed id',
116-
name: 'TOBI'
118+
name: 'john'
117119
}, done);
118120
});
119121
});
@@ -125,7 +127,7 @@ Anything you can do with superagent, you can do with supertest - for example mul
125127
request(app)
126128
.post('/')
127129
.field('name', 'my awesome avatar')
128-
.attach('avatar', 'test/fixtures/homeboy.jpg')
130+
.attach('avatar', 'test/fixtures/avatar.jpg')
129131
...
130132
```
131133

lib/agent.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,18 @@ function TestAgent(app, options) {
3838
* Inherits from `Agent.prototype`.
3939
*/
4040

41-
TestAgent.prototype.__proto__ = Agent.prototype;
41+
Object.setPrototypeOf(TestAgent.prototype, Agent.prototype);
42+
43+
// set a host name
44+
TestAgent.prototype.host = function(host) {
45+
this._host = host;
46+
return this;
47+
};
4248

4349
// override HTTP verb methods
4450
methods.forEach(function(method) {
4551
TestAgent.prototype[method] = function(url, fn) { // eslint-disable-line no-unused-vars
46-
var req = new Test(this.app, method.toUpperCase(), url);
52+
var req = new Test(this.app, method.toUpperCase(), url, this._host);
4753
req.ca(this._ca);
4854
req.cert(this._cert);
4955
req.key(this._key);

lib/test.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ module.exports = Test;
2525
* @api public
2626
*/
2727

28-
function Test(app, method, path) {
28+
function Test(app, method, path, host) {
2929
Request.call(this, method.toUpperCase(), path);
3030
this.redirects(0);
3131
this.buffer();
3232
this.app = app;
3333
this._asserts = [];
3434
this.url = typeof app === 'string'
3535
? app + path
36-
: this.serverAddress(app, path);
36+
: this.serverAddress(app, path, host);
3737
}
3838

3939
/**
4040
* Inherits from `Request.prototype`.
4141
*/
4242

43-
Test.prototype.__proto__ = Request.prototype;
43+
Object.setPrototypeOf(Test.prototype, Request.prototype);
4444

4545
/**
4646
* Returns a URL, extracted from a server.
@@ -51,15 +51,15 @@ Test.prototype.__proto__ = Request.prototype;
5151
* @api private
5252
*/
5353

54-
Test.prototype.serverAddress = function(app, path) {
54+
Test.prototype.serverAddress = function(app, path, host) {
5555
var addr = app.address();
5656
var port;
5757
var protocol;
5858

5959
if (!addr) this._server = app.listen(0);
6060
port = app.address().port;
6161
protocol = app instanceof https.Server ? 'https' : 'http';
62-
return protocol + '://127.0.0.1:' + port + path;
62+
return protocol + '://' + (host || '127.0.0.1') + ':' + port + path;
6363
};
6464

6565
/**
@@ -159,11 +159,13 @@ Test.prototype.assert = function(resError, res, fn) {
159159
ETIMEDOUT: 'Operation timed out'
160160
};
161161

162-
if (!res && resError && (resError instanceof Error) && (resError.syscall === 'connect')
163-
&& (Object.getOwnPropertyNames(sysErrors).indexOf(resError.code) >= 0)) {
164-
error = new Error(resError.code + ': ' + sysErrors[resError.code]);
165-
fn.call(this, error, null);
166-
return;
162+
if (!res && resError) {
163+
if (resError instanceof Error && resError.syscall === 'connect'
164+
&& Object.getOwnPropertyNames(sysErrors).indexOf(resError.code) >= 0) {
165+
error = new Error(resError.code + ': ' + sysErrors[resError.code]);
166+
} else {
167+
error = resError;
168+
}
167169
}
168170

169171
// asserts

0 commit comments

Comments
 (0)