Skip to content

Commit

Permalink
feat: add optional request field to Response object
Browse files Browse the repository at this point in the history
tests: add test for `request` field in `Response` object
  • Loading branch information
uid11 committed Nov 8, 2023
1 parent 66acbf6 commit b910ea6
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 18 deletions.
13 changes: 12 additions & 1 deletion autotests/tests/waitForResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ it(

void addUser();

const response = await waitForResponse(
let response = await waitForResponse(
({responseBody}: Response<Body>) => responseBody?.name === 'John',
);

Expand All @@ -38,5 +38,16 @@ it(
},
() => undefined,
);

void addUser();

response = await waitForResponse(
({request}: Response<Body>) => request?.url === 'https://reqres.in/api/users',
);

await expect(response.responseBody, 'second response has correct body').contains({
job: 'leader',
name: 'John',
});
},
);
2 changes: 1 addition & 1 deletion src/constants/pages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* Unique token for creating page instances.
*/
export const CREATE_PAGE_TOKEN = Symbol('Unique token for creating page instances');
export const CREATE_PAGE_TOKEN = Symbol('e2ed:CREATE_PAGE_TOKEN');
4 changes: 2 additions & 2 deletions src/constants/requestHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export const INCLUDE_HEADERS_IN_RESPONSE_EVENT = {includeHeaders: true} as const
/**
* Key for id of TestCafe's request hook context.
*/
export const REQUEST_HOOK_CONTEXT_ID_KEY = Symbol('Key for id of request hook context');
export const REQUEST_HOOK_CONTEXT_ID_KEY = Symbol('e2ed:REQUEST_HOOK_CONTEXT_ID_KEY');

/**
* Key for TestCafe's request hook context on request hook events.
*/
export const REQUEST_HOOK_CONTEXT_KEY = Symbol('Key for request hook context');
export const REQUEST_HOOK_CONTEXT_KEY = Symbol('e2ed:REQUEST_HOOK_CONTEXT_KEY');
2 changes: 1 addition & 1 deletion src/constants/selector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* Key for string description of Selector.
*/
export const DESCRIPTION_KEY = Symbol.for('e2ed:Key for string description of Selector');
export const DESCRIPTION_KEY = Symbol.for('e2ed:DESCRIPTION_KEY');
4 changes: 3 additions & 1 deletion src/types/http/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ export type Request<
}>;

/**
* HTTP response object.
* HTTP response object with its corresponding request..
*/
export type Response<
ResponseBody = unknown,
ResponseHeaders extends Headers = Headers,
SomeStatusCode extends StatusCode = StatusCode,
SomeRequest extends Request = Request,
> = Readonly<{
request?: SomeRequest;
responseBody: ResponseBody;
responseHeaders: ResponseHeaders;
statusCode: SomeStatusCode;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/report/client/sanitizeHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function createSafeHtmlWithoutSanitize(
stringParts: readonly string[],
...values: readonly unknown[]
): SafeHtml {
const key = Symbol.for('e2ed:SafeHtml key');
const key = Symbol.for('e2ed:SafeHtml:key');
const parts: string[] = [];

for (let index = 0; index < values.length; index += 1) {
Expand Down Expand Up @@ -50,7 +50,7 @@ export function sanitizeHtml(
stringParts: readonly string[],
...values: readonly unknown[]
): SafeHtml {
const key = Symbol.for('e2ed:SafeHtml key');
const key = Symbol.for('e2ed:SafeHtml:key');

const sanitizeValue = (value: unknown): string =>
String(value)
Expand Down
22 changes: 15 additions & 7 deletions src/utils/requestHooks/RequestHookToWaitForEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import {
} from '../../constants/internal';

import {assertValueIsDefined} from '../asserts';
import {setReadonlyProperty} from '../setReadonlyProperty';
import {addNotCompleteRequest, completeRequest, processEventsPredicates} from '../waitForEvents';

import {getRequestFromRequestOptions} from './getRequestFromRequestOptions';
import {getResponseFromResponseEvent} from './getResponseFromResponseEvent';
import {RequestHookWithEvents} from './RequestHookWithEvents';

import type {
Request,
RequestHookConfigureResponseEvent,
RequestHookContextId,
RequestHookRequestEvent,
Expand Down Expand Up @@ -56,21 +58,27 @@ export class RequestHookToWaitForEvents extends RequestHookWithEvents {
*/
override async onResponse(event: RequestHookResponseEvent): Promise<void> {
const {headers} = event;
const requestHookContextId = (headers as Record<symbol, RequestHookContextId>)[
REQUEST_HOOK_CONTEXT_ID_KEY
];

assertValueIsDefined(requestHookContextId, 'requestHookContextId is defined', {
requestHookResponseEvent: event,
});
let request: Request | undefined;

if (headers) {
const requestHookContextId = (headers as Record<symbol, RequestHookContextId>)[
REQUEST_HOOK_CONTEXT_ID_KEY
];

assertValueIsDefined(requestHookContextId, 'requestHookContextId is defined', {
requestHookResponseEvent: event,
});

request = this.waitForEventsState.hashOfNotCompleteRequests[requestHookContextId];

completeRequest(requestHookContextId, this.waitForEventsState);
}

if (this.waitForEventsState.responsePredicates.size > 0) {
const response = await getResponseFromResponseEvent(event);

setReadonlyProperty(response, 'request', request);

await processEventsPredicates({
eventType: 'Response',
requestOrResponse: response,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import type {Fn, RequestHookClassWithContext, RequestHookContextId} from '../../
/**
* If class has this symbol, then the context has already been added to the methods of the class.
*/
const IS_CONTEXT_ADDED_KEY = Symbol(
'If class has this symbol, then the context has already been added',
);
const IS_CONTEXT_ADDED_KEY = Symbol('e2ed:IS_CONTEXT_ADDED_KEY');

/**
* Count of all created request hook contexts.
Expand Down

0 comments on commit b910ea6

Please sign in to comment.