Skip to content

Commit

Permalink
feat: allow to pass a promise as a response
Browse files Browse the repository at this point in the history
  • Loading branch information
maxime1992 committed Dec 18, 2023
1 parent fed66fe commit 1879753
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/playwright/mock-grpc-unary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@ import { readGrpcRequest } from './read-grpc-request';
export async function mockGrpcUnary(
page: Page,
rpc: UnaryMethodDefinitionish,
response: GrpcResponse | ((request: Uint8Array | null) => GrpcResponse),
response:
| GrpcResponse
| Promise<GrpcResponse>
| ((request: Uint8Array | null) => GrpcResponse | Promise<GrpcResponse>),
mockAtContextLevel: boolean = false,
): Promise<MockedGrpcCall> {
const url = `/${rpc.service.serviceName}/${rpc.methodName}`;

// note this wildcard route url base is done in order to match both localhost and deployed service usages.
await (mockAtContextLevel ? page.context() : page).route('**' + url, (route) => {
await (mockAtContextLevel ? page.context() : page).route('**' + url, async (route) => {
expect(route.request().method(), 'ALL gRPC requests should be a POST request').toBe('POST');

const grpcResponse = typeof response === 'function' ? response(readGrpcRequest(route.request())) : response;
const grpcResponseWrapped = typeof response === 'function' ? response(readGrpcRequest(route.request())) : response;
const grpcResponse = await Promise.resolve(grpcResponseWrapped);

const grpcResponseBody = grpcResponseToBuffer(grpcResponse);

return route.fulfill({
return await route.fulfill({
body: grpcResponseBody,
contentType: 'application/grpc-web+proto',
headers: {
Expand Down

0 comments on commit 1879753

Please sign in to comment.