Skip to content

Commit c02f8f3

Browse files
committed
Fix http error tests
1 parent 66bfd76 commit c02f8f3

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/internal/http.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {createServer, Server} from 'http';
22
import {Got} from 'got';
3-
import {httpClient} from './http';
3+
import {get, httpClient} from './http';
44
import {
55
CFToolsUnavailable,
66
DuplicateResourceCreation, GrantRequired,
@@ -52,49 +52,49 @@ describe('http', () => {
5252
it('throws ResourceNotFound on 404', async () => {
5353
httpResponse = {code: 404, errorText: 'invalid-resource'};
5454

55-
await expect(client('not-found')).rejects.toThrowError(new ResourceNotFound());
55+
await expect(get('not-found', undefined, client)).rejects.toThrowError(new ResourceNotFound());
5656
});
5757

5858
it('throws RequestLimitExceeded on 429', async () => {
5959
httpResponse = {code: 429, errorText: 'rate-limited'};
6060

61-
await expect(client('rate-limited')).rejects.toThrowError(new RequestLimitExceeded());
61+
await expect(get('rate-limited', undefined, client)).rejects.toThrowError(new RequestLimitExceeded());
6262
});
6363

6464
it('throws DuplicateResourceCreation on 400 with duplicate', async () => {
65-
httpResponse = {code: 429, errorText: 'duplicate'};
65+
httpResponse = {code: 400, errorText: 'duplicate'};
6666

67-
await expect(client('duplicate')).rejects.toThrowError(new DuplicateResourceCreation());
67+
await expect(get('duplicate', undefined, client)).rejects.toThrowError(new DuplicateResourceCreation());
6868
});
6969

7070
it('throws UnknownError on 500 with unexpected-error', async () => {
7171
httpResponse = {code: 500, errorText: 'unexpected-error', request_id: 'SOME_ID'};
7272

73-
await expect(client('unexpected-error')).rejects.toThrowError(new UnknownError('SOME_ID'));
73+
await expect(get('unexpected-error', undefined, client)).rejects.toThrowError(new UnknownError('SOME_ID'));
7474
});
7575

7676
it('throws TimeoutError on 500 with timeout', async () => {
7777
httpResponse = {code: 500, errorText: 'timeout'};
7878

79-
await expect(client('timeout')).rejects.toThrowError(new TimeoutError());
79+
await expect(get('timeout', undefined, client)).rejects.toThrowError(new TimeoutError());
8080
});
8181

8282
it('throws CFToolsUnavailable on 500 with system-unavailable', async () => {
8383
httpResponse = {code: 500, errorText: 'system-unavailable'};
8484

85-
await expect(client('system-unavailable')).rejects.toThrowError(new CFToolsUnavailable());
85+
await expect(get('system-unavailable', undefined, client)).rejects.toThrowError(new CFToolsUnavailable());
8686
});
8787

8888
it('throws GrantRequired on 403 with no-grant', async () => {
8989
httpResponse = {code: 403, errorText: 'no-grant'};
9090

91-
await expect(client('no-grant')).rejects.toThrowError(new GrantRequired());
91+
await expect(get('no-grant', undefined, client)).rejects.toThrowError(new GrantRequired());
9292
});
9393

9494
it('throws TokenExpired on 403 with expired-token', async () => {
9595
httpResponse = {code: 403, errorText: 'expired-token'};
9696

97-
await expect(client('expired-token')).rejects.toThrowError(new TokenExpired());
97+
await expect(get('expired-token', undefined, client)).rejects.toThrowError(new TokenExpired());
9898
});
9999

100100
afterAll(() => {

src/internal/http.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ export function fromHttpError(error: HTTPError): Error {
4848
return error;
4949
}
5050

51-
export async function get<T>(url: string, options?: OptionsOfTextResponseBody): Promise<T> {
52-
return withErrorHandler(httpClient(url, options).json<T>());
51+
export async function get<T>(url: string, options?: OptionsOfTextResponseBody, client: Got = httpClient): Promise<T> {
52+
return withErrorHandler(client(url, options).json<T>());
5353
}
5454

55-
export async function post<T>(url: string, options?: OptionsOfTextResponseBody): Promise<T> {
56-
return withErrorHandler(httpClient.post(url, options).json<T>());
55+
export async function post<T>(url: string, options?: OptionsOfTextResponseBody, client: Got = httpClient): Promise<T> {
56+
return withErrorHandler(client.post(url, options).json<T>());
5757
}
5858

59-
export async function httpDelete<T>(url: string, options?: OptionsOfTextResponseBody): Promise<T> {
60-
return withErrorHandler(httpClient.delete(url, options).json<T>());
59+
export async function httpDelete<T>(url: string, options?: OptionsOfTextResponseBody, client: Got = httpClient): Promise<T> {
60+
return withErrorHandler(client.delete(url, options).json<T>());
6161
}
6262

6363
async function withErrorHandler<T>(request: Promise<T>): Promise<T> {

0 commit comments

Comments
 (0)