Browse Source

ES2015ify the tests

http2
Sindre Sorhus 10 years ago
parent
commit
a03201fb16
  1. 41
      test/server.js
  2. 49
      test/test-arguments.js
  3. 43
      test/test-error.js
  4. 44
      test/test-gzip.js
  5. 50
      test/test-headers.js
  6. 49
      test/test-helpers.js
  7. 58
      test/test-http.js
  8. 58
      test/test-https.js
  9. 48
      test/test-json.js
  10. 95
      test/test-post.js
  11. 59
      test/test-redirects.js
  12. 76
      test/test-stream.js
  13. 37
      test/test-unix-socket.js

41
test/server.js

@ -1,44 +1,31 @@
'use strict';
var http = require('http');
var https = require('https');
import http from 'http';
import https from 'https';
exports.host = 'localhost';
exports.port = 6767;
exports.portSSL = 16167;
export const host = 'localhost';
export let port = 6767;
export let portSSL = 16167;
exports.createServer = function (port) {
var host = exports.host;
export const createServer = port2 => {
port = port2 || ++port;
port = port || exports.port;
exports.port += 1;
var s = http.createServer(function (req, resp) {
s.emit(req.url, req, resp);
});
const s = http.createServer((req, resp) => s.emit(req.url, req, resp));
s.host = host;
s.port = port;
s.url = 'http://' + host + ':' + port;
s.url = `http://${host}:${port}`;
s.protocol = 'http';
return s;
};
exports.createSSLServer = function (port, opts) {
var host = exports.host;
port = port || exports.portSSL;
export const createSSLServer = (portSSL2, opts) => {
portSSL = portSSL2 || ++portSSL;
exports.portSSL += 1;
var s = https.createServer(opts, function (req, resp) {
s.emit(req.url, req, resp);
});
const s = https.createServer(opts, (req, resp) => s.emit(req.url, req, resp));
s.host = host;
s.port = port;
s.url = 'https://' + host + ':' + port;
s.port = portSSL;
s.url = `https://${host}:${portSSL}`;
s.protocol = 'https';
return s;

49
test/test-arguments.js

@ -1,57 +1,58 @@
'use strict';
var test = require('ava');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();
import test from 'ava';
import got from '../';
import {createServer} from './server.js';
s.on('/', function (req, res) {
const s = createServer();
s.on('/', (req, res) => {
res.statusCode = 404;
res.end();
});
s.on('/test', function (req, res) {
s.on('/test', (req, res) => {
res.end(req.url);
});
s.on('/?test=wow', function (req, res) {
s.on('/?test=wow', (req, res) => {
res.end(req.url);
});
test.before('arguments - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
test.before('arguments - setup', t => {
s.listen(s.port, () => t.end());
});
test('arguments - url argument is required', function (t) {
test('arguments - url argument is required', t => {
t.plan(2);
t.throws(function () {
got(undefined, function () {});
t.throws(() => {
got(undefined, () => {});
}, /Parameter `url` must be a string or object, not undefined/);
got()
.catch(function (err) {
t.regexTest(/Parameter `url` must be a string or object, not undefined/, err.message);
});
got().catch(err => {
t.regexTest(/Parameter `url` must be a string or object, not undefined/, err.message);
});
});
test('arguments - accepts url.parse object as first argument', function (t) {
got({hostname: s.host, port: s.port, path: '/test'}, function (err, data) {
test('arguments - accepts url.parse object as first argument', t => {
got({
hostname: s.host,
port: s.port,
path: '/test'
}, (err, data) => {
t.ifError(err);
t.is(data, '/test');
t.end();
});
});
test('arguments - overrides querystring from opts', function (t) {
got(s.url + '/?test=doge', {query: {test: 'wow'}}, function (err, data) {
test('arguments - overrides querystring from opts', t => {
got(`${s.url}/?test=doge`, {query: {test: 'wow'}}, (err, data) => {
t.ifError(err);
t.is(data, '/?test=wow');
t.end();
});
});
test.after('arguments - cleanup', function (t) {
test.after('arguments - cleanup', t => {
s.close();
t.end();
});

43
test/test-error.js

@ -1,32 +1,30 @@
'use strict';
var test = require('ava');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();
import test from 'ava';
import got from '../';
import {createServer} from './server.js';
s.on('/', function (req, res) {
const s = createServer();
s.on('/', (req, res) => {
res.statusCode = 404;
res.end('not');
});
test.before('error - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
test.before('error - setup', t => {
s.listen(s.port, () => t.end());
});
test('error - error message', function (t) {
got(s.url, function (err) {
test('error - error message', t => {
got(s.url, err => {
t.ok(err);
t.is(err.message, 'Response code 404 (Not Found)');
t.is(err.host, s.host + ':' + s.port);
t.is(err.host, `${s.host}:${s.port}`);
t.is(err.method, 'GET');
t.end();
});
});
test('error - dns error message', function (t) {
got('.com', function (err) {
test('error - dns error message', t => {
got('.com', err => {
t.ok(err);
t.regexTest(/getaddrinfo ENOTFOUND/, err.message);
t.is(err.host, '.com');
@ -35,19 +33,18 @@ test('error - dns error message', function (t) {
});
});
test('error - options.body error message', function (t) {
test('error - options.body error message', t => {
t.plan(2);
t.throws(function () {
got(s.url, {body: function () {}}, function () {});
t.throws(() => {
got(s.url, {body: () => {}}, () => {});
}, /options.body must be a ReadableStream, string, Buffer or plain Object/);
got(s.url, {body: function () {}})
.catch(function (err) {
t.regexTest(/options.body must be a ReadableStream, string, Buffer or plain Object/, err.message);
});
got(s.url, {body: () => {}}).catch(err => {
t.regexTest(/options.body must be a ReadableStream, string, Buffer or plain Object/, err.message);
});
});
test.after('error - cleanup', function (t) {
test.after('error - cleanup', t => {
s.close();
t.end();
});

44
test/test-gzip.js

@ -1,43 +1,39 @@
'use strict';
var zlib = require('zlib');
var test = require('ava');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();
var testContent = 'Compressible response content.\n';
s.on('/', function (req, res) {
import zlib from 'zlib';
import test from 'ava';
import got from '../';
import {createServer} from './server.js';
const s = createServer();
const testContent = 'Compressible response content.\n';
s.on('/', (req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Encoding', 'gzip');
zlib.gzip(testContent, function (_, data) {
res.end(data);
});
zlib.gzip(testContent, (_, data) => res.end(data));
});
s.on('/corrupted', function (req, res) {
s.on('/corrupted', (req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Encoding', 'gzip');
res.end('Not gzipped content');
});
test.before('gzip - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
test.before('gzip - setup', t => {
s.listen(s.port, () => t.end());
});
test('gzip - ungzip content', function (t) {
got(s.url, function (err, data) {
test('gzip - ungzip content', t => {
got(s.url, (err, data) => {
t.ifError(err);
t.is(data, testContent);
t.end();
});
});
test('gzip - ungzip error', function (t) {
got(s.url + '/corrupted', function (err) {
test('gzip - ungzip error', t => {
got(`${s.url}/corrupted`, err => {
t.ok(err);
t.is(err.message, 'incorrect header check');
t.is(err.path, '/corrupted');
@ -46,15 +42,15 @@ test('gzip - ungzip error', function (t) {
});
});
test('gzip - preserve headers property', function (t) {
got(s.url, function (err, data, res) {
test('gzip - preserve headers property', t => {
got(s.url, (err, data, res) => {
t.ifError(err);
t.ok(res.headers);
t.end();
});
});
test.after('gzip - cleanup', function (t) {
test.after('gzip - cleanup', t => {
s.close();
t.end();
});

50
test/test-headers.js

@ -1,72 +1,70 @@
'use strict';
var test = require('ava');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();
import test from 'ava';
import got from '../';
import {createServer} from './server.js';
s.on('/', function (req, res) {
const s = createServer();
s.on('/', (req, res) => {
res.end(JSON.stringify(req.headers));
});
test.before('headers - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
test.before('headers - setup', t => {
s.listen(s.port, () => t.end());
});
test('headers - send user-agent header by default', function (t) {
got(s.url, function (err, data) {
test('headers - send user-agent header by default', t => {
got(s.url, (err, data) => {
t.ifError(err);
var headers = JSON.parse(data);
const headers = JSON.parse(data);
t.is(headers['user-agent'], 'https://github.com/sindresorhus/got');
t.end();
});
});
test('headers - send accept-encoding header by default', function (t) {
got(s.url, function (err, data) {
test('headers - send accept-encoding header by default', t => {
got(s.url, (err, data) => {
t.ifError(err);
var headers = JSON.parse(data);
const headers = JSON.parse(data);
t.is(headers['accept-encoding'], 'gzip,deflate');
t.end();
});
});
test('headers - send accept header with json option', function (t) {
got(s.url, {json: true}, function (err, headers) {
test('headers - send accept header with json option', t => {
got(s.url, {json: true}, (err, headers) => {
t.ifError(err);
t.is(headers.accept, 'application/json');
t.end();
});
});
test('headers - send host header by default', function (t) {
got(s.url, function (err, data) {
test('headers - send host header by default', t => {
got(s.url, (err, data) => {
t.ifError(err);
var headers = JSON.parse(data);
const headers = JSON.parse(data);
t.is(headers.host, 'localhost:' + s.port);
t.is(headers.host, `localhost:${s.port}`);
t.end();
});
});
test('headers - transform headers names to lowercase', function (t) {
got(s.url, {headers: {'USER-AGENT': 'test'}}, function (err, data) {
test('headers - transform headers names to lowercase', t => {
got(s.url, {headers: {'USER-AGENT': 'test'}}, (err, data) => {
t.ifError(err);
var headers = JSON.parse(data);
const headers = JSON.parse(data);
t.is(headers['user-agent'], 'test');
t.end();
});
});
test.after('headers - cleanup', function (t) {
test.after('headers - cleanup', t => {
s.close();
t.end();
});

49
test/test-helpers.js

@ -1,52 +1,47 @@
'use strict';
var test = require('ava');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();
import test from 'ava';
import got from '../';
import {createServer} from './server.js';
s.on('/', function (req, res) {
const s = createServer();
s.on('/', (req, res) => {
res.end('ok');
});
s.on('/404', function (req, res) {
s.on('/404', (req, res) => {
res.statusCode = 404;
res.end('not found');
});
test.before('helpers - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
test.before('helpers - setup', t => {
s.listen(s.port, () => t.end());
});
test('helpers - callback mode', function (t) {
got.get(s.url, function (err, data) {
test('helpers - callback mode', t => {
got.get(s.url, (err, data) => {
t.ifError(err);
t.is(data, 'ok');
t.end();
});
});
test('helpers - promise mode', function (t) {
test('helpers - promise mode', t => {
t.plan(3);
got.get(s.url)
.then(function (res) {
t.is(res.body, 'ok');
});
got.get(s.url).then(res => {
t.is(res.body, 'ok');
});
got.get(s.url + '/404')
.catch(function (err) {
t.is(err.response.body, 'not found');
});
got.get(`${s.url}/404`).catch(err => {
t.is(err.response.body, 'not found');
});
got.get('.com')
.catch(function (err) {
t.ok(err);
});
got.get('.com').catch(err => {
t.ok(err);
});
});
test.after('helpers - cleanup', function (t) {
test.after('helpers - cleanup', t => {
s.close();
t.end();
});

58
test/test-http.js

@ -1,60 +1,58 @@
'use strict';
var test = require('ava');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();
import test from 'ava';
import got from '../';
import {createServer} from './server.js';
s.on('/', function (req, res) {
const s = createServer();
s.on('/', (req, res) => {
res.end('ok');
});
s.on('/empty', function (req, res) {
s.on('/empty', (req, res) => {
res.end();
});
s.on('/404', function (req, res) {
setTimeout(function () {
s.on('/404', (req, res) => {
setTimeout(() => {
res.statusCode = 404;
res.end('not');
}, 10);
});
s.on('/?recent=true', function (req, res) {
s.on('/?recent=true', (req, res) => {
res.end('recent');
});
test.before('http - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
test.before('http - setup', t => {
s.listen(s.port, () => t.end());
});
test('http - callback mode', function (t) {
got(s.url, function (err, data) {
test('http - callback mode', t => {
got(s.url, (err, data) => {
t.ifError(err);
t.is(data, 'ok');
t.end();
});
});
test('http - protocol-less URLs', function (t) {
got(s.url.replace(/^http:\/\//, ''), function (err, data) {
test('http - protocol-less URLs', t => {
got(s.url.replace(/^http:\/\//, ''), (err, data) => {
t.ifError(err);
t.is(data, 'ok');
t.end();
});
});
test('http - empty response', function (t) {
got(s.url + '/empty', function (err, data) {
test('http - empty response', t => {
got(`${s.url}/empty`, (err, data) => {
t.ifError(err);
t.is(data, '');
t.end();
});
});
test('http - error with code', function (t) {
got(s.url + '/404', function (err, data) {
test('http - error with code', t => {
got(`${s.url}/404`, (err, data) => {
t.ok(err);
t.is(err.statusCode, 404);
t.is(data, 'not');
@ -62,36 +60,36 @@ test('http - error with code', function (t) {
});
});
test('http - buffer on encoding === null', function (t) {
got(s.url, {encoding: null}, function (err, data) {
test('http - buffer on encoding === null', t => {
got(s.url, {encoding: null}, (err, data) => {
t.ifError(err);
t.ok(Buffer.isBuffer(data));
t.end();
});
});
test('http - timeout option', function (t) {
got(s.url + '/404', {timeout: 1}, function (err) {
test('http - timeout option', t => {
got(`${s.url}/404`, {timeout: 1}, err => {
t.is(err.code, 'ETIMEDOUT');
t.end();
});
});
test('http - query option', function (t) {
test('http - query option', t => {
t.plan(4);
got(s.url, {query: {recent: true}}, function (err, data) {
got(s.url, {query: {recent: true}}, (err, data) => {
t.ifError(err);
t.is(data, 'recent');
});
got(s.url, {query: 'recent=true'}, function (err, data) {
got(s.url, {query: 'recent=true'}, (err, data) => {
t.ifError(err);
t.is(data, 'recent');
});
});
test.after('http - cleanup', function (t) {
test.after('http - cleanup', t => {
s.close();
t.end();
});

58
test/test-https.js

@ -1,20 +1,19 @@
'use strict';
var test = require('ava');
var pem = require('pem');
var got = require('../');
var server = require('./server.js');
import test from 'ava';
import pem from 'pem';
import got from '../';
import {createSSLServer, portSSL} from './server.js';
var s;
var key;
var cert;
var caRootKey;
var caRootCert;
let s;
let key;
let cert;
let caRootKey;
let caRootCert;
test.before('https - create root pem', function (t) {
test.before('https - create root pem', t => {
pem.createCertificate({
days: 1,
selfSigned: true
}, function (err, keys) {
}, (err, keys) => {
t.ifError(err);
caRootKey = keys.serviceKey;
caRootCert = keys.certificate;
@ -22,7 +21,7 @@ test.before('https - create root pem', function (t) {
});
});
test.before('https - create pem', function (t) {
test.before('https - create pem', t => {
pem.createCertificate({
serviceCertificate: caRootCert,
serviceKey: caRootKey,
@ -34,7 +33,7 @@ test.before('https - create pem', function (t) {
organization: '',
organizationUnit: '',
commonName: 'sindresorhus.com'
}, function (err, keys) {
}, (err, keys) => {
t.ifError(err);
key = keys.clientKey;
cert = keys.certificate;
@ -42,52 +41,43 @@ test.before('https - create pem', function (t) {
});
});
test.before('https - setup', function (t) {
s = server.createSSLServer(server.portSSL + 1, {
key: key,
cert: cert
});
s.on('/', function (req, res) {
res.end('ok');
});
s.listen(s.port, function () {
t.end();
});
test.before('https - setup', t => {
s = createSSLServer(portSSL + 1, {key, cert});
s.on('/', (req, res) => res.end('ok'));
s.listen(s.port, () => t.end());
});
test('https - redirects from http to https works', function (t) {
got('http://github.com', function (err, data) {
test('https - redirects from http to https works', t => {
got('http://github.com', (err, data) => {
t.ifError(err);
t.ok(data);
t.end();
});
});
test('https - make request to https server', function (t) {
test('https - make request to https server', t => {
got('https://google.com', {
strictSSL: true
}, function (err, data) {
}, (err, data) => {
t.ifError(err);
t.ok(data);
t.end();
});
});
test('https - make request to https server with ca', function (t) {
test('https - make request to https server with ca', t => {
got(s.url, {
strictSSL: true,
ca: caRootCert,
headers: {host: 'sindresorhus.com'}
}, function (err, data) {
}, (err, data) => {
t.ifError(err);
t.is(data, 'ok');
t.end();
});
});
test.after('https - cleanup', function (t) {
test.after('https - cleanup', t => {
s.close();
t.end();
});

48
test/test-json.js

@ -1,55 +1,53 @@
'use strict';
var test = require('ava');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();
import test from 'ava';
import got from '../';
import {createServer} from './server.js';
s.on('/', function (req, res) {
const s = createServer();
s.on('/', (req, res) => {
res.end('{"data":"dog"}');
});
s.on('/invalid', function (req, res) {
s.on('/invalid', (req, res) => {
res.end('/');
});
s.on('/204', function (req, res) {
s.on('/204', (req, res) => {
res.statusCode = 204;
res.end();
});
s.on('/non200', function (req, res) {
s.on('/non200', (req, res) => {
res.statusCode = 500;
res.end('{"data":"dog"}');
});
s.on('/non200-invalid', function (req, res) {
s.on('/non200-invalid', (req, res) => {
res.statusCode = 500;
res.end('Internal error');
});
test.before('json - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
test.before('json - setup', t => {
s.listen(s.port, () => t.end());
});
test('json - json option should parse response', function (t) {
got(s.url, {json: true}, function (err, json) {
test('json - json option should parse response', t => {
got(s.url, {json: true}, (err, json) => {
t.ifError(err);
t.same(json, {data: 'dog'});
t.end();
});
});
test('json - json option should not parse responses without a body', function (t) {
got(s.url + '/204', {json: true}, function (err) {
test('json - json option should not parse responses without a body', t => {
got(`${s.url}/204`, {json: true}, err => {
t.ifError(err);
t.end();
});
});
test('json - json option wrap parsing errors', function (t) {
got(s.url + '/invalid', {json: true}, function (err) {
test('json - json option wrap parsing errors', t => {
got(`${s.url}/invalid`, {json: true}, err => {
t.ok(err);
t.regexTest(/Unexpected token/, err.message);
t.ok(err.message.indexOf(err.hostname) !== -1, err.message);
@ -58,16 +56,16 @@ test('json - json option wrap parsing errors', function (t) {
});
});
test('json - json option should parse non-200 responses', function (t) {
got(s.url + '/non200', {json: true}, function (err, json) {
test('json - json option should parse non-200 responses', t => {
got(`${s.url}/non200`, {json: true}, (err, json) => {
t.ok(err);
t.same(json, {data: 'dog'});
t.end();
});
});
test('json - json option should catch errors on invalid non-200 responses', function (t) {
got(s.url + '/non200-invalid', {json: true}, function (err, json) {
test('json - json option should catch errors on invalid non-200 responses', t => {
got(`${s.url}/non200-invalid`, {json: true}, (err, json) => {
t.ok(err);
t.regexTest(/Unexpected token/, err.message);
t.is(json, 'Internal error');
@ -76,7 +74,7 @@ test('json - json option should catch errors on invalid non-200 responses', func
});
});
test.after('json - cleanup', function (t) {
test.after('json - cleanup', t => {
s.close();
t.end();
});

95
test/test-post.js

@ -1,110 +1,141 @@
'use strict';
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) {
import test from 'ava';
import intoStream from 'into-stream';
import got from '../';
import {createServer} from './server.js';
const s = createServer();
s.on('/', (req, res) => {
res.setHeader('method', req.method);
req.pipe(res);
});
s.on('/headers', function (req, res) {
s.on('/headers', (req, res) => {
res.end(JSON.stringify(req.headers));
});
s.on('/empty', function (req, res) {
s.on('/empty', (req, res) => {
res.end();
});
test.before('post - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
test.before('post - setup', t => {
s.listen(s.port, () => t.end());
});
test('post - GET can have body', function (t) {
test('post - GET can have body', t => {
t.plan(3);
got.get(s.url, {body: 'hi'}, function (err, data, res) {
got.get(s.url, {body: 'hi'}, (err, data, res) => {
t.ifError(err);
t.is(data, 'hi');
t.is(res.headers.method, 'GET');
});
});
test('post - send data from options with post request', function (t) {
test('post - send data from options with post request', t => {
t.plan(6);
got(s.url, {body: 'wow'}, function (err, data) {
got(s.url, {body: 'wow'}, (err, data) => {
t.ifError(err);
t.is(data, 'wow');
});
got(s.url, {body: new Buffer('wow')}, function (err, data) {
got(s.url, {body: new Buffer('wow')}, (err, data) => {
t.ifError(err);
t.is(data, 'wow');
});
got(s.url, {body: intoStream(['wow'])}, function (err, data) {
got(s.url, {body: intoStream(['wow'])}, (err, data) => {
t.ifError(err);
t.is(data, 'wow');
});
});
test('post - works with empty post response', function (t) {
got(s.url + '/empty', {body: 'wow'}, function (err, data) {
test('post - works with empty post response', t => {
got(`${s.url}/empty`, {body: 'wow'}, (err, data) => {
t.ifError(err);
t.is(data, '');
t.end();
});
});
test('post - post have content-length header to string', function (t) {
test('post - post have content-length header to string', t => {
t.plan(10);
got(s.url + '/headers', {body: 'wow', json: true}, function (err, headers) {
got(`${s.url}/headers`, {
body: 'wow',
json: true
}, (err, headers) => {
t.ifError(err);
t.is(headers['content-length'], '3');
});
got(s.url + '/headers', {body: new Buffer('wow'), json: true}, function (err, headers) {
got(`${s.url}/headers`, {
body: new Buffer('wow'),
json: true
}, (err, headers) => {
t.ifError(err);
t.is(headers['content-length'], '3');
});
got(s.url + '/headers', {body: intoStream(['wow']), json: true}, function (err, headers) {
got(`${s.url}/headers`, {
body: intoStream(['wow']),
json: true
}, (err, headers) => {
t.ifError(err);
t.is(headers['content-length'], undefined);
});
got(s.url + '/headers', {body: 'wow', json: true, headers: {'content-length': '10'}}, function (err, headers) {
got(`${s.url}/headers`, {
body: 'wow',
json: true,
headers: {
'content-length': '10'
}
}, (err, headers) => {
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) {
got(`${s.url}/headers`, {
body: '3\r\nwow\r\n0\r\n',
json: true,
headers: {
'transfer-encoding': 'chunked'
}
}, (err, headers) => {
t.ifError(err);
t.is(headers['content-length'], undefined);
});
});
test('post - works with plain object in body', function (t) {
test('post - works with plain object in body', t => {
t.plan(4);
got(s.url, {body: {such: 'wow'}}, function (err, data) {
got(s.url, {
body: {
such: 'wow'
}
}, (err, data) => {
t.ifError(err);
t.is(data, 'such=wow');
});
got(s.url + '/headers', {headers: {'content-type': 'doge'}, body: {such: 'wow'}, json: true}, function (err, headers) {
got(`${s.url}/headers`, {
headers: {
'content-type': 'doge'
},
body: {
such: 'wow'
},
json: true
}, (err, headers) => {
t.ifError(err);
t.is(headers['content-type'], 'doge');
});
});
test.after('post - cleanup', function (t) {
test.after('post - cleanup', t => {
s.close();
t.end();
});

59
test/test-redirects.js

@ -1,89 +1,90 @@
'use strict';
var test = require('ava');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();
import test from 'ava';
import got from '../';
import {createServer} from './server.js';
s.on('/', function (req, res) {
const s = createServer();
s.on('/', (req, res) => {
res.end('reached');
});
s.on('/finite', function (req, res) {
s.on('/finite', (req, res) => {
res.writeHead(302, {
location: s.url + '/'
location: `${s.url}/`
});
res.end();
});
s.on('/endless', function (req, res) {
s.on('/endless', (req, res) => {
res.writeHead(302, {
location: s.url + '/endless'
location: `${s.url}/endless`
});
res.end();
});
s.on('/relative', function (req, res) {
s.on('/relative', (req, res) => {
res.writeHead(302, {
location: '/'
});
res.end();
});
s.on('/relativeQuery?bang', function (req, res) {
s.on('/relativeQuery?bang', (req, res) => {
res.writeHead(302, {
location: '/'
});
res.end();
});
test.before('redirects - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
test.before('redirects - setup', t => {
s.listen(s.port, () => t.end());
});
test('redirects - follows redirect', function (t) {
got(s.url + '/finite', function (err, data) {
test('redirects - follows redirect', t => {
got(`${s.url}/finite`, (err, data) => {
t.ifError(err);
t.is(data, 'reached');
t.end();
});
});
test('redirects - follows relative redirect', function (t) {
got(s.url + '/relative', function (err, data) {
test('redirects - follows relative redirect', t => {
got(`${s.url}/relative`, (err, data) => {
t.ifError(err);
t.is(data, 'reached');
t.end();
});
});
test('redirects - throws on endless redirect', function (t) {
got(s.url + '/endless', function (err) {
test('redirects - throws on endless redirect', t => {
got(`${s.url}/endless`, err => {
t.ok(err, 'should get error');
t.is(err.message, 'Redirected 10 times. Aborting.');
t.end();
});
});
test('redirects - query in options are not breaking redirects', function (t) {
got(s.url + '/relativeQuery', {query: 'bang'}, function (err, data) {
test('redirects - query in options are not breaking redirects', t => {
got(`${s.url}/relativeQuery`, {query: 'bang'}, (err, data) => {
t.ifError(err);
t.is(data, 'reached');
t.end();
});
});
test('redirects - hostname+path in options are not breaking redirects', function (t) {
got(s.url + '/relative', {hostname: s.host, path: '/relative'}, function (err, data) {
test('redirects - hostname+path in options are not breaking redirects', t => {
got(`${s.url}/relative`, {
hostname: s.host,
path: '/relative'
}, (err, data) => {
t.ifError(err);
t.is(data, 'reached');
t.end();
});
});
test('redirects - redirect only GET and HEAD requests', function (t) {
got(s.url + '/relative', {body: 'wow'}, function (err) {
test('redirects - redirect only GET and HEAD requests', t => {
got(`${s.url}/relative`, {body: 'wow'}, err => {
t.is(err.message, 'Response code 302 (Moved Temporarily)');
t.is(err.path, '/relative');
t.is(err.statusCode, 302);
@ -91,7 +92,7 @@ test('redirects - redirect only GET and HEAD requests', function (t) {
});
});
test.after('redirect - cleanup', function (t) {
test.after('redirect - cleanup', t => {
s.close();
t.end();
});

76
test/test-stream.js

@ -1,73 +1,71 @@
'use strict';
var test = require('ava');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();
import test from 'ava';
import got from '../';
import {createServer} from './server.js';
s.on('/', function (req, res) {
const s = createServer();
s.on('/', (req, res) => {
res.end('ok');
});
s.on('/post', function (req, res) {
s.on('/post', (req, res) => {
req.pipe(res);
});
s.on('/redirect', function (req, res) {
s.on('/redirect', (req, res) => {
res.writeHead(302, {
location: s.url
});
res.end();
});
s.on('/error', function (req, res) {
s.on('/error', (req, res) => {
res.statusCode = 404;
res.end();
});
test.before('stream - setup', function (t) {
s.listen(s.port, function () {
t.end();
});
test.before('stream - setup', t => {
s.listen(s.port, () => t.end());
});
test('stream - json option can not be used in stream mode', function (t) {
t.throws(function () {
test('stream - json option can not be used in stream mode', t => {
t.throws(() => {
got.stream(s.url, {json: true});
}, 'got can not be used as stream when options.json is used');
t.end();
});
test('stream - callback can not be used in stream mode', function (t) {
t.throws(function () {
got.stream(s.url, {json: true}, function () {});
test('stream - callback can not be used in stream mode', t => {
t.throws(() => {
got.stream(s.url, {json: true}, () => {});
}, 'callback can not be used in stream mode');
t.throws(function () {
got.stream(s.url, function () {});
t.throws(() => {
got.stream(s.url, () => {});
}, 'callback can not be used in stream mode');
t.end();
});
test('stream - return readable stream', function (t) {
test('stream - return readable stream', t => {
got.stream(s.url)
.on('data', function (data) {
.on('data', data => {
t.is(data.toString(), 'ok');
t.end();
});
});
test('stream - return writeable stream', function (t) {
test('stream - return writeable stream', t => {
t.plan(1);
got.stream.post(s.url + '/post')
.on('data', function (data) {
got.stream.post(`${s.url}/post`)
.on('data', data => {
t.is(data.toString(), 'wow');
})
.end('wow');
});
test('stream - throws on write to stream with body specified', function (t) {
t.throws(function () {
test('stream - throws on write to stream with body specified', t => {
t.throws(() => {
got.stream(s.url, {body: 'wow'}).write('wow');
}, 'got\'s stream is not writable when options.body is used');
@ -75,47 +73,47 @@ test('stream - throws on write to stream with body specified', function (t) {
setTimeout(t.end.bind(t), 10);
});
test('stream - request event', function (t) {
test('stream - request event', t => {
got.stream(s.url)
.on('request', function (req) {
.on('request', req => {
t.ok(req);
t.end();
});
});
test('stream - redirect event', function (t) {
got.stream(s.url + '/redirect')
.on('redirect', function (res) {
test('stream - redirect event', t => {
got.stream(`${s.url}/redirect`)
.on('redirect', res => {
t.is(res.headers.location, s.url);
t.end();
});
});
test('stream - response event', function (t) {
test('stream - response event', t => {
got.stream(s.url)
.on('response', function (res) {
.on('response', res => {
t.is(res.statusCode, 200);
t.end();
});
});
test('stream - error event', function (t) {
test('stream - error event', t => {
t.plan(4);
got.stream(s.url + '/error')
.on('error', function (err, data, res) {
got.stream(`${s.url}/error`)
.on('error', (err, data, res) => {
t.is(err.message, 'Response code 404 (Not Found)');
t.is(null, data);
t.ok(res);
});
got.stream('.com')
.on('error', function (err) {
.on('error', err => {
t.regexTest(/getaddrinfo ENOTFOUND/, err.message);
});
});
test.after('stream - cleanup', function (t) {
test.after('stream - cleanup', t => {
s.close();
t.end();
});

37
test/test-unix-socket.js

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

Loading…
Cancel
Save