Skip to content

Commit

Permalink
Release v3.1.0 (#474)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rimiti authored and mikelax committed May 13, 2018
1 parent 199506d commit d4a63af
Show file tree
Hide file tree
Showing 8 changed files with 2,347 additions and 30 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
@@ -1,5 +1,5 @@
language: node_js
node_js:
- "6"
- "5"
- "4"
- 6
- 8
- 9
11 changes: 11 additions & 0 deletions History.md
@@ -1,3 +1,14 @@
3.1.0 / 2018-04-24
===================

* PR-473 - Remove unused dependency in Readme (thanks @pedro-otero)
* PR-472 - Update travis node versions (thanks @rimiti)
* PR-470 - Upgrade the superagent node module to resolve security vulnerabilities & fix the __proto__ property deprecation (thanks @levioza)
* PR-446 - Fix bug, always pass on errors if no response (thanks @bkeepers)
* PR-418 - Add post() examples to the README (thanks @kevinburke)
* PR-297 - Add a .host() method to set a host other than 127.0.0.1 (thanks @mikec)
* PR-275 - Removed ambiguously misappropriated cultural references from readme (thanks @reallistic)

3.0.0 / 2017-01-29
===================

Expand Down
22 changes: 12 additions & 10 deletions Readme.md
Expand Up @@ -32,7 +32,7 @@ const express = require('express');
const app = express();

app.get('/user', function(req, res) {
res.status(200).json({ name: 'tobi' });
res.status(200).json({ name: 'john' });
});

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

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

```js
describe('GET /users', function() {
it('respond with json', function() {
it('responds with json', function() {
return request(app)
.get('/users')
.set('Accept', 'application/json')
Expand All @@ -102,18 +103,19 @@ describe('GET /users', function() {
to modify the response body or headers before executing an assertion.

```js
describe('GET /user', function() {
it('user.name should be an case-insensitive match for "tobi"', function(done) {
describe('POST /user', function() {
it('user.name should be an case-insensitive match for "john"', function(done) {
request(app)
.get('/user')
.post('/user')
.send('name=john') // x-www-form-urlencoded upload
.set('Accept', 'application/json')
.expect(function(res) {
res.body.id = 'some fixed id';
res.body.name = res.body.name.toUpperCase();
})
.expect(200, {
id: 'some fixed id',
name: 'TOBI'
name: 'john'
}, done);
});
});
Expand All @@ -125,7 +127,7 @@ Anything you can do with superagent, you can do with supertest - for example mul
request(app)
.post('/')
.field('name', 'my awesome avatar')
.attach('avatar', 'test/fixtures/homeboy.jpg')
.attach('avatar', 'test/fixtures/avatar.jpg')
...
```

Expand Down
10 changes: 8 additions & 2 deletions lib/agent.js
Expand Up @@ -38,12 +38,18 @@ function TestAgent(app, options) {
* Inherits from `Agent.prototype`.
*/

TestAgent.prototype.__proto__ = Agent.prototype;
Object.setPrototypeOf(TestAgent.prototype, Agent.prototype);

// set a host name
TestAgent.prototype.host = function(host) {
this._host = host;
return this;
};

// override HTTP verb methods
methods.forEach(function(method) {
TestAgent.prototype[method] = function(url, fn) { // eslint-disable-line no-unused-vars
var req = new Test(this.app, method.toUpperCase(), url);
var req = new Test(this.app, method.toUpperCase(), url, this._host);
req.ca(this._ca);
req.cert(this._cert);
req.key(this._key);
Expand Down
22 changes: 12 additions & 10 deletions lib/test.js
Expand Up @@ -25,22 +25,22 @@ module.exports = Test;
* @api public
*/

function Test(app, method, path) {
function Test(app, method, path, host) {
Request.call(this, method.toUpperCase(), path);
this.redirects(0);
this.buffer();
this.app = app;
this._asserts = [];
this.url = typeof app === 'string'
? app + path
: this.serverAddress(app, path);
: this.serverAddress(app, path, host);
}

/**
* Inherits from `Request.prototype`.
*/

Test.prototype.__proto__ = Request.prototype;
Object.setPrototypeOf(Test.prototype, Request.prototype);

/**
* Returns a URL, extracted from a server.
Expand All @@ -51,15 +51,15 @@ Test.prototype.__proto__ = Request.prototype;
* @api private
*/

Test.prototype.serverAddress = function(app, path) {
Test.prototype.serverAddress = function(app, path, host) {
var addr = app.address();
var port;
var protocol;

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

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

if (!res && resError && (resError instanceof Error) && (resError.syscall === 'connect')
&& (Object.getOwnPropertyNames(sysErrors).indexOf(resError.code) >= 0)) {
error = new Error(resError.code + ': ' + sysErrors[resError.code]);
fn.call(this, error, null);
return;
if (!res && resError) {
if (resError instanceof Error && resError.syscall === 'connect'
&& Object.getOwnPropertyNames(sysErrors).indexOf(resError.code) >= 0) {
error = new Error(resError.code + ': ' + sysErrors[resError.code]);
} else {
error = resError;
}
}

// asserts
Expand Down

0 comments on commit d4a63af

Please sign in to comment.