Skip to content

Commit

Permalink
Merge pull request #2806 from Azure/dpwatrous/mock-http-bodies
Browse files Browse the repository at this point in the history
Minor MockHttpClient improvements
  • Loading branch information
dpwatrous committed Sep 25, 2023
2 parents 5867065 + 779c640 commit 0b3544d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 12 deletions.
60 changes: 60 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 @@ -106,6 +106,25 @@ describe("MockHttpClient", () => {
);
});

test("response URL and request URL can be different", async () => {
const client = new MockHttpClient();

client.addExpected(
new MockHttpResponse("https://contoso.net/foobar", {
status: 200,
body: "foobaz",
}),
{
method: "GET",
url: "/foobar",
}
);

const response = await client.fetch("/foobar");
expect(response.url).toBe("https://contoso.net/foobar");
expect(await response.text()).toBe("foobaz");
});

test("can mock all methods", async () => {
const client = new MockHttpClient();

Expand Down Expand Up @@ -222,4 +241,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);
});
});
36 changes: 24 additions & 12 deletions packages/bonito-core/src/http/mock-http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ export class MockHttpClient extends AbstractHttpClient {
response: MockHttpResponse,
requestProps: HttpRequestInit = {}
): MockHttpClient {
const key = this._getKeyFromRequest(response.url, requestProps, true);
const key = this._getKeyFromRequest(
requestProps.url ?? response.url,
requestProps,
true
);
const expected = this._expectedResponses[key] ?? [];
expected.push(response);
this._expectedResponses[key] = expected;
Expand All @@ -101,29 +105,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 +138,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 0b3544d

Please sign in to comment.