Skip to content

Commit 7578697

Browse files
committed
Fix response bodies when recording
1 parent 40b79f2 commit 7578697

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

extensions/ql-vscode/src/common/mock-gh-api/gh-api-request.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export enum RequestKind {
1717
AutoModel = "autoModel",
1818
}
1919

20-
interface BasicErorResponse {
20+
export interface BasicErorResponse {
2121
message: string;
2222
}
2323

@@ -74,7 +74,7 @@ export interface GetVariantAnalysisRepoResultRequest {
7474
};
7575
}
7676

77-
interface CodeSearchResponse {
77+
export interface CodeSearchResponse {
7878
total_count: number;
7979
items: Array<{
8080
repository: Repository;
@@ -92,7 +92,7 @@ interface CodeSearchRequest {
9292
};
9393
}
9494

95-
interface AutoModelResponse {
95+
export interface AutoModelResponse {
9696
models: string;
9797
}
9898

extensions/ql-vscode/src/common/mock-gh-api/recorder.ts

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@ import { join } from "path";
44
import { SetupServer } from "msw/node";
55

66
import { DisposableObject } from "../disposable-object";
7+
import { gzipDecode } from "../zlib";
78

89
import {
10+
AutoModelResponse,
11+
BasicErorResponse,
12+
CodeSearchResponse,
913
GetVariantAnalysisRepoResultRequest,
1014
GitHubApiRequest,
1115
RequestKind,
1216
} from "./gh-api-request";
17+
import {
18+
VariantAnalysis,
19+
VariantAnalysisRepoTask,
20+
} from "../../variant-analysis/gh-api/variant-analysis";
21+
import { Repository } from "../../variant-analysis/gh-api/repository";
1322

1423
export class Recorder extends DisposableObject {
1524
private currentRecordedScenario: GitHubApiRequest[] = [];
@@ -81,7 +90,7 @@ export class Recorder extends DisposableObject {
8190

8291
let bodyFileLink = undefined;
8392
if (writtenRequest.response.body) {
84-
await writeFile(bodyFilePath, writtenRequest.response.body || "");
93+
await writeFile(bodyFilePath, writtenRequest.response.body);
8594
bodyFileLink = `file:${bodyFileName}`;
8695
}
8796

@@ -137,7 +146,9 @@ async function createGitHubApiRequest(
137146
},
138147
response: {
139148
status,
140-
body: await response.json(),
149+
body: await jsonResponseBody<
150+
Repository | BasicErorResponse | undefined
151+
>(response),
141152
},
142153
};
143154
}
@@ -151,7 +162,9 @@ async function createGitHubApiRequest(
151162
},
152163
response: {
153164
status,
154-
body: await response.json(),
165+
body: await jsonResponseBody<
166+
VariantAnalysis | BasicErorResponse | undefined
167+
>(response),
155168
},
156169
};
157170
}
@@ -167,7 +180,9 @@ async function createGitHubApiRequest(
167180
},
168181
response: {
169182
status,
170-
body: await response.json(),
183+
body: await jsonResponseBody<
184+
VariantAnalysis | BasicErorResponse | undefined
185+
>(response),
171186
},
172187
};
173188
}
@@ -183,7 +198,9 @@ async function createGitHubApiRequest(
183198
},
184199
response: {
185200
status,
186-
body: await response.json(),
201+
body: await jsonResponseBody<
202+
VariantAnalysisRepoTask | BasicErorResponse | undefined
203+
>(response),
187204
},
188205
};
189206
}
@@ -200,7 +217,7 @@ async function createGitHubApiRequest(
200217
},
201218
response: {
202219
status,
203-
body: Buffer.from(await response.arrayBuffer()),
220+
body: Buffer.from(await responseBody(response)),
204221
contentType: headers.get("content-type") ?? "application/octet-stream",
205222
},
206223
};
@@ -215,7 +232,9 @@ async function createGitHubApiRequest(
215232
},
216233
response: {
217234
status,
218-
body: await response.json(),
235+
body: await jsonResponseBody<
236+
CodeSearchResponse | BasicErorResponse | undefined
237+
>(response),
219238
},
220239
};
221240
}
@@ -230,14 +249,36 @@ async function createGitHubApiRequest(
230249
},
231250
response: {
232251
status,
233-
body: await response.json(),
252+
body: await jsonResponseBody<
253+
BasicErorResponse | AutoModelResponse | undefined
254+
>(response),
234255
},
235256
};
236257
}
237258

238259
return undefined;
239260
}
240261

262+
async function responseBody(response: Response): Promise<Uint8Array> {
263+
const body = await response.arrayBuffer();
264+
const view = new Uint8Array(body);
265+
266+
if (view[0] === 0x1f && view[1] === 0x8b) {
267+
// Response body is gzipped, so we need to un-gzip it.
268+
269+
return await gzipDecode(view);
270+
} else {
271+
return view;
272+
}
273+
}
274+
275+
async function jsonResponseBody<T>(response: Response): Promise<T> {
276+
const body = await responseBody(response);
277+
const text = new TextDecoder("utf-8").decode(body);
278+
279+
return JSON.parse(text);
280+
}
281+
241282
function shouldWriteBodyToFile(
242283
request: GitHubApiRequest,
243284
): request is GetVariantAnalysisRepoResultRequest {

0 commit comments

Comments
 (0)