mirror of https://github.com/lukechilds/got.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.7 KiB
78 lines
1.7 KiB
import test from 'ava';
|
|
import got from '../';
|
|
import {createServer} from './helpers/server';
|
|
|
|
let s;
|
|
|
|
test.before('setup', async () => {
|
|
s = await createServer();
|
|
|
|
s.on('/', (req, res) => {
|
|
res.end('{"data":"dog"}');
|
|
});
|
|
|
|
s.on('/invalid', (req, res) => {
|
|
res.end('/');
|
|
});
|
|
|
|
s.on('/204', (req, res) => {
|
|
res.statusCode = 204;
|
|
res.end();
|
|
});
|
|
|
|
s.on('/non200', (req, res) => {
|
|
res.statusCode = 500;
|
|
res.end('{"data":"dog"}');
|
|
});
|
|
|
|
s.on('/non200-invalid', (req, res) => {
|
|
res.statusCode = 500;
|
|
res.end('Internal error');
|
|
});
|
|
|
|
await s.listen(s.port);
|
|
});
|
|
|
|
test('parses response', async t => {
|
|
t.same((await got(s.url, {json: true})).body, {data: 'dog'});
|
|
});
|
|
|
|
test('not parses responses without a body', async t => {
|
|
const {body} = await got(`${s.url}/204`, {json: true});
|
|
t.is(body, '');
|
|
});
|
|
|
|
test('wraps 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');
|
|
}
|
|
});
|
|
|
|
test('parses 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('catches 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(err.response.body, 'Internal error');
|
|
t.is(err.path, '/non200-invalid');
|
|
}
|
|
});
|
|
|
|
test.after('cleanup', async () => {
|
|
await s.close();
|
|
});
|
|
|