2020-12-11 12:29:43 +00:00
|
|
|
import * as httpMock from '../../../test/http-mock';
|
2020-06-01 12:01:09 +00:00
|
|
|
import { GiteaHttp, setBaseUrl } from './gitea';
|
2020-02-19 10:19:25 +00:00
|
|
|
|
2021-08-18 05:46:56 +00:00
|
|
|
describe('util/http/gitea', () => {
|
2020-05-30 20:00:07 +00:00
|
|
|
const baseUrl = 'https://gitea.renovatebot.com/api/v1';
|
2020-02-19 10:19:25 +00:00
|
|
|
|
2020-06-01 12:01:09 +00:00
|
|
|
let giteaHttp: GiteaHttp;
|
|
|
|
|
2020-05-30 20:00:07 +00:00
|
|
|
beforeEach(() => {
|
2020-06-01 12:01:09 +00:00
|
|
|
giteaHttp = new GiteaHttp();
|
|
|
|
|
|
|
|
setBaseUrl(baseUrl);
|
2020-02-19 10:19:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('supports responses without pagination when enabled', async () => {
|
2020-05-30 20:00:07 +00:00
|
|
|
httpMock
|
|
|
|
.scope(baseUrl)
|
|
|
|
.get('/pagination-example-1')
|
|
|
|
.reply(200, { hello: 'world' });
|
2020-02-19 10:19:25 +00:00
|
|
|
|
2020-06-01 12:01:09 +00:00
|
|
|
const res = await giteaHttp.getJson('pagination-example-1', {
|
|
|
|
paginate: true,
|
|
|
|
});
|
2020-02-19 10:19:25 +00:00
|
|
|
expect(res.body).toEqual({ hello: 'world' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports root-level pagination', async () => {
|
2020-05-30 20:00:07 +00:00
|
|
|
httpMock
|
|
|
|
.scope(baseUrl)
|
|
|
|
.get('/pagination-example-1')
|
|
|
|
.reply(200, ['abc', 'def', 'ghi'], { 'x-total-count': '5' })
|
|
|
|
.get('/pagination-example-1?page=2')
|
|
|
|
.reply(200, ['jkl'])
|
|
|
|
.get('/pagination-example-1?page=3')
|
|
|
|
.reply(200, ['mno', 'pqr']);
|
|
|
|
|
2020-06-01 12:01:09 +00:00
|
|
|
const res = await giteaHttp.getJson(`${baseUrl}/pagination-example-1`, {
|
2020-03-27 13:58:22 +00:00
|
|
|
paginate: true,
|
|
|
|
});
|
2020-02-19 10:19:25 +00:00
|
|
|
|
|
|
|
expect(res.body).toHaveLength(6);
|
|
|
|
expect(res.body).toEqual(['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports pagination on data property', async () => {
|
2020-05-30 20:00:07 +00:00
|
|
|
httpMock
|
|
|
|
.scope(baseUrl)
|
|
|
|
.get('/pagination-example-2')
|
|
|
|
.reply(200, { data: ['abc', 'def', 'ghi'] }, { 'x-total-count': '5' })
|
|
|
|
.get('/pagination-example-2?page=2')
|
|
|
|
.reply(200, { data: ['jkl'] })
|
|
|
|
.get('/pagination-example-2?page=3')
|
|
|
|
.reply(200, { data: ['mno', 'pqr'] });
|
2020-02-19 10:19:25 +00:00
|
|
|
|
2020-06-27 08:25:20 +00:00
|
|
|
const res = await giteaHttp.getJson<{ data: string[] }>(
|
|
|
|
'pagination-example-2',
|
|
|
|
{
|
|
|
|
paginate: true,
|
2023-11-07 15:50:29 +00:00
|
|
|
},
|
2020-06-27 08:25:20 +00:00
|
|
|
);
|
2020-02-19 10:19:25 +00:00
|
|
|
expect(res.body.data).toHaveLength(6);
|
|
|
|
expect(res.body.data).toEqual(['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr']);
|
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2020-06-27 08:25:20 +00:00
|
|
|
it('handles pagination with empty response', async () => {
|
|
|
|
httpMock
|
|
|
|
.scope(baseUrl)
|
|
|
|
.get('/pagination-example-3')
|
|
|
|
.reply(200, { data: ['abc', 'def', 'ghi'] }, { 'x-total-count': '5' })
|
|
|
|
.get('/pagination-example-3?page=2')
|
|
|
|
.reply(200, { data: [] });
|
|
|
|
|
|
|
|
const res = await giteaHttp.getJson<{ data: string[] }>(
|
|
|
|
'pagination-example-3',
|
|
|
|
{
|
|
|
|
paginate: true,
|
2023-11-07 15:50:29 +00:00
|
|
|
},
|
2020-06-27 08:25:20 +00:00
|
|
|
);
|
|
|
|
expect(res.body.data).toHaveLength(3);
|
|
|
|
expect(res.body.data).toEqual(['abc', 'def', 'ghi']);
|
|
|
|
});
|
2020-02-19 10:19:25 +00:00
|
|
|
});
|