Browse Source

Rewrite tests with async/await 💖

Closes #115
http2
Vsevolod Strukchinsky 9 years ago
parent
commit
f7500d474e
  1. 46
      test/arguments.js
  2. 37
      test/error.js
  3. 27
      test/gzip.js
  4. 57
      test/headers.js
  5. 30
      test/helpers.js
  6. 71
      test/http.js
  7. 27
      test/https.js
  8. 53
      test/json.js
  9. 118
      test/post.js
  10. 56
      test/redirects.js
  11. 20
      test/retry.js
  12. 19
      test/unix-socket.js

46
test/arguments.js

@ -21,42 +21,30 @@ test.before('arguments - setup', t => {
s.listen(s.port, () => t.end());
});
test('arguments - url argument is required', t => {
t.plan(2);
t.throws(() => {
got(undefined, () => {});
}, /Parameter `url` must be a string or object, not undefined/);
got().catch(err => {
test('arguments - url argument is required', async t => {
try {
await got();
t.fail('Exception is not thrown');
} 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', t => {
got({
hostname: s.host,
port: s.port,
path: '/test'
}, (err, data) => {
t.ifError(err);
t.is(data, '/test');
t.end();
});
test('arguments - accepts url.parse object as first argument', async t => {
t.is((await got({hostname: s.host, port: s.port, path: '/test'})).body, '/test');
});
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('arguments - overrides querystring from opts', async t => {
t.is((await got(`${s.url}/?test=doge`, {query: {test: 'wow'}})).body, '/?test=wow');
});
test('arguments - should throw with auth in url', t => {
t.throws(() => {
got(`https://test:45d3ps453@account.myservice.com/api/token`, () => {});
}, /Basic authentication must be done with auth option/);
t.end();
test('arguments - should throw with auth in url', async t => {
try {
await got(`https://test:45d3ps453@account.myservice.com/api/token`);
t.fail('Exception is not thrown');
} catch (err) {
t.regexTest(/Basic authentication must be done with auth option/, err.message);
}
});
test.after('arguments - cleanup', t => {

37
test/error.js

@ -13,35 +13,44 @@ test.before('error - setup', t => {
s.listen(s.port, () => t.end());
});
test('error - error message', t => {
got(s.url, err => {
test('error - error message', async t => {
try {
await got(s.url);
t.fail('Exception was not thrown');
} catch (err) {
t.ok(err);
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('error - dns error message', t => {
got('.com', {retries: 0}, err => {
test('error - dns error message', async t => {
try {
await got('.com', {retries: 0});
t.fail('Exception was not thrown');
} catch (err) {
t.ok(err);
t.regexTest(/getaddrinfo ENOTFOUND/, err.message);
t.is(err.host, '.com');
t.is(err.method, 'GET');
t.end();
});
}
});
test('error - options.body error message', t => {
t.plan(2);
t.throws(() => {
test('error - options.body error message', async t => {
try {
got(s.url, {body: () => {}}, () => {});
}, /options.body must be a ReadableStream, string, Buffer or plain Object/);
t.fail('Exception was not thrown');
} catch (err) {
t.regexTest(/options.body must be a ReadableStream, string, Buffer or plain Object/, err.message);
}
got(s.url, {body: () => {}}).catch(err => {
try {
await got(s.url, {body: () => {}});
t.fail('Exception was not thrown');
} catch (err) {
t.regexTest(/options.body must be a ReadableStream, string, Buffer or plain Object/, err.message);
});
}
});
test.after('error - cleanup', t => {

27
test/gzip.js

@ -24,30 +24,23 @@ test.before('gzip - setup', t => {
s.listen(s.port, () => t.end());
});
test('gzip - ungzip content', t => {
got(s.url, (err, data) => {
t.ifError(err);
t.is(data, testContent);
t.end();
});
test('gzip - ungzip content', async t => {
t.is((await got(s.url)).body, testContent);
});
test('gzip - ungzip error', t => {
got(`${s.url}/corrupted`, err => {
t.ok(err);
test('gzip - ungzip error', async t => {
try {
await got(`${s.url}/corrupted`);
t.fail('Exception was not thrown');
} catch (err) {
t.is(err.message, 'incorrect header check');
t.is(err.path, '/corrupted');
t.is(err.name, 'ReadError');
t.end();
});
}
});
test('gzip - preserve headers property', t => {
got(s.url, (err, data, res) => {
t.ifError(err);
t.ok(res.headers);
t.end();
});
test('gzip - preserve headers property', async t => {
t.ok((await got(s.url)).headers);
});
test.after('gzip - cleanup', t => {

57
test/headers.js

@ -12,56 +12,29 @@ test.before('headers - setup', t => {
s.listen(s.port, () => t.end());
});
test('headers - send user-agent header by default', t => {
got(s.url, (err, data) => {
t.ifError(err);
const headers = JSON.parse(data);
t.is(headers['user-agent'], 'https://github.com/sindresorhus/got');
t.end();
});
test('headers - send user-agent header by default', async t => {
const headers = (await got(s.url, {json: true})).body;
t.is(headers['user-agent'], 'https://github.com/sindresorhus/got');
});
test('headers - send accept-encoding header by default', t => {
got(s.url, (err, data) => {
t.ifError(err);
const headers = JSON.parse(data);
t.is(headers['accept-encoding'], 'gzip,deflate');
t.end();
});
test('headers - send accept-encoding header by default', async t => {
const headers = (await got(s.url, {json: true})).body;
t.is(headers['accept-encoding'], 'gzip,deflate');
});
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 accept header with json option', async t => {
const headers = (await got(s.url, {json: true})).body;
t.is(headers.accept, 'application/json');
});
test('headers - send host header by default', t => {
got(s.url, (err, data) => {
t.ifError(err);
const headers = JSON.parse(data);
t.is(headers.host, `localhost:${s.port}`);
t.end();
});
test('headers - send host header by default', async t => {
const headers = (await got(s.url, {json: true})).body;
t.is(headers.host, `localhost:${s.port}`);
});
test('headers - transform headers names to lowercase', t => {
got(s.url, {headers: {'USER-AGENT': 'test'}}, (err, data) => {
t.ifError(err);
const headers = JSON.parse(data);
t.is(headers['user-agent'], 'test');
t.end();
});
test('headers - transform headers names to lowercase', async t => {
const headers = (await got(s.url, {headers: {'USER-AGENT': 'test'}, json: true})).body;
t.is(headers['user-agent'], 'test');
});
test.after('headers - cleanup', t => {

30
test/helpers.js

@ -17,28 +17,26 @@ test.before('helpers - setup', t => {
s.listen(s.port, () => t.end());
});
test('helpers - callback mode', t => {
got.get(s.url, (err, data) => {
t.ifError(err);
t.is(data, 'ok');
t.end();
});
test('helpers - callback mode', async t => {
t.is((await got.get(s.url)).body, 'ok');
});
test('helpers - promise mode', t => {
t.plan(3);
test('helpers - promise mode', async t => {
t.is((await got.get(s.url)).body, 'ok');
got.get(s.url).then(res => {
t.is(res.body, 'ok');
});
got.get(`${s.url}/404`).catch(err => {
try {
await got.get(`${s.url}/404`);
t.fail('Exception is not thrown');
} catch (err) {
t.is(err.response.body, 'not found');
});
}
got.get('.com', {retries: 0}).catch(err => {
try {
await got.get('.com', {retries: 0});
t.fail('Exception is not thrown');
} catch (err) {
t.ok(err);
});
}
});
test.after('helpers - cleanup', t => {

71
test/http.js

@ -27,66 +27,45 @@ test.before('http - setup', t => {
s.listen(s.port, () => t.end());
});
test('http - callback mode', t => {
got(s.url, (err, data) => {
t.ifError(err);
t.is(data, 'ok');
t.end();
});
test('http - simple request', async t => {
t.is((await got(s.url)).body, 'ok');
});
test('http - protocol-less URLs', t => {
got(s.url.replace(/^http:\/\//, ''), (err, data) => {
t.ifError(err);
t.is(data, 'ok');
t.end();
});
test('http - protocol-less URLs', async t => {
t.is((await got(s.url.replace(/^http:\/\//, ''))).body, 'ok');
});
test('http - empty response', t => {
got(`${s.url}/empty`, (err, data) => {
t.ifError(err);
t.is(data, '');
t.end();
});
test('http - empty response', async t => {
t.is((await got(`${s.url}/empty`)).body, '');
});
test('http - error with code', t => {
got(`${s.url}/404`, (err, data) => {
t.ok(err);
test('http - error with code', async t => {
try {
await got(`${s.url}/404`);
t.fail('Exception was not thrown');
} catch (err) {
t.is(err.statusCode, 404);
t.is(data, 'not');
t.end();
});
t.is(err.response.body, 'not');
}
});
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 - buffer on encoding === null', async t => {
const data = (await got(s.url, {encoding: null})).body;
t.ok(Buffer.isBuffer(data));
});
test('http - timeout option', t => {
got(`${s.url}/404`, {timeout: 1, retries: 0}, err => {
test('http - timeout option', async t => {
try {
await got(`${s.url}/404`, {timeout: 1, retries: 0});
t.fail('Exception was not thrown');
} catch (err) {
t.is(err.code, 'ETIMEDOUT');
t.end();
});
}
});
test('http - query option', t => {
t.plan(4);
got(s.url, {query: {recent: true}}, (err, data) => {
t.ifError(err);
t.is(data, 'recent');
});
got(s.url, {query: 'recent=true'}, (err, data) => {
t.ifError(err);
t.is(data, 'recent');
});
test('http - query option', async t => {
t.is((await got(s.url, {query: {recent: true}})).body, 'recent');
t.is((await got(s.url, {query: 'recent=true'})).body, 'recent');
});
test.after('http - cleanup', t => {

27
test/https.js

@ -47,34 +47,21 @@ test.before('https - setup', t => {
s.listen(s.port, () => t.end());
});
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 - redirects from http to https works', async t => {
t.ok((await got('http://github.com')).body);
});
test('https - make request to https server', t => {
got('https://google.com', {
strictSSL: true
}, (err, data) => {
t.ifError(err);
t.ok(data);
t.end();
});
test('https - make request to https server', async t => {
t.ok((await got('https://google.com', {strictSSL: true})).body);
});
test('https - make request to https server with ca', t => {
got(s.url, {
test('https - make request to https server with ca', async t => {
const {body} = await got(s.url, {
strictSSL: true,
ca: caRootCert,
headers: {host: 'sindresorhus.com'}
}, (err, data) => {
t.ifError(err);
t.is(data, 'ok');
t.end();
});
t.is(body, 'ok');
});
test.after('https - cleanup', t => {

53
test/json.js

@ -31,47 +31,44 @@ test.before('json - setup', t => {
s.listen(s.port, () => t.end());
});
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 parse response', async t => {
t.same((await got(s.url, {json: true})).body, {data: 'dog'});
});
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 should not parse responses without a body', async t => {
const {body} = await got(`${s.url}/204`, {json: true});
t.is(body, '');
});
test('json - json option wrap parsing errors', t => {
got(`${s.url}/invalid`, {json: true}, err => {
t.ok(err);
test('json - json option wrap parsing errors', async t => {
try {
await got(`${s.url}/invalid`, {json: true});
t.fail('Exception was not thrown');
} catch (err) {
t.regexTest(/Unexpected token/, err.message);
t.ok(err.message.indexOf(err.hostname) !== -1, err.message);
t.is(err.path, '/invalid');
t.end();
});
}
});
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 parse non-200 responses', async t => {
try {
await got(`${s.url}/non200`, {json: true});
t.fail('Exception was not thrown');
} catch (err) {
t.same(err.response.body, {data: 'dog'});
}
});
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);
test('json - json option should catch errors on invalid non-200 responses', async t => {
try {
await got(`${s.url}/non200-invalid`, {json: true});
t.fail('Exception was not thrown');
} catch (err) {
t.regexTest(/Unexpected token/, err.message);
t.is(json, 'Internal error');
t.is(err.response.body, 'Internal error');
t.is(err.path, '/non200-invalid');
t.end();
});
}
});
test.after('json - cleanup', t => {

118
test/post.js

@ -1,3 +1,5 @@
/**/
import test from 'ava';
import intoStream from 'into-stream';
import got from '../';
@ -22,106 +24,80 @@ test.before('post - setup', t => {
s.listen(s.port, () => t.end());
});
test('post - GET can have body', t => {
t.plan(3);
got.get(s.url, {body: 'hi'}, (err, data, res) => {
t.ifError(err);
t.is(data, 'hi');
t.is(res.headers.method, 'GET');
});
test('post - GET can have body', async t => {
const {body, headers} = await got.get(s.url, {body: 'hi'});
t.is(body, 'hi');
t.is(headers.method, 'GET');
});
test('post - send data from options with post request', t => {
t.plan(6);
got(s.url, {body: 'wow'}, (err, data) => {
t.ifError(err);
t.is(data, 'wow');
});
got(s.url, {body: new Buffer('wow')}, (err, data) => {
t.ifError(err);
t.is(data, 'wow');
});
test('post - send data from options with post request', async t => {
const {body} = await got(s.url, {body: 'wow'});
t.is(body, 'wow');
});
got(s.url, {body: intoStream(['wow'])}, (err, data) => {
t.ifError(err);
t.is(data, 'wow');
});
test('post - send data from options with post request', async t => {
const {body} = await got(s.url, {body: new Buffer('wow')});
t.is(body, 'wow');
});
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 - send data from options with post request', async t => {
const {body} = await got(s.url, {body: intoStream(['wow'])});
t.is(body, 'wow');
});
test('post - post have content-length header to string', t => {
t.plan(10);
test('post - works with empty post response', async t => {
const {body} = await got(`${s.url}/empty`, {body: 'wow'});
t.is(body, '');
});
got(`${s.url}/headers`, {
body: 'wow',
json: true
}, (err, headers) => {
t.ifError(err);
t.is(headers['content-length'], '3');
});
test('post - post have content-length header to string', async t => {
const {body} = await got(`${s.url}/headers`, {body: 'wow', json: true});
t.is(body['content-length'], '3');
});
got(`${s.url}/headers`, {
body: new Buffer('wow'),
json: true
}, (err, headers) => {
t.ifError(err);
t.is(headers['content-length'], '3');
});
test('post - post have content-length header to string', async t => {
const {body} = await got(`${s.url}/headers`, {body: new Buffer('wow'), json: true});
t.is(body['content-length'], '3');
});
got(`${s.url}/headers`, {
body: intoStream(['wow']),
json: true
}, (err, headers) => {
t.ifError(err);
t.is(headers['content-length'], undefined);
});
test('post - post have content-length header to string', async t => {
const {body} = await got(`${s.url}/headers`, {body: intoStream(['wow']), json: true});
t.is(body['content-length'], undefined);
});
got(`${s.url}/headers`, {
test('post - post have content-length header to string', async t => {
const {body} = await got(`${s.url}/headers`, {
body: 'wow',
json: true,
headers: {
'content-length': '10'
}
}, (err, headers) => {
t.ifError(err);
t.is(headers['content-length'], '10');
});
t.is(body['content-length'], '10');
});
got(`${s.url}/headers`, {
test('post - post have content-length header to string', async t => {
const {body} = await 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);
});
t.is(body['content-length'], undefined);
});
test('post - works with plain object in body', t => {
t.plan(4);
got(s.url, {
test('post - works with plain object in body', async t => {
const {body} = await got(s.url, {
body: {
such: 'wow'
}
}, (err, data) => {
t.ifError(err);
t.is(data, 'such=wow');
});
t.is(body, 'such=wow');
});
got(`${s.url}/headers`, {
test('post - works with plain object in body', async t => {
const {body} = await got(`${s.url}/headers`, {
headers: {
'content-type': 'doge'
},
@ -129,10 +105,8 @@ test('post - works with plain object in body', t => {
such: 'wow'
},
json: true
}, (err, headers) => {
t.ifError(err);
t.is(headers['content-type'], 'doge');
});
t.is(body['content-type'], 'doge');
});
test.after('post - cleanup', t => {

56
test/redirects.js

@ -40,56 +40,40 @@ test.before('redirects - setup', t => {
s.listen(s.port, () => t.end());
});
test('redirects - follows redirect', t => {
got(`${s.url}/finite`, (err, data) => {
t.ifError(err);
t.is(data, 'reached');
t.end();
});
test('redirects - follows redirect', async t => {
t.is((await got(`${s.url}/finite`)).body, 'reached');
});
test('redirects - follows relative redirect', t => {
got(`${s.url}/relative`, (err, data) => {
t.ifError(err);
t.is(data, 'reached');
t.end();
});
test('redirects - follows relative redirect', async t => {
t.is((await got(`${s.url}/relative`)).body, 'reached');
});
test('redirects - throws on endless redirect', t => {
got(`${s.url}/endless`, err => {
t.ok(err, 'should get error');
test('redirects - throws on endless redirect', async t => {
try {
await got(`${s.url}/endless`);
t.fail('Exception was not thrown');
} catch (err) {
t.is(err.message, 'Redirected 10 times. Aborting.');
t.end();
});
}
});
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 - query in options are not breaking redirects', async t => {
t.is((await got(`${s.url}/relativeQuery`, {query: 'bang'})).body, 'reached');
});
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 - hostname+path in options are not breaking redirects', async t => {
t.is((await got(`${s.url}/relative`, {hostname: s.host, path: '/relative'})).body, 'reached');
});
test('redirects - redirect only GET and HEAD requests', t => {
got(`${s.url}/relative`, {body: 'wow'}, err => {
test('redirects - redirect only GET and HEAD requests', async t => {
try {
await got(`${s.url}/relative`, {body: 'wow'});
t.fail('Exception was not thrown');
} catch (err) {
t.is(err.message, 'Response code 302 (Moved Temporarily)');
t.is(err.path, '/relative');
t.is(err.statusCode, 302);
t.end();
});
}
});
test.after('redirects - cleanup', t => {

20
test/retry.js

@ -22,19 +22,17 @@ test.before('retry - setup', t => {
s.listen(s.port, () => t.end());
});
test('retry - timeout errors', t => {
got(`${s.url}/knock-twice`, {timeout: 1000}, (err, data) => {
t.ifError(err);
t.is(data, 'who`s there?');
t.end();
});
test('retry - timeout errors', async t => {
t.is((await got(`${s.url}/knock-twice`, {timeout: 1000})).body, 'who`s there?');
});
test('retry - can be disabled with option', t => {
got(`${s.url}/try-me`, {timeout: 1000, retries: 0}, () => {
t.is(trys, 1);
t.end();
});
test('retry - can be disabled with option', async t => {
try {
await got(`${s.url}/try-me`, {timeout: 1000, retries: 0});
} catch (err) {
t.ok(err);
}
t.is(trys, 1);
});
test.after('error - cleanup', t => {

19
test/unix-socket.js

@ -15,25 +15,14 @@ test.before('unix-socket - setup', t => {
s.listen(socketPath, () => t.end());
});
test('unix-socket - request via unix socket', t => {
// borrow unix domain socket url format from request module
test('unix-socket - request via unix socket', async t => {
const url = format('http://unix:%s:%s', socketPath, '/');
got(url, (err, data) => {
t.ifError(err);
t.is(data, 'ok');
t.end();
});
t.is((await got(url)).body, 'ok');
});
test('unix-socket - protocol-less request', t => {
test('unix-socket - protocol-less request', async t => {
const url = format('unix:%s:%s', socketPath, '/');
got(url, (err, data) => {
t.ifError(err);
t.is(data, 'ok');
t.end();
});
t.is((await got(url)).body, 'ok');
});
test.after('unix-socket - cleanup', t => {

Loading…
Cancel
Save