Skip to content

Commit

Permalink
chore(internal): fix http coverage (#6746)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Jul 14, 2020
1 parent 49680ea commit 76d8574
Show file tree
Hide file tree
Showing 12 changed files with 453 additions and 228 deletions.
18 changes: 18 additions & 0 deletions lib/util/http/__snapshots__/bitbucket-server.spec.ts.snap
@@ -0,0 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`util/http/bitbucket-server posts 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"authorization": "Bearer token",
"host": "git.example.com",
"user-agent": "https://github.com/renovatebot/renovate",
"x-atlassian-token": "no-check",
},
"method": "POST",
"url": "https://git.example.com/some-url",
},
]
`;
9 changes: 9 additions & 0 deletions lib/util/http/__snapshots__/index.spec.ts.snap
Expand Up @@ -60,3 +60,12 @@ Object {
},
}
`;

exports[`util/http/index retries 1`] = `
Object {
"body": "",
"headers": Object {
"x-some-header": "abc",
},
}
`;
229 changes: 136 additions & 93 deletions lib/util/http/auth.spec.ts
@@ -1,108 +1,151 @@
import { getName } from '../../../test/util';
import { removeAuthorization } from './auth';
import { PLATFORM_TYPE_GITEA } from '../../constants/platforms';
import { applyAuthorization, removeAuthorization } from './auth';

describe(getName(__filename), () => {
it('removeAuthorization no authorization', () => {
const opts = {
hostname: 'amazon.com',
href: 'https://amazon.com',
search: 'something X-Amz-Algorithm something',
};

removeAuthorization(opts);

expect(opts).toEqual({
hostname: 'amazon.com',
href: 'https://amazon.com',
search: 'something X-Amz-Algorithm something',
describe('applyAuthorization', () => {
it('does nothing', () => {
const opts = {
hostname: 'amazon.com',
href: 'https://amazon.com',
auth: 'XXXX',
};

applyAuthorization(opts);

expect(opts).toMatchInlineSnapshot(`
Object {
"auth": "XXXX",
"hostname": "amazon.com",
"href": "https://amazon.com",
}
`);
});
});

it('removeAuthorization Amazon', () => {
const opts = {
auth: 'auth',
headers: {
authorization: 'auth',
},
hostname: 'amazon.com',
href: 'https://amazon.com',
search: 'something X-Amz-Algorithm something',
};

removeAuthorization(opts);

expect(opts).toEqual({
headers: {},
hostname: 'amazon.com',
href: 'https://amazon.com',
search: 'something X-Amz-Algorithm something',
it('gittea token', () => {
const opts = {
headers: {},
token: 'XXXX',
hostType: PLATFORM_TYPE_GITEA,
};

applyAuthorization(opts);

expect(opts).toMatchInlineSnapshot(`
Object {
"headers": Object {
"authorization": "token XXXX",
},
"hostType": "gitea",
"token": "XXXX",
}
`);
});
});

it('removeAuthorization Amazon ports', () => {
const opts = {
auth: 'auth',
headers: {
authorization: 'auth',
},
hostname: 'amazon.com',
href: 'https://amazon.com',
port: 3000,
search: 'something X-Amz-Algorithm something',
};

removeAuthorization(opts);

expect(opts).toEqual({
headers: {},
hostname: 'amazon.com',
href: 'https://amazon.com',
search: 'something X-Amz-Algorithm something',
describe('removeAuthorization', () => {
it('no authorization', () => {
const opts = {
hostname: 'amazon.com',
href: 'https://amazon.com',
search: 'something X-Amz-Algorithm something',
};

removeAuthorization(opts);

expect(opts).toEqual({
hostname: 'amazon.com',
href: 'https://amazon.com',
search: 'something X-Amz-Algorithm something',
});
});
});

it('removeAuthorization Azure blob', () => {
const opts = {
auth: 'auth',
headers: {
authorization: 'auth',
},
hostname: 'store123.blob.core.windows.net',
href:
'https://<store>.blob.core.windows.net/<some id>//docker/registry/v2/blobs',
};

removeAuthorization(opts);

expect(opts).toEqual({
headers: {},
hostname: 'store123.blob.core.windows.net',
href:
'https://<store>.blob.core.windows.net/<some id>//docker/registry/v2/blobs',
it('Amazon', () => {
const opts = {
auth: 'auth',
headers: {
authorization: 'auth',
},
hostname: 'amazon.com',
href: 'https://amazon.com',
search: 'something X-Amz-Algorithm something',
};

removeAuthorization(opts);

expect(opts).toEqual({
headers: {},
hostname: 'amazon.com',
href: 'https://amazon.com',
search: 'something X-Amz-Algorithm something',
});
});
});

it('removeAuthorization keep auth', () => {
const opts = {
auth: 'auth',
headers: {
authorization: 'auth',
},
hostname: 'renovate.com',
href: 'https://renovate.com',
search: 'something',
};

removeAuthorization(opts);

expect(opts).toEqual({
auth: 'auth',
headers: {
authorization: 'auth',
},
hostname: 'renovate.com',
href: 'https://renovate.com',
search: 'something',
it('Amazon ports', () => {
const opts = {
auth: 'auth',
headers: {
authorization: 'auth',
},
hostname: 'amazon.com',
href: 'https://amazon.com',
port: 3000,
search: 'something X-Amz-Algorithm something',
};

removeAuthorization(opts);

expect(opts).toEqual({
headers: {},
hostname: 'amazon.com',
href: 'https://amazon.com',
search: 'something X-Amz-Algorithm something',
});
});

it('Azure blob', () => {
const opts = {
auth: 'auth',
headers: {
authorization: 'auth',
},
hostname: 'store123.blob.core.windows.net',
href:
'https://<store>.blob.core.windows.net/<some id>//docker/registry/v2/blobs',
};

removeAuthorization(opts);

expect(opts).toEqual({
headers: {},
hostname: 'store123.blob.core.windows.net',
href:
'https://<store>.blob.core.windows.net/<some id>//docker/registry/v2/blobs',
});
});

it('keep auth', () => {
const opts = {
auth: 'auth',
headers: {
authorization: 'auth',
},
hostname: 'renovate.com',
href: 'https://renovate.com',
search: 'something',
};

removeAuthorization(opts);

expect(opts).toEqual({
auth: 'auth',
headers: {
authorization: 'auth',
},
hostname: 'renovate.com',
href: 'https://renovate.com',
search: 'something',
});
});
});
});
1 change: 1 addition & 0 deletions lib/util/http/auth.ts
Expand Up @@ -49,6 +49,7 @@ export function removeAuthorization(options: any): void {
// if there is no port in the redirect URL string, then delete it from the redirect options.
// This can be evaluated for removal after upgrading to Got v10
const portInUrl = options.href.split('/')[2].split(':')[1];
// istanbul ignore next
if (!portInUrl) {
// eslint-disable-next-line no-param-reassign
delete options.port; // Redirect will instead use 80 or 443 for HTTP or HTTPS respectively
Expand Down
37 changes: 37 additions & 0 deletions lib/util/http/bitbucket-server.spec.ts
@@ -0,0 +1,37 @@
import * as httpMock from '../../../test/httpMock';
import { getName } from '../../../test/util';
import { PLATFORM_TYPE_BITBUCKET_SERVER } from '../../constants/platforms';
import * as hostRules from '../host-rules';
import { BitbucketServerHttp, setBaseUrl } from './bitbucket-server';

const baseUrl = 'https://git.example.com';

describe(getName(__filename), () => {
let api: BitbucketServerHttp;
beforeEach(() => {
api = new BitbucketServerHttp();

// reset module
jest.resetAllMocks();

// clean up hostRules
hostRules.clear();
hostRules.add({
hostType: PLATFORM_TYPE_BITBUCKET_SERVER,
baseUrl,
token: 'token',
});

httpMock.reset();
httpMock.setup();

setBaseUrl(baseUrl);
});
it('posts', async () => {
const body = ['a', 'b'];
httpMock.scope(baseUrl).post('/some-url').reply(200, body);
const res = await api.postJson('some-url');
expect(res.body).toEqual(body);
expect(httpMock.getTrace()).toMatchSnapshot();
});
});

0 comments on commit 76d8574

Please sign in to comment.