Browse Source

move from tap to ava

http2
Vsevolod Strukchinsky 9 years ago
parent
commit
f57a5e89f8
  1. 2
      .travis.yml
  2. 5
      package.json
  3. 4
      test/server.js
  4. 22
      test/test-arguments.js
  5. 26
      test/test-error.js
  6. 24
      test/test-gzip.js
  7. 36
      test/test-headers.js
  8. 18
      test/test-helpers.js
  9. 48
      test/test-http.js
  10. 28
      test/test-https.js
  11. 34
      test/test-json.js
  12. 80
      test/test-post.js
  13. 42
      test/test-redirects.js
  14. 38
      test/test-stream.js
  15. 18
      test/test-unix-socket.js

2
.travis.yml

@ -1,6 +1,6 @@
sudo: false
language: node_js
node_js:
- 'iojs'
- 'stable'
- '0.12'
- '0.10'

5
package.json

@ -20,7 +20,7 @@
"node": ">=0.10.0"
},
"scripts": {
"test": "xo && tap test/test-*.js"
"test": "xo && ava"
},
"files": [
"index.js"
@ -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": "*"
}

4
test/server.js

@ -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);
});
@ -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);
});

22
test/test-arguments.js

@ -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();
@ -17,13 +17,13 @@ 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 () {});
@ -31,27 +31,27 @@ test('url argument is required', function (t) {
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);
});
});
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
test/test-error.js

