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.

126 lines
2.4 KiB

import test from 'ava';
import got from '../';
import intoStream from 'into-stream';
import {createServer} from './_server';
let s;
test.before('setup', async t => {
s = await createServer();
s.on('/', (req, res) => {
res.end('ok');
});
s.on('/post', (req, res) => {
req.pipe(res);
});
s.on('/redirect', (req, res) => {
res.writeHead(302, {
location: s.url
});
res.end();
});
s.on('/error', (req, res) => {
res.statusCode = 404;
res.end();
});
await s.listen(s.port);
});
test('option.json can not be used', t => {
t.throws(() => {
got.stream(s.url, {json: true});
}, 'got can not be used as stream when options.json is used');
});
test.cb('returns readable stream', t => {
got.stream(s.url)
.on('data', data => {
t.is(data.toString(), 'ok');
t.end();
});
});
test.cb('returns writeable stream', t => {
got.stream.post(`${s.url}/post`)
.on('data', data => {
t.is(data.toString(), 'wow');
t.end();
})
.end('wow');
});
test.cb('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');
// wait for request to end
setTimeout(t.end, 10);
});
test.cb('have request event', t => {
got.stream(s.url)
.on('request', req => {
t.ok(req);
t.end();
});
});
test.cb('have redirect event', t => {
got.stream(`${s.url}/redirect`)
.on('redirect', res => {
t.is(res.headers.location, s.url);
t.end();
});
});
test.cb('have response event', t => {
got.stream(s.url)
.on('response', res => {
t.is(res.statusCode, 200);
t.end();
});
});
test.cb('have error event', t => {
9 years ago
got.stream(`${s.url}/error`, {retries: 0})
.on('response', () => {
t.fail('response event should not be emitted');
})
.on('error', (err, data, res) => {
t.is(err.message, 'Response code 404 (Not Found)');
t.is(null, data);
t.ok(res);
t.end();
});
});
test.cb('have error event', t => {
9 years ago
got.stream('.com', {retries: 0})
.on('response', () => {
t.fail('response event should not be emitted');
})
.on('error', err => {
t.regexTest(/getaddrinfo ENOTFOUND/, err.message);
t.end();
});
});
test.cb('accepts option.body as Stream', t => {
got.stream(`${s.url}/post`, {body: intoStream(['wow'])})
.on('data', chunk => {
t.is(chunk.toString(), 'wow');
t.end();
});
});
test.after('cleanup', async t => {
await s.close();
});