From b910ea69d8b67ab2b7e4d5237a7bb132f1fee8cf Mon Sep 17 00:00:00 2001 From: uid11 Date: Wed, 8 Nov 2023 04:58:20 +0300 Subject: [PATCH] feat: add optional `request` field to `Response` object tests: add test for `request` field in `Response` object --- autotests/tests/waitForResponse.ts | 13 ++++++++++- src/constants/pages.ts | 2 +- src/constants/requestHook.ts | 4 ++-- src/constants/selector.ts | 2 +- src/types/http/http.ts | 4 +++- src/utils/report/client/sanitizeHtml.ts | 4 ++-- .../RequestHookToWaitForEvents.ts | 22 +++++++++++++------ ...addContextToResultsOfClassCreateMethods.ts | 4 +--- 8 files changed, 37 insertions(+), 18 deletions(-) diff --git a/autotests/tests/waitForResponse.ts b/autotests/tests/waitForResponse.ts index 57fe1b03..62553bc0 100644 --- a/autotests/tests/waitForResponse.ts +++ b/autotests/tests/waitForResponse.ts @@ -23,7 +23,7 @@ it( void addUser(); - const response = await waitForResponse( + let response = await waitForResponse( ({responseBody}: Response) => responseBody?.name === 'John', ); @@ -38,5 +38,16 @@ it( }, () => undefined, ); + + void addUser(); + + response = await waitForResponse( + ({request}: Response) => request?.url === 'https://reqres.in/api/users', + ); + + await expect(response.responseBody, 'second response has correct body').contains({ + job: 'leader', + name: 'John', + }); }, ); diff --git a/src/constants/pages.ts b/src/constants/pages.ts index 3dcfac07..116eb4e0 100644 --- a/src/constants/pages.ts +++ b/src/constants/pages.ts @@ -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'); diff --git a/src/constants/requestHook.ts b/src/constants/requestHook.ts index 1fddceeb..94d7f4f0 100644 --- a/src/constants/requestHook.ts +++ b/src/constants/requestHook.ts @@ -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'); diff --git a/src/constants/selector.ts b/src/constants/selector.ts index 4911ac93..7f901169 100644 --- a/src/constants/selector.ts +++ b/src/constants/selector.ts @@ -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'); diff --git a/src/types/http/http.ts b/src/types/http/http.ts index 0881886c..2023ae14 100644 --- a/src/types/http/http.ts +++ b/src/types/http/http.ts @@ -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; diff --git a/src/utils/report/client/sanitizeHtml.ts b/src/utils/report/client/sanitizeHtml.ts index aaf15ee9..71b5d004 100644 --- a/src/utils/report/client/sanitizeHtml.ts +++ b/src/utils/report/client/sanitizeHtml.ts @@ -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) { @@ -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) diff --git a/src/utils/requestHooks/RequestHookToWaitForEvents.ts b/src/utils/requestHooks/RequestHookToWaitForEvents.ts index 7278438f..d672253b 100644 --- a/src/utils/requestHooks/RequestHookToWaitForEvents.ts +++ b/src/utils/requestHooks/RequestHookToWaitForEvents.ts @@ -6,6 +6,7 @@ import { } from '../../constants/internal'; import {assertValueIsDefined} from '../asserts'; +import {setReadonlyProperty} from '../setReadonlyProperty'; import {addNotCompleteRequest, completeRequest, processEventsPredicates} from '../waitForEvents'; import {getRequestFromRequestOptions} from './getRequestFromRequestOptions'; @@ -13,6 +14,7 @@ import {getResponseFromResponseEvent} from './getResponseFromResponseEvent'; import {RequestHookWithEvents} from './RequestHookWithEvents'; import type { + Request, RequestHookConfigureResponseEvent, RequestHookContextId, RequestHookRequestEvent, @@ -56,21 +58,27 @@ export class RequestHookToWaitForEvents extends RequestHookWithEvents { */ override async onResponse(event: RequestHookResponseEvent): Promise { const {headers} = event; - const requestHookContextId = (headers as Record)[ - REQUEST_HOOK_CONTEXT_ID_KEY - ]; - - assertValueIsDefined(requestHookContextId, 'requestHookContextId is defined', { - requestHookResponseEvent: event, - }); + let request: Request | undefined; if (headers) { + const requestHookContextId = (headers as Record)[ + 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, diff --git a/src/utils/requestHooks/addContextToResultsOfClassCreateMethods.ts b/src/utils/requestHooks/addContextToResultsOfClassCreateMethods.ts index 9668ac6e..981045a1 100644 --- a/src/utils/requestHooks/addContextToResultsOfClassCreateMethods.ts +++ b/src/utils/requestHooks/addContextToResultsOfClassCreateMethods.ts @@ -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.