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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVA 🌠 #104

Merged
merged 1 commit into from
Sep 17, 2015
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
sudo: false
language: node_js
node_js:
- 'iojs'
- 'stable'
- '0.12'
- '0.10'
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"node": ">=0.10.0"
},
"scripts": {
"test": "xo && tap test/test-*.js"
"test": "xo && ava"
},
"files": [
"index.js"
Expand Down Expand Up @@ -57,10 +57,9 @@
"unzip-response": "^1.0.0"
},
"devDependencies": {
"ava": "^0.2.0",
"into-stream": "^2.0.0",
"istanbul": "^0.3.13",
"pem": "^1.4.4",
"tap": "^1.0.0",
"tempfile": "^1.1.1",
"xo": "*"
}
Expand Down
4 changes: 4 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ exports.createServer = function (port) {

port = port || exports.port;

exports.port += 1;

var s = http.createServer(function (req, resp) {
s.emit(req.url, req, resp);
});
Expand All @@ -28,6 +30,8 @@ exports.createSSLServer = function (port, opts) {

port = port || exports.portSSL;

exports.portSSL += 1;

var s = https.createServer(opts, function (req, resp) {
s.emit(req.url, req, resp);
});
Expand Down
22 changes: 11 additions & 11 deletions test/test-arguments.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
var test = require('tap').test;
var test = require('ava');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();
Expand All @@ -17,41 +17,41 @@ s.on('/?test=wow', function (req, res) {
res.end(req.url);
});

test('setup', function (t) {
test.before('arguments - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
});

test('url argument is required', function (t) {
test('arguments - url argument is required', function (t) {
t.plan(2);
t.throws(function () {
got(undefined, function () {});
}, /Parameter `url` must be a string or object, not undefined/);

got()
.catch(function (err) {
t.ok(/Parameter `url` must be a string or object, not undefined/.test(err.message));
t.regexTest(/Parameter `url` must be a string or object, not undefined/, err.message);
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above test could use t.regexTest

});

test('accepts url.parse object as first argument', function (t) {
test('arguments - accepts url.parse object as first argument', function (t) {
got({hostname: s.host, port: s.port, path: '/test'}, function (err, data) {
t.error(err);
t.equal(data, '/test');
t.ifError(err);
t.is(data, '/test');
t.end();
});
});

test('overrides querystring from opts', function (t) {
test('arguments - overrides querystring from opts', function (t) {
got(s.url + '/?test=doge', {query: {test: 'wow'}}, function (err, data) {
t.error(err);
t.equal(data, '/?test=wow');
t.ifError(err);
t.is(data, '/?test=wow');
t.end();
});
});

test('cleanup', function (t) {
test.after('arguments - cleanup', function (t) {
s.close();
t.end();
});
26 changes: 13 additions & 13 deletions test/test-error.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
var test = require('tap').test;
var test = require('ava');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();
Expand All @@ -9,45 +9,45 @@ s.on('/', function (req, res) {
res.end('not');
});

test('setup', function (t) {
test.before('error - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
});

test('error message', function (t) {
test('error - error message', function (t) {
got(s.url, function (err) {
t.ok(err);
t.equal(err.message, 'Response code 404 (Not Found)');
t.equal(err.host, 'localhost:6767');
t.equal(err.method, 'GET');
t.is(err.message, 'Response code 404 (Not Found)');
t.is(err.host, s.host + ':' + s.port);
t.is(err.method, 'GET');
t.end();
});
});

test('dns error message', function (t) {
test('error - dns error message', function (t) {
got('.com', function (err) {
t.ok(err);
t.ok(/getaddrinfo ENOTFOUND/.test(err.message));
t.equal(err.host, '.com');
t.equal(err.method, 'GET');
t.regexTest(/getaddrinfo ENOTFOUND/, err.message);
t.is(err.host, '.com');
t.is(err.method, 'GET');
t.end();
});
});

test('options.body error message', function (t) {
test('error - options.body error message', function (t) {
t.plan(2);
t.throws(function () {
got(s.url, {body: function () {}}, function () {});
}, /options.body must be a ReadableStream, string, Buffer or plain Object/);

got(s.url, {body: function () {}})
.catch(function (err) {
t.ok(/options.body must be a ReadableStream, string, Buffer or plain Object/.test(err.message));
t.regexTest(/options.body must be a ReadableStream, string, Buffer or plain Object/, err.message);
});
});

test('cleanup', function (t) {
test.after('error - cleanup', function (t) {
s.close();
t.end();
});
24 changes: 12 additions & 12 deletions test/test-gzip.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
var zlib = require('zlib');
var test = require('tap').test;
var test = require('ava');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();
Expand All @@ -22,39 +22,39 @@ s.on('/corrupted', function (req, res) {
res.end('Not gzipped content');
});

test('setup', function (t) {
test.before('gzip - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
});

test('ungzip content', function (t) {
test('gzip - ungzip content', function (t) {
got(s.url, function (err, data) {
t.error(err);
t.equal(data, testContent);
t.ifError(err);
t.is(data, testContent);
t.end();
});
});

test('ungzip error', function (t) {
test('gzip - ungzip error', function (t) {
got(s.url + '/corrupted', function (err) {
t.ok(err);
t.equal(err.message, 'incorrect header check');
t.equal(err.path, '/corrupted');
t.equal(err.name, 'ReadError');
t.is(err.message, 'incorrect header check');
t.is(err.path, '/corrupted');
t.is(err.name, 'ReadError');
t.end();
});
});

test('preserve headers property', function (t) {
test('gzip - preserve headers property', function (t) {
got(s.url, function (err, data, res) {
t.error(err);
t.ifError(err);
t.ok(res.headers);
t.end();
});
});

test('cleanup', function (t) {
test.after('gzip - cleanup', function (t) {
s.close();
t.end();
});
36 changes: 18 additions & 18 deletions test/test-headers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
var test = require('tap').test;
var test = require('ava');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();
Expand All @@ -8,65 +8,65 @@ s.on('/', function (req, res) {
res.end(JSON.stringify(req.headers));
});

test('setup', function (t) {
test.before('headers - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
});

test('send user-agent header by default', function (t) {
test('headers - send user-agent header by default', function (t) {
got(s.url, function (err, data) {
t.error(err);
t.ifError(err);

var headers = JSON.parse(data);

t.equal(headers['user-agent'], 'https://github.com/sindresorhus/got');
t.is(headers['user-agent'], 'https://github.com/sindresorhus/got');
t.end();
});
});

test('send accept-encoding header by default', function (t) {
test('headers - send accept-encoding header by default', function (t) {
got(s.url, function (err, data) {
t.error(err);
t.ifError(err);

var headers = JSON.parse(data);

t.equal(headers['accept-encoding'], 'gzip,deflate');
t.is(headers['accept-encoding'], 'gzip,deflate');
t.end();
});
});

test('send accept header with json option', function (t) {
test('headers - send accept header with json option', function (t) {
got(s.url, {json: true}, function (err, headers) {
t.error(err);
t.equal(headers.accept, 'application/json');
t.ifError(err);
t.is(headers.accept, 'application/json');
t.end();
});
});

test('send host header by default', function (t) {
test('headers - send host header by default', function (t) {
got(s.url, function (err, data) {
t.error(err);
t.ifError(err);

var headers = JSON.parse(data);

t.equal(headers.host, 'localhost:' + s.port);
t.is(headers.host, 'localhost:' + s.port);
t.end();
});
});

test('transform headers names to lowercase', function (t) {
test('headers - transform headers names to lowercase', function (t) {
got(s.url, {headers: {'USER-AGENT': 'test'}}, function (err, data) {
t.error(err);
t.ifError(err);

var headers = JSON.parse(data);

t.equal(headers['user-agent'], 'test');
t.is(headers['user-agent'], 'test');
t.end();
});
});

test('cleanup', function (t) {
test.after('headers - cleanup', function (t) {
s.close();
t.end();
});
18 changes: 9 additions & 9 deletions test/test-helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
var test = require('tap').test;
var test = require('ava');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();
Expand All @@ -13,31 +13,31 @@ s.on('/404', function (req, res) {
res.end('not found');
});

test('setup', function (t) {
test.before('helpers - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
});

test('callback mode', function (t) {
test('helpers - callback mode', function (t) {
got.get(s.url, function (err, data) {
t.error(err);
t.equal(data, 'ok');
t.ifError(err);
t.is(data, 'ok');
t.end();
});
});

test('promise mode', function (t) {
test('helpers - promise mode', function (t) {
t.plan(3);

got.get(s.url)
.then(function (res) {
t.equal(res.body, 'ok');
t.is(res.body, 'ok');
});

got.get(s.url + '/404')
.catch(function (err) {
t.equal(err.response.body, 'not found');
t.is(err.response.body, 'not found');
});

got.get('.com')
Expand All @@ -46,7 +46,7 @@ test('promise mode', function (t) {
});
});

test('cleanup', function (t) {
test.after('helpers - cleanup', function (t) {
s.close();
t.end();
});
Loading