Skip to content

Commit a594b0d

Browse files
authored
Merge pull request #12 from cloudnc/feat-allow-to-pass-promise-as-response
feat: allow to pass promise as response
2 parents fed66fe + 1879753 commit a594b0d

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/playwright/mock-grpc-unary.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@ import { readGrpcRequest } from './read-grpc-request';
66
export async function mockGrpcUnary(
77
page: Page,
88
rpc: UnaryMethodDefinitionish,
9-
response: GrpcResponse | ((request: Uint8Array | null) => GrpcResponse),
9+
response:
10+
| GrpcResponse
11+
| Promise<GrpcResponse>
12+
| ((request: Uint8Array | null) => GrpcResponse | Promise<GrpcResponse>),
1013
mockAtContextLevel: boolean = false,
1114
): Promise<MockedGrpcCall> {
1215
const url = `/${rpc.service.serviceName}/${rpc.methodName}`;
1316

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

18-
const grpcResponse = typeof response === 'function' ? response(readGrpcRequest(route.request())) : response;
21+
const grpcResponseWrapped = typeof response === 'function' ? response(readGrpcRequest(route.request())) : response;
22+
const grpcResponse = await Promise.resolve(grpcResponseWrapped);
1923

2024
const grpcResponseBody = grpcResponseToBuffer(grpcResponse);
2125

22-
return route.fulfill({
26+
return await route.fulfill({
2327
body: grpcResponseBody,
2428
contentType: 'application/grpc-web+proto',
2529
headers: {

0 commit comments

Comments
 (0)