Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

supertest

Package Overview
Dependencies
Maintainers
5
Versions
69
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

supertest - npm Package Compare versions

Comparing version 0.15.0 to 1.0.0

8

History.md

@@ -1,4 +0,10 @@

0.14.0 / 2014-09-29
1.0.0 / 2015-05-08
===================
* Bumping version to 1.0.0!, big changes with superagent
* Update superagent dependency to 1.2.0
0.15.0 / 2015-11-11
===================
* Update superagent dependency

@@ -5,0 +11,0 @@

19

lib/agent.js

@@ -7,5 +7,5 @@

var Agent = require('superagent').agent
, methods = require('methods')
, http = require('http')
, Test = require('./test');
, methods = require('methods')
, http = require('http')
, Test = require('./test');

@@ -22,10 +22,12 @@ /**

* @param {Function|Server} app
* @param {Object} options
* @api public
*/
function TestAgent(app){
if (!(this instanceof TestAgent)) return new TestAgent(app);
if ('function' == typeof app) app = http.createServer(app);
Agent.call(this);
this.app = app;
function TestAgent(app, options){
if (!(this instanceof TestAgent)) return new TestAgent(app, options);
if ('function' == typeof app) app = http.createServer(app);
if (options) this._ca = options.ca;
Agent.call(this);
this.app = app;
}

@@ -43,2 +45,3 @@

var req = new Test(this.app, method.toUpperCase(), url);
req.ca(this._ca);

@@ -45,0 +48,0 @@ req.on('response', this.saveCookies.bind(this));

@@ -126,3 +126,2 @@ /**

end.call(this, function(err, res){
if (err) return fn(err);
if (server) return server.close(assert);

@@ -133,3 +132,3 @@

function assert(){
self.assert(res, fn);
self.assert(err, res, fn);
}

@@ -149,3 +148,3 @@ });

Test.prototype.assert = function(res, fn){
Test.prototype.assert = function(resError, res, fn){
var status = this._status

@@ -169,3 +168,3 @@ , fields = this._fields

var b = util.inspect(res.body);
return fn(error('expected ' + a + ' response body, got ' + b, body, res.body));
return fn(error('expected ' + a + ' response body, got ' + b, body, res.body), res);
}

@@ -181,6 +180,6 @@ } else {

if (!body.test(res.text)) {
return fn(error('expected body ' + b + ' to match ' + body, body, res.body));
return fn(error('expected body ' + b + ' to match ' + body, body, res.body), res);
}
} else {
return fn(error('expected ' + a + ' response body, got ' + b, body, res.body));
return fn(error('expected ' + a + ' response body, got ' + b, body, res.body), res);
}

@@ -195,3 +194,3 @@ }

actual = res.header[field.toLowerCase()];
if (null == actual) return fn(new Error('expected "' + field + '" header field'));
if (null == actual) return fn(new Error('expected "' + field + '" header field'), res);
for (var i = 0; i < expecteds.length; i++) {

@@ -202,4 +201,4 @@ var fieldExpected = expecteds[i];

if (re && re.test(actual)) continue;
if (re) return fn(new Error('expected "' + field + '" matching ' + fieldExpected + ', got "' + actual + '"'));
return fn(new Error('expected "' + field + '" of "' + fieldExpected + '", got "' + actual + '"'));
if (re) return fn(new Error('expected "' + field + '" matching ' + fieldExpected + ', got "' + actual + '"'), res);
return fn(new Error('expected "' + field + '" of "' + fieldExpected + '", got "' + actual + '"'), res);
}

@@ -224,7 +223,7 @@ }

}
if (!err) continue;
return fn(err instanceof Error ? err : new Error(err))
if (!(err instanceof Error)) continue;
return fn(err instanceof Error ? err : new Error(err), res)
}
fn.call(this, null, res);
fn.call(this, resError, res);
};

@@ -231,0 +230,0 @@

{
"name": "supertest",
"version": "0.15.0",
"version": "1.0.0",
"description": "Super-agent driven library for testing HTTP servers",

@@ -10,10 +10,14 @@ "main": "index.js",

"dependencies": {
"superagent": "~0.21.0",
"superagent": "~1.2.0",
"methods": "1.x"
},
"devDependencies": {
"express": "3.1.0",
"mocha": "1.19.0",
"should": "3.3.1"
"express": "3.20.2",
"mocha": "2.2.4",
"should": "6.0.1",
"body-parser": "~1.12.3"
},
"engines": {
"node": ">=0.8.0"
},
"keywords": [

@@ -20,0 +24,0 @@ "superagent",

@@ -1,2 +0,2 @@

# SuperTest
# SuperTest [![Build Status](https://travis-ci.org/visionmedia/supertest.svg?branch=master)](https://travis-ci.org/visionmedia/supertest)

@@ -10,2 +10,11 @@ HTTP assertions made easy via [super-agent](http://github.com/visionmedia/superagent).

## Getting Started
Install SuperTest as an npm module and save it to your package.json file as a development dependency:
```
npm install supertest --save-dev
```
Once installed it can now be referenced by simply calling ```require("supertest");```
## Example

@@ -54,2 +63,18 @@

One thing to note with the above statement is that superagent now sends any HTTP
error (anything other than a 2XX response code) to the callback as the first arguement. Example:
```js
describe('GET /redirect-url', function(){
it('respond with 302 redirect', function(done){
request(app)
.get('/redirect-url')
.expect(302, function (error) {
(error !== null).should.be.true;
done();
});
})
})
```
If you are using the `.end()` method `.expect()` assertions that fail will

@@ -79,2 +104,3 @@ not throw - they will return the assertion as an error to the `.end()` callback. In

.post('/')
.field('name', 'my awesome avatar')
.attach('avatar', 'test/fixtures/homeboy.jpg')

@@ -106,10 +132,5 @@ ...

var app = express();
app.use(express.cookieParser());
describe('request.agent(app)', function(){
var app = express();
app.use(express.cookieParser());

@@ -169,3 +190,3 @@

Pass a custom assertion function. It'll be given the response object to check. If the response is ok, it should return falsy, most commonly by not returning anything. If the check fails, throw an error or return a truthy value like a string that'll be turned into an error.
Pass a custom assertion function. It'll be given the response object to check. If the response is ok, it should return falsy, most commonly by not returning anything. If the check fails, throw an error or return a truthy value like a string that'll be turned into an error.

@@ -172,0 +193,0 @@ Here the string or error throwing options are both demonstrated:

@@ -8,2 +8,3 @@

, express = require('express');
var bodyParser = require('body-parser');

@@ -24,3 +25,3 @@ describe('request(url)', function(){

});
})
});

@@ -31,2 +32,6 @@ describe('.end(cb)', function() {

app.get('/', function(req, res){
res.send('hello');
});
var s = app.listen(function(){

@@ -40,5 +45,5 @@ var url = 'http://localhost:' + s.address().port;

});
})
})
})
});
});
});

@@ -56,7 +61,7 @@ describe('request(app)', function(){

.end(function(err, res){
res.should.have.status(200);
res.status.should.equal(200);
res.text.should.equal('hey');
done();
});
})
});

@@ -74,3 +79,3 @@ it('should work with an active server', function(done){

.end(function(err, res){
res.should.have.status(200);
res.status.should.equal(200);
res.text.should.equal('hey');

@@ -80,3 +85,3 @@ done();

});
})
});

@@ -94,3 +99,3 @@ it('should work with remote server', function(done){

.end(function(err, res){
res.should.have.status(200);
res.status.should.equal(200);
res.text.should.equal('hey');

@@ -100,3 +105,3 @@ done();

});
})
});

@@ -120,7 +125,7 @@ it('should work with a https server', function(done){

if (err) return done(err);
res.should.have.status(200);
res.status.should.equal(200);
res.text.should.equal('hey');
done();
});
})
});

@@ -130,3 +135,3 @@ it('should work with .send() etc', function(done){

app.use(express.bodyParser());
app.use(bodyParser.json());

@@ -141,3 +146,3 @@ app.post('/', function(req, res){

.expect('tobi', done);
})
});

@@ -154,3 +159,3 @@ it('should work when unbuffered', function(done){

.expect('Hello', done);
})
});

@@ -166,4 +171,4 @@ it('should default redirects to 0', function(done){

.get('/')
.expect(302, done);
})
.expect(302, function() { done(); });
});

@@ -183,3 +188,3 @@ it('should handle socket errors', function(done) {

});
})
});

@@ -238,3 +243,3 @@ describe('.end(fn)', function(){

(err === null).should.be.true;
res.should.have.status(200);
res.status.should.equal(200);
res.text.should.equal('supertest FTW!');

@@ -262,4 +267,4 @@ done();

});
})
})
});
});

@@ -272,3 +277,3 @@ describe('.expect(status)', function () {

res.send('hey');
})
});

@@ -279,4 +284,4 @@ request(app)

.end(done)
})
})
});
});

@@ -313,3 +318,3 @@ describe('.expect(status, body[, fn])', function(){

});
})
});

@@ -333,3 +338,3 @@ describe('.expect(body[, fn])', function(){

});
})
});

@@ -367,3 +372,3 @@ it('should assert the body before the status', function (done) {

.expect('{"foo":"bar"}', done);
})
});

@@ -390,3 +395,3 @@ it('should assert the parsed response body', function(done){

});
})
});

@@ -407,3 +412,3 @@ it('should support regular expressions', function(done){

});
})
});

@@ -426,3 +431,3 @@ it('should assert response body multiple times', function(done){

});
})
});

@@ -441,4 +446,4 @@ it('should assert response body multiple times with no exception', function(done){

.expect('hey tj', done);
})
})
});
});

@@ -460,3 +465,3 @@ describe('.expect(field, value[, fn])', function(){

});
})
});

@@ -477,3 +482,3 @@ it('should assert the header field value', function(done){

});
})
});

@@ -492,3 +497,3 @@ it('should assert multiple fields', function(done){

.end(done);
})
});

@@ -509,3 +514,3 @@ it('should support regular expressions', function(done){

});
})
});

@@ -526,13 +531,21 @@ it('should support numbers', function(done){

});
})
});
describe('handling arbitrary expect functions', function(){
it('reports errors',function(done) {
var app = express();
var app, get;
before(function(){
app = express();
app.get('/', function(req, res){
res.send('hey');
});
});
request(app)
.get('/')
beforeEach(function(){
get = request(app).get('/');
});
it('reports errors',function(done) {
get
.expect(function(res) {

@@ -547,10 +560,4 @@ throw new Error("failed")

it('ensures truthy non-errors returned from asserts are promoted to errors',function(done){
var app = express();
app.get('/', function(req, res){
res.send('hey');
});
request(app)
.get('/')
it('ensures truthy non-errors returned from asserts are not promoted to errors',function(done){
get
.expect(function(res) {

@@ -560,3 +567,3 @@ return "some descriptive error";

.end(function(err) {
err.message.should.equal('some descriptive error');
should.not.exist(err);
done()

@@ -566,10 +573,16 @@ });

it("doesn't create false negatives", function(done){
var app = express();
app.get('/', function(req, res){
res.send('hey');
it('ensures truthy errors returned from asserts are throw to end',function(done){
get
.expect(function(res) {
return new Error("some descriptive error");
})
.end(function(err) {
err.message.should.equal("some descriptive error");
(err instanceof Error).should.be.true;
done();
});
});
request(app)
.get('/')
it("doesn't create false negatives", function(done){
get
.expect(function(res) {})

@@ -580,10 +593,4 @@ .end(done);

it("handles multiple asserts", function(done){
var app = express();
app.get('/', function(req, res){
res.send('hey');
});
var calls = [];
request(app)
.get('/')
get
.expect(function(res) { calls[0] = 1 })

@@ -602,9 +609,3 @@ .expect(function(res) { calls[1] = 1 })

it("plays well with normal assertions - no false positives", function(done){
var app = express();
app.get('/', function(req, res){
res.send('hey');
});
request(app)
.get('/')
get
.expect(function(res) {})

@@ -615,13 +616,7 @@ .expect('Content-Type', /json/)

done();
})
});
});
it("plays well with normal assertions - no false negatives", function(done){
var app = express();
app.get('/', function(req, res){
res.send('hey');
});
request(app)
.get('/')
get
.expect(function(res) {})

@@ -699,8 +694,6 @@ .expect('Content-Type', /html/)

});
});
});
});
})
})
describe('request.agent(app)', function(){

@@ -727,3 +720,3 @@ var app = express();

.expect('set-cookie', 'cookie=hey; Path=/', done);
})
});

@@ -734,4 +727,4 @@ it('should send cookies', function(done){

.expect('hey', done);
})
})
});
});

@@ -751,3 +744,3 @@ describe(".<http verb> works as expected", function(){

var app = express();
app.del('/', function(req, res){
app.delete('/', function(req, res){
res.send(200);

@@ -791,1 +784,66 @@ });

});
describe("request.get(url).query(vals) works as expected", function(){
it("normal single query string value works", function(done) {
var app = express();
app.get('/', function(req, res){
res.send(200, req.query.val);
});
request(app)
.get('/')
.query({val: "Test1"})
.expect(200, function(err, res) {
res.text.should.be.equal('Test1');
done();
});
});
it("array query string value works", function(done) {
var app = express();
app.get('/', function(req, res){
res.send(200, Array.isArray(req.query.val));
});
request(app)
.get('/')
.query({'val[]': ["Test1", "Test2"]})
.expect(200, function(err, res) {
res.req.path.should.be.equal('/?val%5B%5D=Test1&val%5B%5D=Test2');
res.text.should.be.equal('true');
done();
});
});
it("array query string value work even with single value", function(done) {
var app = express();
app.get('/', function(req, res){
res.send(200, Array.isArray(req.query.val));
});
request(app)
.get('/')
.query({'val[]': ["Test1"]})
.expect(200, function(err, res) {
res.req.path.should.be.equal('/?val%5B%5D=Test1');
res.text.should.be.equal('true');
done();
});
});
it("object query string value works", function(done) {
var app = express();
app.get('/', function(req, res){
res.send(200, req.query.val.test);
});
request(app)
.get('/')
.query({val: { test: 'Test1' } })
.expect(200, function(err, res) {
res.text.should.be.equal('Test1');
done();
});
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc