|
|
@ -31,14 +31,14 @@ test.before('setup', async t => { |
|
|
|
await s.listen(s.port); |
|
|
|
}); |
|
|
|
|
|
|
|
test('option.json can not be used', t => { |
|
|
|
test.cb('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'); |
|
|
|
t.end(); |
|
|
|
}); |
|
|
|
|
|
|
|
test('callback can not be used', t => { |
|
|
|
test.cb('callback can not be used', t => { |
|
|
|
t.throws(() => { |
|
|
|
got.stream(s.url, {json: true}, () => {}); |
|
|
|
}, 'callback can not be used with stream mode'); |
|
|
@ -54,7 +54,7 @@ test('callback can not be used', t => { |
|
|
|
t.end(); |
|
|
|
}); |
|
|
|
|
|
|
|
test('returns readable stream', t => { |
|
|
|
test.cb('returns readable stream', t => { |
|
|
|
got.stream(s.url) |
|
|
|
.on('data', data => { |
|
|
|
t.is(data.toString(), 'ok'); |
|
|
@ -62,33 +62,33 @@ test('returns readable stream', t => { |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
test('returns writeable stream', t => { |
|
|
|
t.plan(1); |
|
|
|
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('throws on write to stream with body specified', t => { |
|
|
|
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.bind(t), 10); |
|
|
|
setTimeout(() => t.end(), 10); |
|
|
|
}); |
|
|
|
|
|
|
|
test('have request event', t => { |
|
|
|
test.cb('have request event', t => { |
|
|
|
got.stream(s.url) |
|
|
|
.on('request', req => { |
|
|
|
t.ok(req); |
|
|
|
t.truthy(req); |
|
|
|
t.end(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
test('have redirect event', t => { |
|
|
|
test.cb('have redirect event', t => { |
|
|
|
got.stream(`${s.url}/redirect`) |
|
|
|
.on('redirect', res => { |
|
|
|
t.is(res.headers.location, s.url); |
|
|
@ -96,7 +96,7 @@ test('have redirect event', t => { |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
test('have response event', t => { |
|
|
|
test.cb('have response event', t => { |
|
|
|
got.stream(s.url) |
|
|
|
.on('response', res => { |
|
|
|
t.is(res.statusCode, 200); |
|
|
@ -104,7 +104,7 @@ test('have response event', t => { |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
test('have error event', t => { |
|
|
|
test.cb('have error event', t => { |
|
|
|
got.stream(`${s.url}/error`, {retries: 0}) |
|
|
|
.on('response', () => { |
|
|
|
t.fail('response event should not be emitted'); |
|
|
@ -112,31 +112,30 @@ test('have error event', t => { |
|
|
|
.on('error', (err, data, res) => { |
|
|
|
t.is(err.message, 'Response code 404 (Not Found)'); |
|
|
|
t.is(null, data); |
|
|
|
t.ok(res); |
|
|
|
t.truthy(res); |
|
|
|
t.end(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
test('have error event', t => { |
|
|
|
test.cb('throws error with invalid domain', t => { |
|
|
|
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.regex(err.message, /getaddrinfo ENOTFOUND/); |
|
|
|
t.end(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
test('accepts option.body as Stream', t => { |
|
|
|
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 => { |
|
|
|
test.after('cleanup', async () => { |
|
|
|
await s.close(); |
|
|
|
}); |
|
|
|