Skip to content

Commit

Permalink
chore(deps): upgrade to gts v2 (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Apr 12, 2020
1 parent be9542f commit 69a3649
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 160 deletions.
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ process.env.CHROME_BIN = fs.existsSync('/usr/bin/chromium-browser')
? '/usr/bin/chromium-browser'
: require('puppeteer').executablePath();

module.exports = function(config) {
module.exports = function (config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@
"@types/node-fetch": "^2.1.6",
"@types/sinon": "^9.0.0",
"@types/tmp": "0.1.0",
"assert-rejects": "^1.0.0",
"c8": "^7.0.0",
"codecov": "^3.2.0",
"execa": "^4.0.0",
"express": "^4.16.4",
"gts": "2.0.0-alpha.4",
"gts": "^2.0.0",
"is-docker": "^2.0.0",
"karma": "^5.0.0",
"karma-chrome-launcher": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion samples/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function quickstart() {
const url = 'https://www.googleapis.com/discovery/v1/apis/';
const res = await request({url});
console.log(`status: ${res.status}`);
console.log(`data:`);
console.log('data:');
console.log(res.data);
}
quickstart();
Expand Down
108 changes: 29 additions & 79 deletions test/test.getch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import nock from 'nock';
import sinon from 'sinon';
import stream from 'stream';
import {describe, it, afterEach} from 'mocha';
import assertRejects = require('assert-rejects');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const HttpsProxyAgent = require('https-proxy-agent');
import {
Expand All @@ -41,17 +40,15 @@ afterEach(() => {
const url = 'https://example.com';

describe('🦖 option validation', () => {
it('should throw an error if a url is not provided', () => {
assertRejects(request({}), /URL is required/);
it('should throw an error if a url is not provided', async () => {
await assert.rejects(request({}), /URL is required/);
});
});

describe('🚙 error handling', () => {
it('should throw on non-2xx responses by default', async () => {
const scope = nock(url)
.get('/')
.reply(500);
await assertRejects(request({url}), (err: GaxiosError) => {
const scope = nock(url).get('/').reply(500);
await assert.rejects(request({url}), (err: GaxiosError) => {
scope.done();
return err.code === '500';
});
Expand All @@ -60,19 +57,15 @@ describe('🚙 error handling', () => {

describe('🥁 configuration options', () => {
it('should use options passed into the constructor', async () => {
const scope = nock(url)
.head('/')
.reply(200);
const scope = nock(url).head('/').reply(200);
const inst = new Gaxios({method: 'HEAD'});
const res = await inst.request({url});
scope.done();
assert.strictEqual(res.config.method, 'HEAD');
});

it('should handle nested options passed into the constructor', async () => {
const scope = nock(url)
.get('/')
.reply(200);
const scope = nock(url).get('/').reply(200);
const inst = new Gaxios({headers: {apple: 'juice'}});
const res = await inst.request({url, headers: {figgy: 'pudding'}});
scope.done();
Expand All @@ -81,43 +74,33 @@ describe('🥁 configuration options', () => {
});

it('should allow setting a base url in the options', async () => {
const scope = nock(url)
.get('/v1/mango')
.reply(200, {});
const scope = nock(url).get('/v1/mango').reply(200, {});
const inst = new Gaxios({baseURL: `${url}/v1`});
const res = await inst.request({url: '/mango'});
scope.done();
assert.deepStrictEqual(res.data, {});
});

it('should allow overriding valid status', async () => {
const scope = nock(url)
.get('/')
.reply(304);
const scope = nock(url).get('/').reply(304);
const res = await request({url, validateStatus: () => true});
scope.done();
assert.strictEqual(res.status, 304);
});

it('should allow setting maxContentLength', async () => {
const body = {hello: '🌎'};
const scope = nock(url)
.get('/')
.reply(200, body);
const scope = nock(url).get('/').reply(200, body);
const maxContentLength = 1;
await assertRejects(request({url, maxContentLength}), /over limit/);
await assert.rejects(request({url, maxContentLength}), /over limit/);
scope.done();
});

it('should support redirects by default', async () => {
const body = {hello: '🌎'};
const scopes = [
nock(url)
.get('/foo')
.reply(200, body),
nock(url)
.get('/')
.reply(302, undefined, {location: '/foo'}),
nock(url).get('/foo').reply(200, body),
nock(url).get('/').reply(302, undefined, {location: '/foo'}),
];
const res = await request({url});
scopes.forEach(x => x.done());
Expand All @@ -126,11 +109,9 @@ describe('🥁 configuration options', () => {
});

it('should support disabling redirects', async () => {
const scope = nock(url)
.get('/')
.reply(302, undefined, {location: '/foo'});
const scope = nock(url).get('/').reply(302, undefined, {location: '/foo'});
const maxRedirects = 0;
await assertRejects(request({url, maxRedirects}), /maximum redirect/);
await assert.rejects(request({url, maxRedirects}), /maximum redirect/);
scope.done();
});

Expand All @@ -153,9 +134,7 @@ describe('🥁 configuration options', () => {
it('should encode URL parameters', async () => {
const path = '/?james=kirk&montgomery=scott';
const opts = {url: `${url}${path}`};
const scope = nock(url)
.get(path)
.reply(200, {});
const scope = nock(url).get(path).reply(200, {});
const res = await request(opts);
assert.strictEqual(res.status, 200);
assert.strictEqual(res.config.url, url + path);
Expand All @@ -165,9 +144,7 @@ describe('🥁 configuration options', () => {
it('should encode parameters from the params option', async () => {
const opts = {url, params: {james: 'kirk', montgomery: 'scott'}};
const path = '/?james=kirk&montgomery=scott';
const scope = nock(url)
.get(path)
.reply(200, {});
const scope = nock(url).get(path).reply(200, {});
const res = await request(opts);
assert.strictEqual(res.status, 200);
assert.strictEqual(res.config.url, url + path);
Expand All @@ -180,9 +157,7 @@ describe('🥁 configuration options', () => {
params: {james: 'kirk'},
};
const path = '/?james=kirk&montgomery=scott';
const scope = nock(url)
.get(path)
.reply(200, {});
const scope = nock(url).get(path).reply(200, {});
const res = await request(opts);
assert.strictEqual(res.status, 200);
assert.strictEqual(res.config.url, url + path);
Expand All @@ -200,9 +175,7 @@ describe('🥁 configuration options', () => {
return '?oh=HAI';
},
};
const scope = nock(url)
.get(`/${qs}`)
.reply(200, {});
const scope = nock(url).get(`/${qs}`).reply(200, {});
const res = await request(opts);
assert.strictEqual(res.status, 200);
assert.strictEqual(res.config.url, `${url}/${qs}`);
Expand All @@ -211,9 +184,7 @@ describe('🥁 configuration options', () => {

it('should return json by default', async () => {
const body = {hello: '🌎'};
const scope = nock(url)
.get('/')
.reply(200, body);
const scope = nock(url).get('/').reply(200, body);
const res = await request({url});
scope.done();
assert.deepStrictEqual(body, res.data);
Expand All @@ -232,9 +203,7 @@ describe('🥁 configuration options', () => {
it('should use an https proxy if asked nicely', async () => {
sandbox.stub(process, 'env').value({https_proxy: 'https://fake.proxy'});
const body = {hello: '🌎'};
const scope = nock(url)
.get('/')
.reply(200, body);
const scope = nock(url).get('/').reply(200, body);
const res = await request({url});
scope.done();
assert.deepStrictEqual(res.data, body);
Expand All @@ -244,10 +213,7 @@ describe('🥁 configuration options', () => {
it('should load the proxy from the cache', async () => {
sandbox.stub(process, 'env').value({HTTPS_PROXY: 'https://fake.proxy'});
const body = {hello: '🌎'};
const scope = nock(url)
.get('/')
.twice()
.reply(200, body);
const scope = nock(url).get('/').twice().reply(200, body);
const res1 = await request({url});
const agent = res1.config.agent;
const res2 = await request({url});
Expand All @@ -257,9 +223,7 @@ describe('🥁 configuration options', () => {

it('should include the request data in the response config', async () => {
const body = {hello: '🌎'};
const scope = nock(url)
.post('/', body)
.reply(200);
const scope = nock(url).post('/', body).reply(200);
const res = await request({url, method: 'POST', data: body});
scope.done();
assert.deepStrictEqual(res.config.data, body);
Expand All @@ -271,9 +235,7 @@ describe('🎏 data handling', () => {
const body = fs.createReadStream('package.json');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const contents = require('../../package.json');
const scope = nock(url)
.post('/', contents)
.reply(200, {});
const scope = nock(url).post('/', contents).reply(200, {});
const res = await request({url, method: 'POST', data: body});
scope.done();
assert.deepStrictEqual(res.data, {});
Expand Down Expand Up @@ -349,19 +311,15 @@ describe('🎏 data handling', () => {

it('should return stream if asked nicely', async () => {
const body = {hello: '🌎'};
const scope = nock(url)
.get('/')
.reply(200, body);
const scope = nock(url).get('/').reply(200, body);
const res = await request<stream.Readable>({url, responseType: 'stream'});
scope.done();
assert(res.data instanceof stream.Readable);
});

it('should return an ArrayBuffer if asked nicely', async () => {
const body = {hello: '🌎'};
const scope = nock(url)
.get('/')
.reply(200, body);
const scope = nock(url).get('/').reply(200, body);
const res = await request<ArrayBuffer>({
url,
responseType: 'arraybuffer',
Expand All @@ -376,29 +334,23 @@ describe('🎏 data handling', () => {

it('should return a blob if asked nicely', async () => {
const body = {hello: '🌎'};
const scope = nock(url)
.get('/')
.reply(200, body);
const scope = nock(url).get('/').reply(200, body);
const res = await request<Blob>({url, responseType: 'blob'});
scope.done();
assert.ok(res.data);
});

it('should return text if asked nicely', async () => {
const body = 'hello 🌎';
const scope = nock(url)
.get('/')
.reply(200, body);
const scope = nock(url).get('/').reply(200, body);
const res = await request<string>({url, responseType: 'text'});
scope.done();
assert.strictEqual(res.data, body);
});

it('should return status text', async () => {
const body = {hello: '🌎'};
const scope = nock(url)
.get('/')
.reply(200, body);
const scope = nock(url).get('/').reply(200, body);
const res = await request({url});
scope.done();
assert.ok(res.data);
Expand All @@ -414,9 +366,7 @@ describe('🍂 defaults & instances', () => {

it('should allow passing empty options', async () => {
const body = {hello: '🌎'};
const scope = nock(url)
.get('/')
.reply(200, body);
const scope = nock(url).get('/').reply(200, body);
const gax = new Gaxios({url});
const res = await gax.request();
scope.done();
Expand Down
Loading

0 comments on commit 69a3649

Please sign in to comment.