Skip to content

Commit d46d287

Browse files
committed
Address test failures due to V4 API response changes
1 parent 197dfd5 commit d46d287

File tree

12 files changed

+127
-84
lines changed

12 files changed

+127
-84
lines changed

packages/containers-shared/src/client/core/request.ts

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,17 @@ import type { ApiRequestOptions } from "./ApiRequestOptions";
99
import type { ApiResult } from "./ApiResult";
1010
import type { OnCancel } from "./CancelablePromise";
1111

12-
interface FetchResponseInfo {
12+
type FetchResponseInfo = {
1313
code: number;
14-
documentation_url?: string;
1514
message: string;
16-
source?: {
17-
pointer?: string;
18-
};
19-
}
15+
};
2016

21-
interface FetchResult<ResponseType = any> {
17+
type FetchResult<ResponseType = unknown> = {
2218
success: boolean;
23-
result: ResponseType;
24-
errors: FetchResponseInfo[];
19+
result?: ResponseType;
20+
errors?: FetchResponseInfo[];
2521
messages?: FetchResponseInfo[];
26-
}
22+
};
2723

2824
const isDefined = <T>(
2925
value: T | null | undefined
@@ -220,6 +216,36 @@ const isResponseSchemaV4 = (
220216
return config.BASE.endsWith("/containers");
221217
};
222218

219+
const parseResponseSchemaV4 = <T>(
220+
url: string,
221+
response: Response,
222+
responseHeader: string | undefined,
223+
responseBody: any
224+
): ApiResult => {
225+
const fetchResult =
226+
(typeof responseBody === "object"
227+
? responseBody
228+
: JSON.parse(responseBody)) as FetchResult<T>;
229+
const ok = response.ok && fetchResult.success;
230+
let result: any;
231+
if (ok) {
232+
if (fetchResult.result !== undefined) {
233+
result = fetchResult.result;
234+
} else {
235+
result = {};
236+
}
237+
} else {
238+
result = { error: fetchResult.errors?.[0].message };
239+
}
240+
return {
241+
url,
242+
ok,
243+
status: response.status,
244+
statusText: response.statusText,
245+
body: responseHeader ?? result,
246+
};
247+
};
248+
223249
export const sendRequest = async (
224250
config: OpenAPIConfig,
225251
options: ApiRequestOptions,
@@ -342,26 +368,24 @@ export const request = <T>(
342368
options.responseHeader
343369
);
344370

345-
const parseResponseSchemaV4 = (body: any): ApiResult => {
346-
const fetchResult = body as FetchResult<T>;
347-
return {
371+
let result: ApiResult;
372+
373+
if (isResponseSchemaV4(config, options)) {
374+
result = parseResponseSchemaV4(
348375
url,
349-
ok: response.ok && fetchResult.success,
376+
response,
377+
responseHeader,
378+
responseBody
379+
);
380+
} else {
381+
result = {
382+
url,
383+
ok: response.ok,
350384
status: response.status,
351385
statusText: response.statusText,
352-
body: responseHeader ?? fetchResult.result,
386+
body: responseHeader ?? responseBody,
353387
};
354-
};
355-
356-
const result: ApiResult = isResponseSchemaV4(config, options)
357-
? parseResponseSchemaV4(responseBody)
358-
: {
359-
url,
360-
ok: response.ok,
361-
status: response.status,
362-
statusText: response.statusText,
363-
body: responseHeader ?? responseBody,
364-
};
388+
}
365389

366390
catchErrorCodes(options, result);
367391
resolve(result.body);

packages/wrangler/src/__tests__/cloudchamber/build.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { UserError } from "../../errors";
1414
import { mockAccountId, mockApiToken } from "../helpers/mock-account-id";
1515
import { runInTempDir } from "../helpers/run-in-tmp";
1616
import { runWrangler } from "../helpers/run-wrangler";
17-
import { mockAccount } from "./utils";
17+
import { mockAccountV4 as mockAccount } from "./utils";
1818
import type { CompleteAccountCustomer } from "@cloudflare/containers-shared";
1919

2020
const MiB = 1024 * 1024;

packages/wrangler/src/__tests__/cloudchamber/curl.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,7 @@ describe("cloudchamber curl", () => {
171171
);
172172
expect(std.err).toMatchInlineSnapshot(`""`);
173173
expect(std.out).toMatchInlineSnapshot(`
174-
"├ Loading account
175-
176-
>> Body
174+
">> Body
177175
[
178176
{
179177
\\"id\\": \\"1\\",
@@ -332,8 +330,7 @@ describe("cloudchamber curl", () => {
332330
"cloudchamber curl /deployments/v2 --header something:here"
333331
);
334332
expect(std.err).toMatchInlineSnapshot(`""`);
335-
const text = std.out.split("\n").splice(2).join("\n");
336-
const response = JSON.parse(text);
333+
const response = JSON.parse(std.out);
337334
expect(response.status).toEqual(500);
338335
expect(response.statusText).toEqual("Unhandled Exception");
339336
});

packages/wrangler/src/__tests__/cloudchamber/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,20 @@ export function mockAccount() {
3232
)
3333
);
3434
}
35+
36+
export function mockAccountV4() {
37+
msw.use(
38+
http.get(
39+
"*/me",
40+
async () => {
41+
return HttpResponse.json({ success: true, result: {
42+
external_account_id: "test_account_id",
43+
limits: {
44+
disk_mb_per_deployment: 2000,
45+
},
46+
}}, { type: "application/json" });
47+
},
48+
{ once: true }
49+
)
50+
);
51+
}

packages/wrangler/src/__tests__/containers/delete.test.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { useMockIsTTY } from "../helpers/mock-istty";
77
import { msw } from "../helpers/msw";
88
import { runWrangler } from "../helpers/run-wrangler";
99

10+
const testContainerID = "6925adea-c4ad-4aa6-bffd-d26783e9afbb";
11+
1012
describe("containers delete", () => {
1113
const stdCli = mockCLIOutput();
1214

@@ -52,25 +54,24 @@ describe("containers delete", () => {
5254
"*/applications/:id",
5355
async ({ request }) => {
5456
expect(await request.text()).toEqual("");
55-
return new HttpResponse(`{"error": "something happened"}`, {
56-
status: code,
57-
});
57+
return HttpResponse.json(
58+
`{"success": false, "errors": [{"code": 1000, "message": "something happened"}]}`,
59+
{
60+
status: code,
61+
}
62+
);
5863
},
5964
{ once: true }
6065
)
6166
);
62-
await expect(runWrangler("containers delete 123")).rejects
67+
await expect(runWrangler(`containers delete ${testContainerID}`)).rejects
6368
.toMatchInlineSnapshot(`
6469
[Error: There has been an error deleting the container.
6570
something happened]
6671
`);
6772
expect(stdCli.stderr).toMatchInlineSnapshot(`""`);
6873
expect(stdCli.stdout).toMatchInlineSnapshot(`
69-
"├ Loading account
70-
71-
├ Loading account
72-
73-
╭ Delete your container
74+
"╭ Delete your container
7475
7576
"
7677
`);
@@ -86,25 +87,22 @@ describe("containers delete", () => {
8687
"*/applications/:id",
8788
async ({ request }) => {
8889
expect(await request.text()).toEqual("");
89-
return new HttpResponse(`{"error": "something happened"}`, {
90+
return new HttpResponse(`{"success": false, "errors": [{"code": 1000, "message": "something happened"}]}`, {
91+
type: "applicaton/json",
9092
status: 500,
9193
});
9294
},
9395
{ once: true }
9496
)
9597
);
96-
await expect(runWrangler("containers delete 123")).rejects
98+
await expect(runWrangler(`containers delete ${testContainerID}`)).rejects
9799
.toMatchInlineSnapshot(`
98100
[Error: There has been an unknown error deleting the container.
99-
"{/"error/": /"something happened/"}"]
101+
{"error":"something happened"}]
100102
`);
101103
expect(stdCli.stderr).toMatchInlineSnapshot(`""`);
102104
expect(stdCli.stdout).toMatchInlineSnapshot(`
103-
"├ Loading account
104-
105-
├ Loading account
106-
107-
╭ Delete your container
105+
"╭ Delete your container
108106
109107
"
110108
`);
@@ -117,19 +115,15 @@ describe("containers delete", () => {
117115
"*/applications/:id",
118116
async ({ request }) => {
119117
expect(await request.text()).toEqual("");
120-
return new HttpResponse("{}");
118+
return new HttpResponse(`{"success": true, "result": {}}`, {type: "application/json"});
121119
},
122120
{ once: true }
123121
)
124122
);
125-
await runWrangler("containers delete 123");
123+
await runWrangler(`containers delete ${testContainerID}`);
126124
expect(stdCli.stderr).toMatchInlineSnapshot(`""`);
127125
expect(stdCli.stdout).toMatchInlineSnapshot(`
128-
"├ Loading account
129-
130-
├ Loading account
131-
132-
╭ Delete your container
126+
"╭ Delete your container
133127
134128
╰ Your container has been deleted
135129
@@ -145,14 +139,14 @@ describe("containers delete", () => {
145139
"*/applications/:id",
146140
async ({ request }) => {
147141
expect(await request.text()).toEqual("");
148-
return new HttpResponse("{}");
142+
return HttpResponse.json(`{"success": true, "result": {}}`);
149143
},
150144
{ once: true }
151145
)
152146
);
153-
await runWrangler("containers delete --json asdf");
147+
await runWrangler(`containers delete --json ${testContainerID}`);
154148
expect(std.err).toMatchInlineSnapshot(`""`);
155-
expect(std.out).toMatchInlineSnapshot(`"\\"{}\\""`);
149+
expect(std.out).toMatchInlineSnapshot(`"{}"`);
156150
});
157151

158152
it("should error when trying to delete a non-existant container (json)", async () => {
@@ -163,17 +157,23 @@ describe("containers delete", () => {
163157
"*/applications/*",
164158
async ({ request }) => {
165159
expect(await request.text()).toEqual("");
166-
return new HttpResponse(JSON.stringify({ error: "Not Found" }), {
167-
status: 404,
168-
});
160+
return new HttpResponse(
161+
JSON.stringify({
162+
success: false,
163+
errors: [{ code: 1000, message: "Not Found" }],
164+
}),
165+
{
166+
status: 404,
167+
}
168+
);
169169
},
170170
{ once: true }
171171
)
172172
);
173173
expect(std.err).toMatchInlineSnapshot(`""`);
174-
await runWrangler("containers delete --json nope");
174+
await runWrangler(`containers delete --json ${testContainerID}`);
175175
expect(std.out).toMatchInlineSnapshot(
176-
`"\\"{/\\"error/\\":/\\"Not Found/\\"}\\""`
176+
`"{\\"error\\":\\"Not Found\\"}"`
177177
);
178178
});
179179
});

