2020-12-11 12:29:43 +00:00
|
|
|
import * as httpMock from '../../../test/http-mock';
|
2020-06-01 14:02:25 +00:00
|
|
|
import * as hostRules from '../host-rules';
|
|
|
|
import { BitbucketHttp, setBaseUrl } from './bitbucket';
|
2020-05-24 16:57:33 +00:00
|
|
|
|
|
|
|
const baseUrl = 'https://api.bitbucket.org';
|
2019-05-21 08:34:28 +00:00
|
|
|
|
2021-08-18 05:46:56 +00:00
|
|
|
describe('util/http/bitbucket', () => {
|
2020-06-01 14:02:25 +00:00
|
|
|
let api: BitbucketHttp;
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2018-08-29 05:30:03 +00:00
|
|
|
beforeEach(() => {
|
2020-06-01 14:02:25 +00:00
|
|
|
api = new BitbucketHttp();
|
|
|
|
|
2018-08-29 05:30:03 +00:00
|
|
|
// reset module
|
|
|
|
jest.resetAllMocks();
|
|
|
|
|
2018-09-12 10:16:17 +00:00
|
|
|
// clean up hostRules
|
|
|
|
hostRules.clear();
|
2019-05-24 15:40:39 +00:00
|
|
|
hostRules.add({
|
2022-11-01 14:46:09 +00:00
|
|
|
hostType: 'bitbucket',
|
2021-05-13 20:53:18 +00:00
|
|
|
matchHost: baseUrl,
|
2018-08-29 05:30:03 +00:00
|
|
|
token: 'token',
|
|
|
|
});
|
2020-05-24 16:57:33 +00:00
|
|
|
|
2020-06-01 14:02:25 +00:00
|
|
|
setBaseUrl(baseUrl);
|
2018-08-29 05:30:03 +00:00
|
|
|
});
|
2021-05-27 12:13:31 +00:00
|
|
|
|
2018-08-29 05:30:03 +00:00
|
|
|
it('posts', async () => {
|
|
|
|
const body = ['a', 'b'];
|
2020-05-24 16:57:33 +00:00
|
|
|
httpMock.scope(baseUrl).post('/some-url').reply(200, body);
|
2020-06-01 14:02:25 +00:00
|
|
|
const res = await api.postJson('some-url');
|
2018-08-29 05:30:03 +00:00
|
|
|
expect(res.body).toEqual(body);
|
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2020-03-19 05:38:52 +00:00
|
|
|
it('accepts custom baseUrl', async () => {
|
2020-05-24 16:57:33 +00:00
|
|
|
const customBaseUrl = 'https://api-test.bitbucket.org';
|
|
|
|
httpMock.scope(baseUrl).post('/some-url').reply(200, {});
|
|
|
|
httpMock.scope(customBaseUrl).post('/some-url').reply(200, {});
|
2020-03-19 05:38:52 +00:00
|
|
|
|
2022-04-12 14:09:19 +00:00
|
|
|
expect(await api.postJson('some-url')).toEqual({
|
|
|
|
authorization: true,
|
|
|
|
body: {},
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
},
|
|
|
|
statusCode: 200,
|
|
|
|
});
|
2020-03-19 05:38:52 +00:00
|
|
|
|
2020-06-01 14:02:25 +00:00
|
|
|
setBaseUrl(customBaseUrl);
|
2022-04-12 14:09:19 +00:00
|
|
|
expect(await api.postJson('some-url')).toEqual({
|
|
|
|
authorization: false,
|
|
|
|
body: {},
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
},
|
|
|
|
statusCode: 200,
|
|
|
|
});
|
2020-03-19 05:38:52 +00:00
|
|
|
});
|
2018-08-29 05:30:03 +00:00
|
|
|
});
|