Skip to content

Commit 0b3544d

Browse files
authored
Merge pull request #2806 from Azure/dpwatrous/mock-http-bodies
Minor MockHttpClient improvements
2 parents 5867065 + 779c640 commit 0b3544d

2 files changed

Lines changed: 84 additions & 12 deletions

File tree

packages/bonito-core/src/http/__tests__/mock-http-client.spec.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,25 @@ describe("MockHttpClient", () => {
106106
);
107107
});
108108

109+
test("response URL and request URL can be different", async () => {
110+
const client = new MockHttpClient();
111+
112+
client.addExpected(
113+
new MockHttpResponse("https://contoso.net/foobar", {
114+
status: 200,
115+
body: "foobaz",
116+
}),
117+
{
118+
method: "GET",
119+
url: "/foobar",
120+
}
121+
);
122+
123+
const response = await client.fetch("/foobar");
124+
expect(response.url).toBe("https://contoso.net/foobar");
125+
expect(await response.text()).toBe("foobaz");
126+
});
127+
109128
test("can mock all methods", async () => {
110129
const client = new MockHttpClient();
111130

@@ -222,4 +241,45 @@ describe("MockHttpClient", () => {
222241
await (await client.put("/some/url", { body: "two" })).text()
223242
).toBe("response two");
224243
});
244+
245+
test("supports sending string bodies", async () => {
246+
const client = new MockHttpClient();
247+
client.addExpected(
248+
new MockHttpResponse("/some/url", {
249+
status: 200,
250+
}),
251+
{
252+
method: "PUT",
253+
body: "this is a string",
254+
}
255+
);
256+
const response = await client.put("/some/url", {
257+
body: "this is a string",
258+
});
259+
expect(response.status).toBe(200);
260+
});
261+
262+
test("supports sending URLSearchParams bodies", async () => {
263+
const client = new MockHttpClient();
264+
265+
const body = new URLSearchParams({
266+
color: "red",
267+
shape: "triangle",
268+
});
269+
expect(client.serializeBody(body)).toBe("color=red&shape=triangle");
270+
271+
client.addExpected(
272+
new MockHttpResponse("/some/url", {
273+
status: 200,
274+
}),
275+
{
276+
method: "PUT",
277+
body,
278+
}
279+
);
280+
const response = await client.put("/some/url", {
281+
body,
282+
});
283+
expect(response.status).toBe(200);
284+
});
225285
});

packages/bonito-core/src/http/mock-http-client.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ export class MockHttpClient extends AbstractHttpClient {
8585
response: MockHttpResponse,
8686
requestProps: HttpRequestInit = {}
8787
): MockHttpClient {
88-
const key = this._getKeyFromRequest(response.url, requestProps, true);
88+
const key = this._getKeyFromRequest(
89+
requestProps.url ?? response.url,
90+
requestProps,
91+
true
92+
);
8993
const expected = this._expectedResponses[key] ?? [];
9094
expected.push(response);
9195
this._expectedResponses[key] = expected;
@@ -101,29 +105,24 @@ export class MockHttpClient extends AbstractHttpClient {
101105
const method = requestProps.method ?? HttpRequestMethod.Get;
102106

103107
let bodyId: string = "";
104-
const reqBody = requestProps.body;
105-
if (reqBody) {
106-
if (typeof reqBody !== "string") {
107-
throw new Error(
108-
"MockHttpClient only supports string request bodies"
109-
);
110-
}
111-
if (this._expectedRequestBodies[reqBody]) {
108+
if (requestProps.body) {
109+
const bodyString = this.serializeBody(requestProps.body);
110+
if (this._expectedRequestBodies[bodyString]) {
112111
// Existing body identifier
113-
const expectedBody = this._expectedRequestBodies[reqBody];
112+
const expectedBody = this._expectedRequestBodies[bodyString];
114113
if (updateBodyIds) {
115114
expectedBody.refCount++;
116115
}
117116
bodyId = expectedBody.bodyId;
118117
} else if (updateBodyIds) {
119118
// New body identifier
120119
bodyId = `body${this._bodyIdCounter++}`;
121-
this._expectedRequestBodies[reqBody] = {
120+
this._expectedRequestBodies[bodyString] = {
122121
refCount: 1,
123122
bodyId: bodyId,
124123
};
125124
} else {
126-
throw new Error(`Unexpected request body: ${reqBody}`);
125+
throw new Error(`Unexpected request body: ${bodyString}`);
127126
}
128127
}
129128

@@ -139,6 +138,19 @@ export class MockHttpClient extends AbstractHttpClient {
139138
(key) => key.split("::")[1]
140139
);
141140
}
141+
142+
serializeBody(
143+
body: string | Blob | BufferSource | FormData | URLSearchParams
144+
): string {
145+
if (typeof body === "string") {
146+
return body;
147+
} else if (body instanceof URLSearchParams) {
148+
return body.toString();
149+
}
150+
throw new Error(
151+
"Unsupported request body type: MockHttpClient supports bodies of type string or URLSearchParams"
152+
);
153+
}
142154
}
143155

144156
/**

0 commit comments

Comments
 (0)