packages/wrangler/src/__tests__/containers/info.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe("containers info", () => {
5353
"*/applications/asdf",
5454
async ({ request }) => {
5555
expect(await request.text()).toEqual("");
56-
return HttpResponse.json(MOCK_APPLICATION_SINGLE);
56+
return HttpResponse.json(`{"success": true, "result": ${MOCK_APPLICATION_SINGLE}}`);
5757
},
5858
{ once: true }
5959
)

packages/wrangler/src/__tests__/containers/list.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe("containers list", () => {
4949
"*/applications",
5050
async ({ request }) => {
5151
expect(await request.text()).toEqual("");
52-
return HttpResponse.json(MOCK_APPLICATIONS);
52+
return HttpResponse.json(`{"success": true, "result": ${JSON.stringify(MOCK_APPLICATIONS, null, 4)}}`);
5353
},
5454
{ once: true }
5555
)

packages/wrangler/src/__tests__/deploy.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
import { clearOutputFilePath } from "../output";
2323
import { sniffUserAgent } from "../package-manager";
2424
import { writeAuthConfigFile } from "../user";
25-
import { mockAccount as mockContainersAccount } from "./cloudchamber/utils";
25+
import { mockAccountV4 as mockContainersAccount } from "./cloudchamber/utils";
2626
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
2727
import { mockAuthDomain } from "./helpers/mock-auth-domain";
2828
import { mockConsoleMethods } from "./helpers/mock-console";
@@ -93,7 +93,7 @@ function mockGetApplications(applications: Application[]) {
9393
http.get(
9494
"*/applications",
9595
async () => {
96-
return HttpResponse.json(applications);
96+
return HttpResponse.json({success: true, result: applications});
9797
},
9898
{ once: true }
9999
)
@@ -583,7 +583,7 @@ describe("deploy", () => {
583583

584584
expect(std.out).toMatchInlineSnapshot(`
585585
"Attempting to login via OAuth...
586-
Opening a link in your default browser: https://dash.cloudflare.com/oauth2/auth?response_type=code&client_id=54d11594-84e4-41aa-b438-e81b8fa78ee7&redirect_uri=http%3A%2F%2Flocalhost%3A8976%2Foauth%2Fcallback&scope=account%3Aread%20user%3Aread%20workers%3Awrite%20workers_kv%3Awrite%20workers_routes%3Awrite%20workers_scripts%3Awrite%20workers_tail%3Aread%20d1%3Awrite%20pages%3Awrite%20zone%3Aread%20ssl_certs%3Awrite%20ai%3Awrite%20queues%3Awrite%20pipelines%3Awrite%20secrets_store%3Awrite%20offline_access&state=MOCK_STATE_PARAM&code_challenge=MOCK_CODE_CHALLENGE&code_challenge_method=S256
586+
Opening a link in your default browser: https://dash.cloudflare.com/oauth2/auth?response_type=code&client_id=54d11594-84e4-41aa-b438-e81b8fa78ee7&redirect_uri=http%3A%2F%2Flocalhost%3A8976%2Foauth%2Fcallback&scope=account%3Aread%20user%3Aread%20workers%3Awrite%20workers_kv%3Awrite%20workers_routes%3Awrite%20workers_scripts%3Awrite%20workers_tail%3Aread%20d1%3Awrite%20pages%3Awrite%20zone%3Aread%20ssl_certs%3Awrite%20ai%3Awrite%20queues%3Awrite%20pipelines%3Awrite%20secrets_store%3Awrite%20containers%3Awrite%20cloudchamber%3Awrite%20offline_access&state=MOCK_STATE_PARAM&code_challenge=MOCK_CODE_CHALLENGE&code_challenge_method=S256
587587
Successfully logged in.
588588
Total Upload: xx KiB / gzip: xx KiB
589589
Worker Startup Time: 100 ms
@@ -624,7 +624,7 @@ describe("deploy", () => {
624624

625625
expect(std.out).toMatchInlineSnapshot(`
626626
"Attempting to login via OAuth...
627-
Opening a link in your default browser: https://dash.staging.cloudflare.com/oauth2/auth?response_type=code&client_id=54d11594-84e4-41aa-b438-e81b8fa78ee7&redirect_uri=http%3A%2F%2Flocalhost%3A8976%2Foauth%2Fcallback&scope=account%3Aread%20user%3Aread%20workers%3Awrite%20workers_kv%3Awrite%20workers_routes%3Awrite%20workers_scripts%3Awrite%20workers_tail%3Aread%20d1%3Awrite%20pages%3Awrite%20zone%3Aread%20ssl_certs%3Awrite%20ai%3Awrite%20queues%3Awrite%20pipelines%3Awrite%20secrets_store%3Awrite%20offline_access&state=MOCK_STATE_PARAM&code_challenge=MOCK_CODE_CHALLENGE&code_challenge_method=S256
627+
Opening a link in your default browser: https://dash.staging.cloudflare.com/oauth2/auth?response_type=code&client_id=54d11594-84e4-41aa-b438-e81b8fa78ee7&redirect_uri=http%3A%2F%2Flocalhost%3A8976%2Foauth%2Fcallback&scope=account%3Aread%20user%3Aread%20workers%3Awrite%20workers_kv%3Awrite%20workers_routes%3Awrite%20workers_scripts%3Awrite%20workers_tail%3Aread%20d1%3Awrite%20pages%3Awrite%20zone%3Aread%20ssl_certs%3Awrite%20ai%3Awrite%20queues%3Awrite%20pipelines%3Awrite%20secrets_store%3Awrite%20containers%3Awrite%20cloudchamber%3Awrite%20offline_access&state=MOCK_STATE_PARAM&code_challenge=MOCK_CODE_CHALLENGE&code_challenge_method=S256
628628
Successfully logged in.
629629
Total Upload: xx KiB / gzip: xx KiB
630630
Worker Startup Time: 100 ms
@@ -8913,7 +8913,7 @@ addEventListener('fetch', event => {});`
89138913
expect(json).toMatchObject(expected);
89148914
}
89158915

8916-
return HttpResponse.json(json);
8916+
return HttpResponse.json({success: true, result: json});
89178917
},
89188918
{ once: true }
89198919
)
@@ -8929,12 +8929,12 @@ addEventListener('fetch', event => {});`
89298929
(await request.json()) as ImageRegistryCredentialsConfiguration;
89308930
expect(json.permissions).toEqual(["push", "pull"]);
89318931

8932-
return HttpResponse.json({
8932+
return HttpResponse.json({success: true, result: {
89338933
account_id: "test_account_id",
89348934
registry_host: getCloudflareContainerRegistry(),
89358935
username: "v1",
89368936
password: "mockpassword",
8937-
} as AccountRegistryToken);
8937+
} as AccountRegistryToken});
89388938
},
89398939
{ once: true }
89408940
)
@@ -9121,7 +9121,7 @@ addEventListener('fetch', event => {});`
91219121
expect(json).toMatchObject(expected);
91229122
}
91239123

9124-
return HttpResponse.json(json);
9124+
return HttpResponse.json({success: true, result: json});
91259125
},
91269126
{ once: true }
91279127
)

0 commit comments

Comments
 (0)