@ -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();
@ -9,33 +9,33 @@ 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 () {});
@ -43,11 +43,11 @@ test('options.body error message', function (t) {
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
test/test-gzip.js

@ -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();
@ -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
test/test-headers.js

@ -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();
@ -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
test/test-helpers.js

@ -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();
@ -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')
@ -46,7 +46,7 @@ test('promise mode', function (t) {
});
});
test('cleanup', function (t) {
test.after('helpers - cleanup', function (t) {
s.close();
t.end();
});

48
test/test-http.js

@ -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();
@ -23,75 +23,75 @@ s.on('/?recent=true', function (req, res) {
res.end('recent');
});
test('setup', function (t) {
test.before('http - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
});
test('callback mode', function (t) {
test('http - callback mode', function (t) {
got(s.url, function (err, data) {
t.error(err);
t.equal(data, 'ok');
t.ifError(err);
t.is(data, 'ok');
t.end();
});
});
test('protocol-less URLs', function (t) {
test('http - protocol-less URLs', function (t) {
got(s.url.replace(/^http:\/\//, ''), function (err, data) {
t.error(err);
t.equal(data, 'ok');
t.ifError(err);
t.is(data, 'ok');
t.end();
});
});
test('empty response', function (t) {
test('http - empty response', function (t) {
got(s.url + '/empty', function (err, data) {
t.error(err);
t.equal(data, '');
t.ifError(err);
t.is(data, '');
t.end();
});
});
test('error with code', function (t) {
test('http - error with code', function (t) {
got(s.url + '/404', function (err, data) {
t.ok(err);
t.equal(err.statusCode, 404);
t.equal(data, 'not');
t.is(err.statusCode, 404);
t.is(data, 'not');
t.end();
});
});
test('buffer on encoding === null', function (t) {
test('http - buffer on encoding === null', function (t) {
got(s.url, {encoding: null}, function (err, data) {
t.error(err);
t.ifError(err);
t.ok(Buffer.isBuffer(data));
t.end();
});
});
test('timeout option', function (t) {
test('http - timeout option', function (t) {
got(s.url + '/404', {timeout: 1}, function (err) {
t.equal(err.code, 'ETIMEDOUT');
t.is(err.code, 'ETIMEDOUT');
t.end();
});
});
test('query option', function (t) {
test('http - query option', function (t) {
t.plan(4);
got(s.url, {query: {recent: true}}, function (err, data) {
t.error(err);
t.equal(data, 'recent');
t.ifError(err);
t.is(data, 'recent');
});
got(s.url, {query: 'recent=true'}, function (err, data) {
t.error(err);
t.equal(data, 'recent');
t.ifError(err);
t.is(data, 'recent');
});
});
test('cleanup', function (t) {
test.after('http - cleanup', function (t) {
s.close();
t.end();
});

28
test/test-https.js

@ -1,5 +1,5 @@
'use strict';
var test = require('tap').test;
var test = require('ava');
var pem = require('pem');
var got = require('../');
var server = require('./server.js');
@ -10,19 +10,19 @@ var cert;
var caRootKey;
var caRootCert;
test('root pem', function (t) {
test.before('https - create root pem', function (t) {
pem.createCertificate({
days: 1,
selfSigned: true
}, function (err, keys) {
t.error(err);
t.ifError(err);
caRootKey = keys.serviceKey;
caRootCert = keys.certificate;
t.end();
});
});
test('pem', function (t) {
test.before('https - create pem', function (t) {
pem.createCertificate({
serviceCertificate: caRootCert,
serviceKey: caRootKey,
@ -35,14 +35,14 @@ test('pem', function (t) {
organizationUnit: '',
commonName: 'sindresorhus.com'
}, function (err, keys) {
t.error(err);
t.ifError(err);
key = keys.clientKey;
cert = keys.certificate;
t.end();
});
});
test('setup', function (t) {
test.before('https - setup', function (t) {
s = server.createSSLServer(server.portSSL + 1, {
key: key,
cert: cert
@ -57,37 +57,37 @@ test('setup', function (t) {
});
});
test('redirects from http to https works', function (t) {
test('https - redirects from http to https works', function (t) {
got('http://github.com', function (err, data) {
t.error(err);
t.ifError(err);
t.ok(data);
t.end();
});
});
test('make request to https server', function (t) {
test('https - make request to https server', function (t) {
got('https://google.com', {
strictSSL: true
}, function (err, data) {
t.error(err);
t.ifError(err);
t.ok(data);
t.end();
});
});
test('make request to https server with ca', function (t) {
test('https - make request to https server with ca', function (t) {
got(s.url, {
strictSSL: true,
ca: caRootCert,
headers: {host: 'sindresorhus.com'}
}, function (err, data) {
t.error(err);
t.equal(data, 'ok');
t.ifError(err);
t.is(data, 'ok');
t.end();
});
});
test('cleanup', function (t) {
test.after('https - cleanup', function (t) {
s.close();
t.end();
});

34
test/test-json.js

@ -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();
@ -27,56 +27,56 @@ s.on('/non200-invalid', function (req, res) {
res.end('Internal error');
});
test('setup', function (t) {
test.before('json - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
});
test('json option should parse response', function (t) {
test('json - json option should parse response', function (t) {
got(s.url, {json: true}, function (err, json) {
t.error(err);
t.deepEqual(json, {data: 'dog'});
t.ifError(err);
t.same(json, {data: 'dog'});
t.end();
});
});
test('json option should not parse responses without a body', function (t) {
test('json - json option should not parse responses without a body', function (t) {
got(s.url + '/204', {json: true}, function (err) {
t.error(err);
t.ifError(err);
t.end();
});
});
test('json option wrap parsing errors', function (t) {
test('json - json option wrap parsing errors', function (t) {
got(s.url + '/invalid', {json: true}, function (err) {
t.ok(err);
t.ok(/Unexpected token/.test(err.message), err.message);
t.regexTest(/Unexpected token/, err.message);
t.ok(err.message.indexOf(err.hostname) !== -1, err.message);
t.equal(err.path, '/invalid');
t.is(err.path, '/invalid');
t.end();
});
});
test('json option should parse non-200 responses', function (t) {
test('json - json option should parse non-200 responses', function (t) {
got(s.url + '/non200', {json: true}, function (err, json) {
t.ok(err);
t.deepEqual(json, {data: 'dog'});
t.same(json, {data: 'dog'});
t.end();
});
});
test('json option should catch errors on invalid non-200 responses', function (t) {
test('json - json option should catch errors on invalid non-200 responses', function (t) {
got(s.url + '/non200-invalid', {json: true}, function (err, json) {
t.ok(err);
t.ok(/Unexpected token/.test(err.message), err.message);
t.deepEqual(json, 'Internal error');
t.equal(err.path, '/non200-invalid');
t.regexTest(/Unexpected token/, err.message);
t.is(json, 'Internal error');
t.is(err.path, '/non200-invalid');
t.end();
});
});
test('cleanup', function (t) {
test.after('json - cleanup', function (t) {
s.close();
t.end();
});

80
test/test-post.js

@ -1,11 +1,12 @@
'use strict';
var test = require('tap').test;
var test = require('ava');
var intoStream = require('into-stream');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();
s.on('/', function (req, res) {
res.setHeader('method', req.method);
req.pipe(res);
});
@ -13,108 +14,97 @@ s.on('/headers', function (req, res) {
res.end(JSON.stringify(req.headers));
});
s.on('/method', function (req, res) {
res.setHeader('method', req.method);
res.end();
});
s.on('/empty', function (req, res) {
res.end();
});
test('setup', function (t) {
test.before('post - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
});
test('GET can have body', function (t) {
test('post - GET can have body', function (t) {
t.plan(3);
var stream = intoStream(['wow']);
stream.on('end', function () {
// ensure that stream was dumped
t.ok(true);
});
got.get(s.url + '/method', {body: stream}, function (err, data, res) {
t.error(err);
t.equal(res.headers.method, 'GET');
got.get(s.url, {body: 'hi'}, function (err, data, res) {
t.ifError(err);
t.is(data, 'hi');
t.is(res.headers.method, 'GET');
});
});
test('send data from options with post request', function (t) {
test('post - send data from options with post request', function (t) {
t.plan(6);
got(s.url, {body: 'wow'}, function (err, data) {
t.error(err);
t.equal(data, 'wow');
t.ifError(err);
t.is(data, 'wow');
});
got(s.url, {body: new Buffer('wow')}, function (err, data) {
t.error(err);
t.equal(data, 'wow');
t.ifError(err);
t.is(data, 'wow');
});
got(s.url, {body: intoStream(['wow'])}, function (err, data) {
t.error(err);
t.equal(data, 'wow');
t.ifError(err);
t.is(data, 'wow');
});
});
test('works with empty post response', function (t) {
test('post - works with empty post response', function (t) {
got(s.url + '/empty', {body: 'wow'}, function (err, data) {
t.error(err);
t.equal(data, '');
t.ifError(err);
t.is(data, '');
t.end();
});
});
test('post have content-length header to string', function (t) {
test('post - post have content-length header to string', function (t) {
t.plan(10);
got(s.url + '/headers', {body: 'wow', json: true}, function (err, headers) {
t.error(err);
t.equal(headers['content-length'], '3');
t.ifError(err);
t.is(headers['content-length'], '3');
});
got(s.url + '/headers', {body: new Buffer('wow'), json: true}, function (err, headers) {
t.error(err);
t.equal(headers['content-length'], '3');
t.ifError(err);
t.is(headers['content-length'], '3');
});
got(s.url + '/headers', {body: intoStream(['wow']), json: true}, function (err, headers) {
t.error(err);
t.equal(headers['content-length'], undefined);
t.ifError(err);
t.is(headers['content-length'], undefined);
});
got(s.url + '/headers', {body: 'wow', json: true, headers: {'content-length': '10'}}, function (err, headers) {
t.error(err);
t.equal(headers['content-length'], '10');
t.ifError(err);
t.is(headers['content-length'], '10');
});
got(s.url + '/headers', {body: '3\r\nwow\r\n0\r\n', json: true, headers: {'transfer-encoding': 'chunked'}}, function (err, headers) {
t.error(err);
t.equal(headers['content-length'], undefined);
t.ifError(err);
t.is(headers['content-length'], undefined);
});
});
test('works with plain object in body', function (t) {
test('post - works with plain object in body', function (t) {
t.plan(4);
got(s.url, {body: {such: 'wow'}}, function (err, data) {
t.error(err);
t.equal(data, 'such=wow');
t.ifError(err);
t.is(data, 'such=wow');
});
got(s.url + '/headers', {headers: {'content-type': 'doge'}, body: {such: 'wow'}, json: true}, function (err, headers) {
t.error(err);
t.equal(headers['content-type'], 'doge');
t.ifError(err);
t.is(headers['content-type'], 'doge');
});
});
test('cleanup', function (t) {
test.after('post - cleanup', function (t) {
s.close();
t.end();
});

42
test/test-redirects.js

@ -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();
@ -36,62 +36,62 @@ s.on('/relativeQuery?bang', function (req, res) {
res.end();
});
test('setup', function (t) {
test.before('redirects - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
});
test('follows redirect', function (t) {
test('redirects - follows redirect', function (t) {
got(s.url + '/finite', function (err, data) {
t.error(err);
t.equal(data, 'reached');
t.ifError(err);
t.is(data, 'reached');
t.end();
});
});
test('follows relative redirect', function (t) {
test('redirects - follows relative redirect', function (t) {
got(s.url + '/relative', function (err, data) {
t.error(err);
t.equal(data, 'reached');
t.ifError(err);
t.is(data, 'reached');
t.end();
});
});
test('throws on endless redirect', function (t) {
test('redirects - throws on endless redirect', function (t) {
got(s.url + '/endless', function (err) {
t.ok(err, 'should get error');
t.equal(err.message, 'Redirected 10 times. Aborting.');
t.is(err.message, 'Redirected 10 times. Aborting.');
t.end();
});
});
test('query in options are not breaking redirects', function (t) {
test('redirects - query in options are not breaking redirects', function (t) {
got(s.url + '/relativeQuery', {query: 'bang'}, function (err, data) {
t.error(err);
t.equal(data, 'reached');
t.ifError(err);
t.is(data, 'reached');
t.end();
});
});
test('hostname+path in options are not breaking redirects', function (t) {
test('redirects - hostname+path in options are not breaking redirects', function (t) {
got(s.url + '/relative', {hostname: s.host, path: '/relative'}, function (err, data) {
t.error(err);
t.equal(data, 'reached');
t.ifError(err);
t.is(data, 'reached');
t.end();
});
});
test('redirect only GET and HEAD requests', function (t) {
test('redirects - redirect only GET and HEAD requests', function (t) {
got(s.url + '/relative', {body: 'wow'}, function (err) {
t.equal(err.message, 'Response code 302 (Moved Temporarily)');
t.equal(err.path, '/relative');
t.equal(err.statusCode, 302);
t.is(err.message, 'Response code 302 (Moved Temporarily)');
t.is(err.path, '/relative');
t.is(err.statusCode, 302);
t.end();
});
});
test('cleanup', function (t) {
test.after('redirect - cleanup', function (t) {
s.close();
t.end();
});

38
test/test-stream.js

@ -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();
@ -24,20 +24,20 @@ s.on('/error', function (req, res) {
res.end();
});
test('setup', function (t) {
test.before('stream - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
});
test('json option can not be used in stream mode', function (t) {
test('stream - json option can not be used in stream mode', function (t) {
t.throws(function () {
got.stream(s.url, {json: true});
}, 'got can not be used as stream when options.json is used');
t.end();
});
test('callback can not be used in stream mode', function (t) {
test('stream - callback can not be used in stream mode', function (t) {
t.throws(function () {
got.stream(s.url, {json: true}, function () {});
}, 'callback can not be used in stream mode');
@ -49,24 +49,24 @@ test('callback can not be used in stream mode', function (t) {
t.end();
});
test('return readable stream', function (t) {
test('stream - return readable stream', function (t) {
got.stream(s.url)
.on('data', function (data) {
t.equal(data.toString(), 'ok');
t.is(data.toString(), 'ok');
t.end();
});
});
test('return writeable stream', function (t) {
test('stream - return writeable stream', function (t) {
t.plan(1);
got.stream.post(s.url + '/post')
.on('data', function (data) {
t.equal(data.toString(), 'wow');
t.is(data.toString(), 'wow');
})
.end('wow');
});
test('throws on write to stream with body specified', function (t) {
test('stream - throws on write to stream with body specified', function (t) {
t.throws(function () {
got.stream(s.url, {body: 'wow'}).write('wow');
}, 'got\'s stream is not writable when options.body is used');
@ -75,7 +75,7 @@ test('throws on write to stream with body specified', function (t) {
setTimeout(t.end.bind(t), 10);
});
test('request event', function (t) {
test('stream - request event', function (t) {
got.stream(s.url)
.on('request', function (req) {
t.ok(req);
@ -83,39 +83,39 @@ test('request event', function (t) {
});
});
test('redirect event', function (t) {
test('stream - redirect event', function (t) {
got.stream(s.url + '/redirect')
.on('redirect', function (res) {
t.equal(res.headers.location, s.url);
t.is(res.headers.location, s.url);
t.end();
});
});
test('response event', function (t) {
test('stream - response event', function (t) {
got.stream(s.url)
.on('response', function (res) {
t.equal(res.statusCode, 200);
t.is(res.statusCode, 200);
t.end();
});
});
test('error event', function (t) {
test('stream - error event', function (t) {
t.plan(4);
got.stream(s.url + '/error')
.on('error', function (err, data, res) {
t.equal(err.message, 'Response code 404 (Not Found)');
t.equal(null, data);
t.is(err.message, 'Response code 404 (Not Found)');
t.is(null, data);
t.ok(res);
});
got.stream('.com')
.on('error', function (err) {
t.ok(/getaddrinfo ENOTFOUND/.test(err.message));
t.regexTest(/getaddrinfo ENOTFOUND/, err.message);
});
});
test('cleanup', function (t) {
test.after('stream - cleanup', function (t) {
s.close();
t.end();
});

18
test/test-unix-socket.js

@ -1,7 +1,7 @@
'use strict';
var tempfile = require('tempfile');
var format = require('util').format;
var test = require('tap').test;
var test = require('ava');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();
@ -12,34 +12,34 @@ s.on('/', function (req, res) {
res.end('ok');
});
test('setup', function (t) {
test.before('unix-socket - setup', function (t) {
s.listen(socketPath, function () {
t.end();
});
});
test('request via unix socket', function (t) {
test('unix-socket - request via unix socket', function (t) {
// borrow unix domain socket url format from request module
var url = format('http://unix:%s:%s', socketPath, '/');
got(url, function (err, data) {
t.error(err);
t.equal(data, 'ok');
t.ifError(err);
t.is(data, 'ok');
t.end();
});
});
test('protocol-less request', function (t) {
test('unix-socket - protocol-less request', function (t) {
var url = format('unix:%s:%s', socketPath, '/');
got(url, function (err, data) {
t.error(err);
t.equal(data, 'ok');
t.ifError(err);
t.is(data, 'ok');
t.end();
});
});
test('cleanup', function (t) {
test.after('unix-socket - cleanup', function (t) {
s.close();
t.end();
});

Loading…
Cancel
Save