Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor MockHttpClient improvements #2806

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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