Browse Source

Rewrite stream tests to use async/await

No more `test.cb()` \o/
update-cacheable-request
Sindre Sorhus 7 years ago
parent
commit
6e4eafa0fc
  1. 1
      package.json
  2. 102
      test/stream.js
  3. 12
      test/timeout.js

1
package.json

@ -75,6 +75,7 @@
"form-data": "^2.1.1", "form-data": "^2.1.1",
"get-port": "^3.0.0", "get-port": "^3.0.0",
"nyc": "^11.0.2", "nyc": "^11.0.2",
"p-event": "^1.3.0",
"pem": "^1.4.4", "pem": "^1.4.4",
"sinon": "^4.0.0", "sinon": "^4.0.0",
"slow-stream": "0.0.4", "slow-stream": "0.0.4",

102
test/stream.js

@ -1,6 +1,7 @@
import test from 'ava'; import test from 'ava';
import intoStream from 'into-stream'; import intoStream from 'into-stream';
import getStream from 'get-stream'; import getStream from 'get-stream';
import pEvent from 'p-event';
import got from '..'; import got from '..';
import {createServer} from './helpers/server'; import {createServer} from './helpers/server';
@ -38,94 +39,59 @@ test('option.json can not be used', t => {
}, 'Got can not be used as a stream when the `json` option is used'); }, 'Got can not be used as a stream when the `json` option is used');
}); });
test.cb('returns readable stream', t => { test('returns readable stream', async t => {
got.stream(s.url) const data = await pEvent(got.stream(s.url), 'data');
.on('data', data => { t.is(data.toString(), 'ok');
t.is(data.toString(), 'ok');
t.end();
});
}); });
test.cb('returns writeable stream', t => { test('returns writeable stream', async t => {
got.stream.post(`${s.url}/post`) const stream = got.stream.post(`${s.url}/post`);
.on('data', data => { const promise = pEvent(stream, 'data');
t.is(data.toString(), 'wow'); stream.end('wow');
t.end(); t.is((await promise).toString(), 'wow');
})
.end('wow');
}); });
test.cb('throws on write to stream with body specified', t => { test('throws on write to stream with body specified', t => {
t.throws(() => { t.throws(() => {
got.stream(s.url, {body: 'wow'}).write('wow'); got.stream(s.url, {body: 'wow'}).end('wow');
}, 'Got\'s stream is not writable when the `body` option is used'); }, 'Got\'s stream is not writable when the `body` option is used');
// Wait for request to end
setTimeout(t.end, 10);
}); });
test.cb('have request event', t => { test('have request event', async t => {
got.stream(s.url) const request = await pEvent(got.stream(s.url), 'request');
.on('request', req => { t.truthy(request);
t.truthy(req); t.is(request.method, 'GET');
t.end();
});
}); });
test.cb('have redirect event', t => { test('have redirect event', async t => {
got.stream(`${s.url}/redirect`) const response = await pEvent(got.stream(`${s.url}/redirect`), 'redirect');
.on('redirect', res => { t.is(response.headers.location, s.url);
t.is(res.headers.location, s.url);
t.end();
});
}); });
test.cb('have response event', t => { test('have response event', async t => {
got.stream(s.url) const response = await pEvent(got.stream(s.url), 'response');
.on('response', res => { t.is(response.statusCode, 200);
t.is(res.statusCode, 200);
t.end();
});
}); });
test.cb('have error event', t => { test('have error event', async t => {
got.stream(`${s.url}/error`, {retries: 0}) const stream = got.stream(`${s.url}/error`, {retries: 0});
.on('response', () => { await t.throws(pEvent(stream, 'response'), /Response code 404 \(Not Found\)/);
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.truthy(res);
t.end();
});
}); });
test.cb('have error event #2', t => { test('have error event #2', async t => {
got.stream('.com', {retries: 0}) const stream = got.stream('.com', {retries: 0});
.on('response', () => { await t.throws(pEvent(stream, 'response'), /getaddrinfo ENOTFOUND/);
t.fail('response event should not be emitted');
})
.on('error', err => {
t.regex(err.message, /getaddrinfo ENOTFOUND/);
t.end();
});
}); });
test.cb('accepts option.body as Stream', t => { test('accepts option.body as Stream', async t => {
got.stream(`${s.url}/post`, {body: intoStream(['wow'])}) const stream = got.stream(`${s.url}/post`, {body: intoStream(['wow'])});
.on('data', chunk => { const data = await pEvent(stream, 'data');
t.is(chunk.toString(), 'wow'); t.is(data.toString(), 'wow');
t.end();
});
}); });
test.cb('redirect response contains old url', t => { test('redirect response contains old url', async t => {
got.stream(`${s.url}/redirect`) const response = await pEvent(got.stream(`${s.url}/redirect`), 'response');
.on('response', res => { t.is(response.requestUrl, `${s.url}/redirect`);
t.is(res.requestUrl, `${s.url}/redirect`);
t.end();
});
}); });
test('check for pipe method', t => { test('check for pipe method', t => {

12
test/timeout.js

@ -1,4 +1,5 @@
import test from 'ava'; import test from 'ava';
import pEvent from 'p-event';
import got from '..'; import got from '..';
import {createServer} from './helpers/server'; import {createServer} from './helpers/server';
@ -51,11 +52,8 @@ test('connection, request timeout', async t => {
t.is(err.code, 'ETIMEDOUT'); t.is(err.code, 'ETIMEDOUT');
}); });
test.cb('timeout with streams', t => { test('timeout with streams', async t => {
got.stream(s.url, {timeout: 1, retries: 0}) const stream = got.stream(s.url, {timeout: 1, retries: 0});
.on('error', err => { const err = await t.throws(pEvent(stream, 'response'));
t.is(err.code, 'ETIMEDOUT'); t.is(err.code, 'ETIMEDOUT');
t.end();
})
.on('data', t.end);
}); });

Loading…
Cancel
Save