Skip to content

Commit

Permalink
fix(internal): fix http api (#5890)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Apr 6, 2020
1 parent 59d140f commit 4c0699c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ jobs:
- name: Unit tests
run: yarn jest --maxWorkers=2 --ci --coverage ${{ env.coverage }}
env:
GITHUB_TOKEN: ${{ github.token }}
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}

- name: Upload coverage
uses: actions/upload-artifact@v1
Expand Down
26 changes: 26 additions & 0 deletions lib/util/http/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`util/http/index get 1`] = `
Object {
"body": "",
"headers": Object {},
}
`;

exports[`util/http/index getJson 1`] = `
Object {
"body": Object {
"test": true,
},
"headers": Object {},
}
`;

exports[`util/http/index postJson 1`] = `
Object {
"body": Object {},
"headers": Object {
"content-type": "application/json",
},
}
`;
64 changes: 64 additions & 0 deletions lib/util/http/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import nock from 'nock';
import { getName } from '../../../test/util';
import { Http } from '.';

const baseUrl = 'http://renovate.com';

describe(getName(__filename), () => {
let http: Http;

beforeEach(() => {
http = new Http('dummy');
nock.cleanAll();
});
it('get', async () => {
nock(baseUrl)
.get('/test')
.reply(200);
expect(await http.get('http://renovate.com/test')).toMatchSnapshot();
expect(nock.isDone()).toBe(true);
});
it('getJson', async () => {
nock(baseUrl)
.get('/')
.reply(200, '{ "test": true }');
expect(await http.getJson('http://renovate.com')).toMatchSnapshot();
});
it('postJson', async () => {
nock(baseUrl)
.post('/')
.reply(200, {});
expect(
await http.postJson('http://renovate.com', { body: {}, baseUrl })
).toMatchSnapshot();
expect(nock.isDone()).toBe(true);
});

it('stream', async () => {
nock(baseUrl)
.get('/some')
.reply(200, {});

const stream = http.stream('/some', {
baseUrl,
});
expect(stream).toBeDefined();

let data = '';

stream.on('data', c => {
data += c;
});

const done = new Promise((resolve, reject) => {
stream.on('end', resolve);
stream.on('error', reject);
});

await done;

expect(data).toBe('{}');
expect(nock.isDone()).toBe(true);
expect(global.repoCache).toEqual({});
});
});
19 changes: 9 additions & 10 deletions lib/util/http/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import is from '@sindresorhus/is/dist';
import URL from 'url';
import got from '../got';

Expand Down Expand Up @@ -32,10 +31,10 @@ export interface HttpResponse<T = string> {
export class Http {
constructor(private hostType: string, private options?: HttpOptions) {}

private async request(
private async request<T>(
url: string | URL,
options?: InternalHttpOptions
): Promise<HttpResponse | null> {
): Promise<HttpResponse<T> | null> {
let resolvedUrl = url.toString();
if (options?.baseUrl) {
resolvedUrl = URL.resolve(options.baseUrl, resolvedUrl);
Expand Down Expand Up @@ -72,30 +71,30 @@ export class Http {
}

get(url: string, options: HttpOptions = {}): Promise<HttpResponse> {
return this.request(url, options);
return this.request<string>(url, options);
}

async requestJson<T = unknown>(
private async requestJson<T = unknown>(
url: string,
options: InternalHttpOptions = {}
options: InternalHttpOptions
): Promise<HttpResponse<T>> {
const res = await this.request(url, { ...options, json: true });
const body = is.string(res.body) ? JSON.parse(res.body) : res.body;
const res = await this.request<T>(url, { ...options, json: true });
const body = res.body;
return { ...res, body };
}

async getJson<T = unknown>(
url: string,
options: HttpOptions = {}
): Promise<HttpResponse<T>> {
return this.requestJson(url, options);
return this.requestJson<T>(url, options);
}

async postJson<T = unknown>(
url: string,
options: HttpPostOptions
): Promise<HttpResponse<T>> {
return this.requestJson(url, { ...options, method: 'post' });
return this.requestJson<T>(url, { ...options, method: 'post' });
}

stream(url: string, options?: HttpOptions): NodeJS.ReadableStream {
Expand Down
5 changes: 5 additions & 0 deletions test/globals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { join } from 'upath';
import { tmpdir } from 'os';
import nock from 'nock';
import { init } from '../lib/workers/global/cache';

jest.mock('../lib/platform', () => ({
Expand All @@ -14,3 +15,7 @@ const tmpDir = process.env.RENOVATE_TMPDIR || process.env.TMPDIR || tmpdir();
const cacheDir = join(tmpDir, './renovate/cache/renovate');

init(cacheDir);

beforeAll(() => {
nock.disableNetConnect();
});
9 changes: 6 additions & 3 deletions tools/jest-gh-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,18 @@ function getPos(
}

class GitHubReporter extends BaseReporter {
private readonly _api: GitHub | null;
private readonly _api: GitHub | null = null;

constructor() {
super();
try {
this._api = new GitHub(getEnv('GITHUB_TOKEN'));
const token = getEnv('GITHUB_TOKEN');
if (!token) {
return;
}
this._api = new GitHub(token);
} catch (e) {
error(`Unexpected error: ${e}`);
this._api = null;
}
}

Expand Down

0 comments on commit 4c0699c

Please sign in to comment.