Skip to content

Commit 9b10a09

Browse files
committed
Fix MSW JSON responses
1 parent 3c10e87 commit 9b10a09

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

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

+20-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ import {
1414

1515
const baseUrl = "https://api.github.com";
1616

17+
const jsonResponse = <T>(
18+
body: T,
19+
init?: ResponseInit,
20+
contentType = "application/json",
21+
): Response => {
22+
return new Response(JSON.stringify(body), {
23+
...init,
24+
headers: {
25+
"Content-Type": contentType,
26+
...init?.headers,
27+
},
28+
});
29+
};
30+
1731
export async function createRequestHandlers(
1832
scenarioDirPath: string,
1933
): Promise<RequestHandler[]> {
@@ -81,7 +95,7 @@ function createGetRepoRequestHandler(
8195
const getRepoRequest = getRepoRequests[0];
8296

8397
return rest.get(`${baseUrl}/repos/:owner/:name`, () => {
84-
return new Response(JSON.stringify(getRepoRequest.response.body), {
98+
return jsonResponse(getRepoRequest.response.body, {
8599
status: getRepoRequest.response.status,
86100
});
87101
});
@@ -103,7 +117,7 @@ function createSubmitVariantAnalysisRequestHandler(
103117
return rest.post(
104118
`${baseUrl}/repositories/:controllerRepoId/code-scanning/codeql/variant-analyses`,
105119
() => {
106-
return new Response(JSON.stringify(getRepoRequest.response.body), {
120+
return jsonResponse(getRepoRequest.response.body, {
107121
status: getRepoRequest.response.status,
108122
});
109123
},
@@ -131,7 +145,7 @@ function createGetVariantAnalysisRequestHandler(
131145
requestIndex++;
132146
}
133147

134-
return new Response(JSON.stringify(request.response.body), {
148+
return jsonResponse(request.response.body, {
135149
status: request.response.status,
136150
});
137151
},
@@ -155,7 +169,7 @@ function createGetVariantAnalysisRepoRequestHandler(
155169
throw Error(`No scenario request found for ${request.url}`);
156170
}
157171

158-
return new Response(JSON.stringify(scenarioRequest.response.body), {
172+
return jsonResponse(scenarioRequest.response.body, {
159173
status: scenarioRequest.response.status,
160174
});
161175
},
@@ -210,7 +224,7 @@ function createCodeSearchRequestHandler(
210224
requestIndex++;
211225
}
212226

213-
return new Response(JSON.stringify(request.response.body), {
227+
return jsonResponse(request.response.body, {
214228
status: request.response.status,
215229
});
216230
});
@@ -235,7 +249,7 @@ function createAutoModelRequestHandler(
235249
requestIndex++;
236250
}
237251

238-
return new Response(JSON.stringify(request.response.body), {
252+
return jsonResponse(request.response.body, {
239253
status: request.response.status,
240254
});
241255
},

0 commit comments

Comments
 (0)