Skip to content

Commit

Permalink
Add support for mocking URLSearchParams bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
dpwatrous committed Sep 23, 2023
1 parent 5867065 commit 47a450a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
41 changes: 41 additions & 0 deletions packages/bonito-core/src/http/__tests__/mock-http-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,45 @@ describe("MockHttpClient", () => {
await (await client.put("/some/url", { body: "two" })).text()
).toBe("response two");
});

test("supports sending string bodies", async () => {
const client = new MockHttpClient();
client.addExpected(
new MockHttpResponse("/some/url", {
status: 200,
}),
{
method: "PUT",
body: "this is a string",
}
);
const response = await client.put("/some/url", {
body: "this is a string",
});
expect(response.status).toBe(200);
});

test("supports sending URLSearchParams bodies", async () => {
const client = new MockHttpClient();

const body = new URLSearchParams({
color: "red",
shape: "triangle",
});
expect(client.serializeBody(body)).toBe("color=red&shape=triangle");

client.addExpected(
new MockHttpResponse("/some/url", {
status: 200,
}),
{
method: "PUT",
body,
}
);
const response = await client.put("/some/url", {
body,
});
expect(response.status).toBe(200);
});
});
30 changes: 19 additions & 11 deletions packages/bonito-core/src/http/mock-http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,24 @@ export class MockHttpClient extends AbstractHttpClient {
const method = requestProps.method ?? HttpRequestMethod.Get;

let bodyId: string = "";
const reqBody = requestProps.body;
if (reqBody) {
if (typeof reqBody !== "string") {
throw new Error(
"MockHttpClient only supports string request bodies"
);
}
if (this._expectedRequestBodies[reqBody]) {
if (requestProps.body) {
const bodyString = this.serializeBody(requestProps.body);
if (this._expectedRequestBodies[bodyString]) {
// Existing body identifier
const expectedBody = this._expectedRequestBodies[reqBody];
const expectedBody = this._expectedRequestBodies[bodyString];
if (updateBodyIds) {
expectedBody.refCount++;
}
bodyId = expectedBody.bodyId;
} else if (updateBodyIds) {
// New body identifier
bodyId = `body${this._bodyIdCounter++}`;
this._expectedRequestBodies[reqBody] = {
this._expectedRequestBodies[bodyString] = {
refCount: 1,
bodyId: bodyId,
};
} else {
throw new Error(`Unexpected request body: ${reqBody}`);
throw new Error(`Unexpected request body: ${bodyString}`);
}
}

Expand All @@ -139,6 +134,19 @@ export class MockHttpClient extends AbstractHttpClient {
(key) => key.split("::")[1]
);
}

serializeBody(
body: string | Blob | BufferSource | FormData | URLSearchParams
): string {
if (typeof body === "string") {
return body;
} else if (body instanceof URLSearchParams) {
return body.toString();
}
throw new Error(
"Unsupported request body type: MockHttpClient supports bodies of type string or URLSearchParams"
);
}
}

/**
Expand Down

0 comments on commit 47a450a

Please sign in to comment.