mirror of
https://github.com/jdalrymple/gitbeaker.git
synced 2026-01-18 15:55:30 +00:00
184 lines
4.4 KiB
TypeScript
184 lines
4.4 KiB
TypeScript
import ky from 'ky';
|
|
import { Headers } from 'headers-utils';
|
|
import { processBody, handler, defaultOptionsHandler } from '../../src/KyRequester';
|
|
|
|
global.Headers = Headers;
|
|
|
|
jest.mock('ky');
|
|
|
|
const MockKy = ky as unknown as jest.Mock;
|
|
|
|
describe('processBody', () => {
|
|
it('should return a json object if type is application/json', async () => {
|
|
const output = await processBody({
|
|
json() {
|
|
return Promise.resolve({ test: 5 });
|
|
},
|
|
headers: {
|
|
get() {
|
|
return 'application/json';
|
|
},
|
|
},
|
|
} as unknown as Response);
|
|
|
|
expect(output).toMatchObject({ test: 5 });
|
|
});
|
|
|
|
it('should return a buffer if type is octet-stream, binary, or gzip', async () => {
|
|
const output = [
|
|
processBody({
|
|
arrayBuffer() {
|
|
return Promise.resolve(Buffer.alloc(0));
|
|
},
|
|
headers: {
|
|
get() {
|
|
return 'application/octet-stream';
|
|
},
|
|
},
|
|
} as unknown as Response),
|
|
processBody({
|
|
arrayBuffer() {
|
|
return Promise.resolve(Buffer.alloc(0));
|
|
},
|
|
headers: {
|
|
get() {
|
|
return 'binary/octet-stream';
|
|
},
|
|
},
|
|
} as unknown as Response),
|
|
processBody({
|
|
arrayBuffer() {
|
|
return Promise.resolve(Buffer.alloc(0));
|
|
},
|
|
headers: {
|
|
get() {
|
|
return 'image/png';
|
|
},
|
|
},
|
|
} as unknown as Response),
|
|
processBody({
|
|
arrayBuffer() {
|
|
return Promise.resolve(Buffer.alloc(0));
|
|
},
|
|
headers: {
|
|
get() {
|
|
return 'application/gzip';
|
|
},
|
|
},
|
|
} as unknown as Response),
|
|
];
|
|
|
|
const fulfilled = await Promise.all(output);
|
|
|
|
fulfilled.forEach((o) => expect(o).toBeInstanceOf(Buffer));
|
|
});
|
|
|
|
it('should return a string if type is text/<subtype>', async () => {
|
|
const output = await processBody({
|
|
text() {
|
|
return Promise.resolve('test');
|
|
},
|
|
headers: {
|
|
get() {
|
|
return 'text/plain';
|
|
},
|
|
},
|
|
} as unknown as Response);
|
|
|
|
expect(typeof output).toBe('string');
|
|
expect(output).toEqual('test');
|
|
});
|
|
|
|
it('should return a empty string when presented with an unknown content-type and empty body', async () => {
|
|
const output = await processBody({
|
|
arrayBuffer() {
|
|
return Promise.resolve(Buffer.alloc(0));
|
|
},
|
|
headers: {
|
|
get() {
|
|
return 'fake';
|
|
},
|
|
},
|
|
} as unknown as Response);
|
|
|
|
expect(output).toBeInstanceOf(Buffer);
|
|
expect(output.length).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('handler', () => {
|
|
it('should return an error with a description when response has an error prop', async () => {
|
|
MockKy.mockImplementationOnce(() => {
|
|
const e = {
|
|
response: {
|
|
json() {
|
|
return Promise.resolve({ error: 'msg' });
|
|
},
|
|
},
|
|
};
|
|
return Promise.reject(e);
|
|
});
|
|
|
|
await expect(handler('http://test.com', {})).rejects.toMatchObject({ description: 'msg' });
|
|
});
|
|
|
|
it('should return an error with a description when response has an message prop', async () => {
|
|
MockKy.mockImplementationOnce(() => {
|
|
const e = {
|
|
response: {
|
|
json() {
|
|
return Promise.resolve({ message: 'msg' });
|
|
},
|
|
},
|
|
};
|
|
return Promise.reject(e);
|
|
});
|
|
|
|
await expect(handler('http://test.com', {})).rejects.toMatchObject({ description: 'msg' });
|
|
});
|
|
|
|
it('should return correct properties if request is valid', async () => {
|
|
MockKy.mockImplementationOnce(() => ({
|
|
status: 404,
|
|
headers: {
|
|
get() {
|
|
return 'application/json';
|
|
},
|
|
entries() {
|
|
return [['token', '1234']];
|
|
},
|
|
},
|
|
json() {
|
|
return Promise.resolve({});
|
|
},
|
|
}));
|
|
|
|
const output = await handler('http://test.com', {});
|
|
|
|
expect(output).toMatchObject({
|
|
body: {},
|
|
headers: {},
|
|
status: 404,
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('defaultRequest', () => {
|
|
const service = {
|
|
headers: { test: '5' },
|
|
url: 'testurl',
|
|
requestTimeout: 50,
|
|
rejectUnauthorized: true,
|
|
};
|
|
|
|
it('should stringify body if it isnt of type FormData', () => {
|
|
const testBody = { test: 6 };
|
|
const { body, headers } = defaultOptionsHandler(service, {
|
|
body: testBody,
|
|
});
|
|
|
|
expect(headers).toBeInstanceOf(Headers);
|
|
expect(body).toBe(JSON.stringify(testBody));
|
|
});
|
|
});
|