Skip to content

Commit 47a450a

Browse files
committed
Add support for mocking URLSearchParams bodies
1 parent 5867065 commit 47a450a

2 files changed

Lines changed: 60 additions & 11 deletions

File tree

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,45 @@ describe("MockHttpClient", () => {
222222
await (await client.put("/some/url", { body: "two" })).text()
223223
).toBe("response two");
224224
});
225+
226+
test("supports sending string bodies", async () => {
227+
const client = new MockHttpClient();
228+
client.addExpected(
229+
new MockHttpResponse("/some/url", {
230+
status: 200,
231+
}),
232+
{
233+
method: "PUT",
234+
body: "this is a string",
235+
}
236+
);
237+
const response = await client.put("/some/url", {
238+
body: "this is a string",
239+
});
240+
expect(response.status).toBe(200);
241+
});
242+
243+
test("supports sending URLSearchParams bodies", async () => {
244+
const client = new MockHttpClient();
245+
246+
const body = new URLSearchParams({
247+
color: "red",
248+
shape: "triangle",
249+
});
250+
expect(client.serializeBody(body)).toBe("color=red&shape=triangle");
251+
252+
client.addExpected(
253+
new MockHttpResponse("/some/url", {
254+
status: 200,
255+
}),
256+
{
257+
method: "PUT",
258+
body,
259+
}
260+
);
261+
const response = await client.put("/some/url", {
262+
body,
263+
});
264+
expect(response.status).toBe(200);
265+
});
225266
});

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,29 +101,24 @@ export class MockHttpClient extends AbstractHttpClient {
101101
const method = requestProps.method ?? HttpRequestMethod.Get;
102102

103103
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]) {
104+
if (requestProps.body) {
105+
const bodyString = this.serializeBody(requestProps.body);
106+
if (this._expectedRequestBodies[bodyString]) {
112107
// Existing body identifier
113-
const expectedBody = this._expectedRequestBodies[reqBody];
108+
const expectedBody = this._expectedRequestBodies[bodyString];
114109
if (updateBodyIds) {
115110
expectedBody.refCount++;
116111
}
117112
bodyId = expectedBody.bodyId;
118113
} else if (updateBodyIds) {
119114
// New body identifier
120115
bodyId = `body${this._bodyIdCounter++}`;
121-
this._expectedRequestBodies[reqBody] = {
116+
this._expectedRequestBodies[bodyString] = {
122117
refCount: 1,
123118
bodyId: bodyId,
124119
};
125120
} else {
126-
throw new Error(`Unexpected request body: ${reqBody}`);
121+
throw new Error(`Unexpected request body: ${bodyString}`);
127122
}
128123
}
129124

@@ -139,6 +134,19 @@ export class MockHttpClient extends AbstractHttpClient {
139134
(key) => key.split("::")[1]
140135
);
141136
}
137+
138+
serializeBody(
139+
body: string | Blob | BufferSource | FormData | URLSearchParams
140+
): string {
141+
if (typeof body === "string") {
142+
return body;
143+
} else if (body instanceof URLSearchParams) {
144+
return body.toString();
145+
}
146+
throw new Error(
147+
"Unsupported request body type: MockHttpClient supports bodies of type string or URLSearchParams"
148+
);
149+
}
142150
}
143151

144152
/**

0 commit comments

Comments
 (0)