Skip to content

Commit

Permalink
fix: mapping of set-cookie header and other headers
Browse files Browse the repository at this point in the history
  • Loading branch information
uid11 committed Aug 4, 2024
1 parent 37eb24c commit 67a498c
Show file tree
Hide file tree
Showing 19 changed files with 55 additions and 58 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ import type {Pack} from 'autotests/configurator';
*/
export const pack: Pack = {
...allTestsPack,
browserInitTimeout: 40_000,
testIdleTimeout: 10_000,
};
```

Expand Down
15 changes: 8 additions & 7 deletions autotests/actions/setPageCookiesAndNavigateToUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {setHeadersAndNavigateToUrl} from 'e2ed/actions';
import {LogEventType} from 'e2ed/constants';
import {getHeaderValue, log, replaceSetCookie} from 'e2ed/utils';

import type {Cookie, Headers, Url} from 'e2ed/types';
import type {Cookie, SetCookieHeaderString, StringHeaders, Url} from 'e2ed/types';

/**
* Navigate to the url and set custom page cookies.
Expand All @@ -11,18 +11,19 @@ export const setPageCookiesAndNavigateToUrl = async (
url: Url,
pageCookies: readonly Cookie[],
): Promise<void> => {
const mapResponseHeaders = (headers: Headers): Headers => {
let setCookies = getHeaderValue(headers, 'set-cookie');
const mapResponseHeaders = (headers: StringHeaders): StringHeaders => {
let cookiesArray: SetCookieHeaderString[] = [];
const setCookies = getHeaderValue(headers, 'set-cookie');

if (setCookies === undefined) {
setCookies = [];
if (setCookies !== undefined) {
cookiesArray.push(setCookies as SetCookieHeaderString);
}

for (const cookie of pageCookies) {
setCookies = replaceSetCookie(setCookies, cookie);
cookiesArray = replaceSetCookie(cookiesArray, cookie);
}

return {'set-cookie': setCookies};
return {'set-cookie': cookiesArray.join('\n')};
};

log(`Navigate to ${url} and set page cookie`, {pageCookies, url}, LogEventType.Action);
Expand Down
6 changes: 3 additions & 3 deletions autotests/actions/setPageRequestHeadersAndNavigateToUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import {setHeadersAndNavigateToUrl} from 'e2ed/actions';
import {LogEventType} from 'e2ed/constants';
import {log} from 'e2ed/utils';

import type {Headers, Url} from 'e2ed/types';
import type {StringHeaders, Url} from 'e2ed/types';

/**
* Navigate to the url and set additional page request headers.
*/
export const setPageRequestHeadersAndNavigateToUrl = async (
url: Url,
pageRequestHeaders: Headers,
pageRequestHeaders: StringHeaders,
): Promise<void> => {
const mapRequestHeaders = (): Headers => pageRequestHeaders;
const mapRequestHeaders = (): StringHeaders => pageRequestHeaders;

log(
`Navigate to ${url} and set page request headers`,
Expand Down
1 change: 0 additions & 1 deletion autotests/packs/allTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export const pack: Pack = {
assertionTimeout: 5_000,
browser,
browserFlags,
browserInitTimeout: 60_000,
concurrency: isLocalRun ? 1 : 2,
customPackProperties: {internalPackRunId: 0, name: 'allTests'},
doAfterPack,
Expand Down
4 changes: 2 additions & 2 deletions autotests/packs/local.example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {pack as allTestsPack} from './allTests';
import type {Pack} from 'autotests/configurator';

/**
* Pack from .gitignore for local development.
* Pack from `.gitignore` for local development.
*/
export const pack: Pack = {
...allTestsPack,
browserInitTimeout: 40_000,
testIdleTimeout: 10_000,
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import {createPageObjectsFromMultiLocator, setReadonlyProperty} from 'e2ed/utils

import {TestRunButton} from './TestRunButton';

import type {Cookie, Headers, Selector, Url} from 'e2ed/types';
import type {Cookie, Selector, StringHeaders, Url} from 'e2ed/types';

type CustomPageParams = {pageCookies?: readonly Cookie[]; pageRequestHeaders?: Headers} | undefined;
type CustomPageParams =
| {pageCookies?: readonly Cookie[]; pageRequestHeaders?: StringHeaders}
| undefined;

/**
* The e2ed report example page.
Expand Down Expand Up @@ -44,7 +46,7 @@ export class E2edReportExample extends Page<CustomPageParams> {
/**
* Request headers that we add to page request.
*/
readonly pageRequestHeaders: Headers | undefined;
readonly pageRequestHeaders: StringHeaders | undefined;

override readonly pageStabilizationInterval = 600;

Expand Down
1 change: 0 additions & 1 deletion src/actions/asserts/assertUrlMatchRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ type MaybeUrlOrPath = Url | string | null | undefined;

/**
* Asserts that url or url path (which can be wrapped in a promise) match route.
* TODO: support Smart Assertions.
*/
export const assertUrlMatchRoute = async (
maybeUrlOrPath: MaybeUrlOrPath | Promise<MaybeUrlOrPath>,
Expand Down
1 change: 0 additions & 1 deletion src/types/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type {OwnE2edConfig} from './ownE2edConfig';
type UserlandTestCafeConfig = Readonly<{
ajaxRequestTimeout: number;
assertionTimeout: number;
browserInitTimeout: number;
concurrency: number;
pageRequestTimeout: number;
port1: number;
Expand Down
7 changes: 6 additions & 1 deletion src/types/http/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type Headers = Readonly<
* Maps headers to new (overridden) headers.
* All headers must be in lower case.
*/
export type MapHeaders = (this: void, headers: Headers) => Headers;
export type MapHeaders = (this: void, headers: StringHeaders) => StringHeaders;

/**
* Options for mappers of headers.
Expand Down Expand Up @@ -117,6 +117,11 @@ export type ResponseWithRequest<
}> &
SomeResponse;

/**
* Headers as strings.
*/
export type StringHeaders = Readonly<Record<string, string>>;

/**
* Brand type for the full url string.
*/
Expand Down
1 change: 1 addition & 0 deletions src/types/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type {
RequestWithUtcTimeInMs,
Response,
ResponseWithRequest,
StringHeaders,
Url,
} from './http';
export type {RequestKeyType, ResponseKeyType} from './internalKeys';
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type {
SameSite,
SetCookieHeaderString,
StatusCode,
StringHeaders,
Url,
} from './http';
export type {KeyboardPressKey} from './keyboard';
Expand Down
1 change: 1 addition & 0 deletions src/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type {
SameSite,
SetCookieHeaderString,
StatusCode,
StringHeaders,
Url,
} from './http';
export type {KeyboardPressKey} from './keyboard';
Expand Down
8 changes: 1 addition & 7 deletions src/utils/config/assertUserlandPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {UserlandPack} from '../../types/internal';
* Asserts that userland pack is correct.
* @internal
*/
// eslint-disable-next-line max-lines-per-function, max-statements
// eslint-disable-next-line max-lines-per-function
export const assertUserlandPack = (userlandPack: UserlandPack): void => {
const logParams = {userlandPack};

Expand All @@ -28,12 +28,6 @@ export const assertUserlandPack = (userlandPack: UserlandPack): void => {
logParams,
);

assertNumberIsPositiveInteger(
userlandPack.browserInitTimeout,
'browserInitTimeout is positive integer',
logParams,
);

assertNumberIsPositiveInteger(
userlandPack.maxRetriesCountInDocker,
'maxRetriesCountInDocker is positive integer',
Expand Down
8 changes: 6 additions & 2 deletions src/utils/report/writeLiteJsonReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {getDurationWithUnits} from '../getDurationWithUnits';

import type {FilePathFromRoot, LiteReport, UtcTimeInMs} from '../../types/internal';

const bytesInKb = 1_024;

/**
* Writes lite JSON report (`lite-report.json` file) with test runs results
* (and without test run logs).
Expand All @@ -23,11 +25,13 @@ export const writeLiteJsonReport = async (liteReport: LiteReport): Promise<void>

await writeFile(reportFilePath, reportJson);

const reportFileSize = await getFileSize(reportFilePath);
const reportFileSizeInBytes = await getFileSize(reportFilePath);

const reportFileSizeInKb = (reportFileSizeInBytes / bytesInKb).toFixed(2);

const durationWithUnits = getDurationWithUnits(Date.now() - startTimeInMs);

generalLog(
`Lite JSON report was written (${reportFileSize} bytes) to "${reportFilePath}" in ${durationWithUnits}`,
`Lite JSON report was written (${reportFileSizeInKb} bytes) to "${reportFilePath}" in ${durationWithUnits}`,
);
};
11 changes: 5 additions & 6 deletions src/utils/requestHooks/applyHeadersMapper.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import {getCopyOfHeaders} from './getCopyOfHeaders';
import {getEquivalentHeadersNames} from './getEquivalentHeadersNames';

import type {Headers, MapHeaders, Mutable} from '../../types/internal';
import type {MapHeaders, Mutable, StringHeaders} from '../../types/internal';

/**
* Map exists headers to new headers and merge this new headers to exists headers.
* @internal
*/
export const applyHeadersMapper = (headers: Headers, mapper: MapHeaders): void => {
const copyOfHeaders = getCopyOfHeaders(headers);
export const applyHeadersMapper = (headers: StringHeaders, mapper: MapHeaders): void => {
const copyOfHeaders = {...headers};
const newHeaders = mapper(copyOfHeaders);

const mutableHeaders: Mutable<Headers> = headers;
const mutableHeaders: Mutable<StringHeaders> = headers;

for (const [name, value] of Object.entries(newHeaders)) {
if (value === undefined) {
Expand All @@ -22,7 +21,7 @@ export const applyHeadersMapper = (headers: Headers, mapper: MapHeaders): void =
delete mutableHeaders[currentName];
}
} else {
mutableHeaders[name] = value as string[] | string | undefined;
mutableHeaders[name] = value;
}
}
};
17 changes: 0 additions & 17 deletions src/utils/requestHooks/getCopyOfHeaders.ts

This file was deleted.

13 changes: 10 additions & 3 deletions src/utils/test/preparePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {getIsPageNavigatingNow, setIsPageNavigatingNow} from '../../context/isPa
import {getNavigationDelay} from '../../context/navigationDelay';
import {getOnResponseCallbacks} from '../../context/onResponseCallbacks';

import {assertValueIsNotNull} from '../asserts';
import {getResponseFromPlaywrightResponse} from '../requestHooks';

import type {Page} from '@playwright/test';
Expand Down Expand Up @@ -76,9 +75,17 @@ export const preparePage = async (page: Page): Promise<void> => {
return;
}

const playwrightResponse = await request.response();
const playwrightResponse = await request.response().catch((error) => {
if (String(error).includes('Target page, context or browser has been closed')) {
return null;
}

assertValueIsNotNull(playwrightResponse, 'response is not null', {url: request.url()});
throw error;
});

if (playwrightResponse === null) {
return;
}

const responseWithRequest = await getResponseFromPlaywrightResponse(playwrightResponse);

Expand Down
4 changes: 2 additions & 2 deletions src/utils/tests/runTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const runTests = async ({runLabel}: RunRetryOptions): Promise<void> => {
setRunLabel(runLabel);

try {
const {browserInitTimeout, enableLiveMode, resourceUsageReadingInternal} = getFullPackConfig();
const {enableLiveMode, testIdleTimeout, resourceUsageReadingInternal} = getFullPackConfig();

startResourceUsageReading(resourceUsageReadingInternal);

Expand All @@ -36,7 +36,7 @@ export const runTests = async ({runLabel}: RunRetryOptions): Promise<void> => {

beforeRunFirstTest();
}
}, browserInitTimeout);
}, testIdleTimeout);

beforeRunFirstTestTimeoutId.unref();

Expand Down
4 changes: 3 additions & 1 deletion src/utils/tests/stripExtraLogs.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {sep} from 'node:path';

/**
* Strip extra Playwright logs.
* @internal
Expand All @@ -8,7 +10,7 @@ export const stripExtraLogs = (text: string): string => {
for (let index = 0; index < lines.length; index += 1) {
const line = lines[index];

if (line?.includes('../node_modules/e2ed/test.js:')) {
if (line?.includes(`${sep}node_modules${sep}e2ed${sep}test.js:`)) {
const startIndex = line.indexOf('›');
const endIndex = line.lastIndexOf('›');

Expand Down

0 comments on commit 67a498c

Please sign in to comment.