Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelMor25 committed Dec 7, 2023
1 parent 1c4ad91 commit 8478b84
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
19 changes: 9 additions & 10 deletions src/native-automation/request-pipeline/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import CertificateErrorEvent = Protocol.Security.CertificateErrorEvent;
import HeaderEntry = Protocol.Fetch.HeaderEntry;
import NativeAutomationRequestHookEventProvider from '../request-hooks/event-provider';
import ResourceInjector, { ResourceInjectorOptions } from '../resource-injector';
import { convertToHeaderEntries, getHeaderValue } from '../utils/headers';
import { convertToHeaderEntries, getHeaderEntry } from '../utils/headers';
import httpHeaders from '../../utils/http-headers';

import {
createRequestPausedEventForResponse,
Expand Down Expand Up @@ -154,13 +155,13 @@ export default class NativeAutomationRequestPipeline extends NativeAutomationApi
await this._resourceInjector.processNonProxiedContent(fulfillInfo, this._client, sessionId);
else {
const userScripts = await this._getUserScripts(event);
const contentType = getHeaderValue(event.responseHeaders, 'content-type');
const contentType = getHeaderEntry(event.responseHeaders, httpHeaders.contentType)?.value;

await this._resourceInjector.processHTMLPageContent(fulfillInfo, {
isIframe: false,
contextStorage: this.contextStorage,
userScripts,
}, this._client, sessionId, contentType as string);
}, this._client, sessionId, contentType);
}

requestPipelineMockLogger(`sent mocked response for the ${event.requestId}`);
Expand Down Expand Up @@ -199,8 +200,8 @@ export default class NativeAutomationRequestPipeline extends NativeAutomationApi
return;
}

const contentType = getHeaderValue(event.responseHeaders, 'content-type');
const resourceInfo = await this._resourceInjector.getDocumentResourceInfo(event, this._client, contentType as string);
const contentType = getHeaderEntry(event.responseHeaders, httpHeaders.contentType)?.value;
const resourceInfo = await this._resourceInjector.getDocumentResourceInfo(event, this._client, contentType);

if (resourceInfo.error) {
if (this._shouldRedirectToErrorPage(event)) {
Expand Down Expand Up @@ -241,7 +242,7 @@ export default class NativeAutomationRequestPipeline extends NativeAutomationApi
contextStorage: this.contextStorage,
userScripts,
},
this._client, sessionId, contentType as string);
this._client, sessionId, contentType);

this._contextInfo.dispose(getRequestId(event));

Expand All @@ -257,9 +258,7 @@ export default class NativeAutomationRequestPipeline extends NativeAutomationApi
}

private static _isPage (responseHeaders: HeaderEntry[] | undefined): boolean {
const contentType = responseHeaders
?.find(header => header.name.toLowerCase() === 'content-type')
?.value;
const contentType = getHeaderEntry(responseHeaders, httpHeaders.contentType)?.value;

if (contentType)
return contentTypeUtils.isPage(contentType);
Expand Down Expand Up @@ -501,7 +500,7 @@ export default class NativeAutomationRequestPipeline extends NativeAutomationApi
const headers = this._formatHeadersForContinueResponse(event.request.headers);

for (const changedHeader of reqOpts._changedHeaders) {
const targetHeader = headers.find(header => header.name.toLowerCase() === changedHeader.name) as HeaderEntry;
const targetHeader = getHeaderEntry(headers, changedHeader.name);

if (targetHeader)
targetHeader.value = changedHeader.value;
Expand Down
6 changes: 3 additions & 3 deletions src/native-automation/resource-injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export default class ResourceInjector {
return stringifyHeaderValues(headers);
}

private async _fulfillRequest (client: ProtocolApi, fulfillRequestInfo: FulfillRequestRequest, body: string, sessionId: SessionId, contentType = ''): Promise<void> {
private async _fulfillRequest (client: ProtocolApi, fulfillRequestInfo: FulfillRequestRequest, body: string, sessionId: SessionId, contentType?: string): Promise<void> {
await safeFulfillRequest(client, {
requestId: fulfillRequestInfo.requestId,
responseCode: fulfillRequestInfo.responseCode || StatusCodes.OK,
Expand All @@ -158,7 +158,7 @@ export default class ResourceInjector {
await navigateTo(client, this._options.specialServiceRoutes.errorPage1);
}

public async getDocumentResourceInfo (event: RequestPausedEvent, client: ProtocolApi, contentType: string): Promise<DocumentResourceInfo> {
public async getDocumentResourceInfo (event: RequestPausedEvent, client: ProtocolApi, contentType?: string): Promise<DocumentResourceInfo> {
const {
requestId,
request,
Expand Down Expand Up @@ -213,7 +213,7 @@ export default class ResourceInjector {
});
}

public async processHTMLPageContent (fulfillRequestInfo: FulfillRequestRequest, injectableResourcesOptions: InjectableResourcesOptions, client: ProtocolApi, sessionId: SessionId, contentType: string): Promise<void> {
public async processHTMLPageContent (fulfillRequestInfo: FulfillRequestRequest, injectableResourcesOptions: InjectableResourcesOptions, client: ProtocolApi, sessionId: SessionId, contentType?: string): Promise<void> {
const injectableResources = await this._prepareInjectableResources(injectableResourcesOptions);

// NOTE: an unhandled exception interrupts the test execution,
Expand Down
4 changes: 2 additions & 2 deletions src/native-automation/utils/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function convertToOutgoingHttpHeaders (headers: HeaderEntry[] | undefined
}, {});
}

export function getHeaderValue (headers: HeaderEntry[] | undefined, headerName: string): string | null {
return headers?.find(header => header.name.toLowerCase() === headerName)?.value || null;
export function getHeaderEntry (headers: HeaderEntry[] | undefined, headerName: string): HeaderEntry | undefined {
return headers?.find(header => header.name.toLowerCase() === headerName);
}

9 changes: 6 additions & 3 deletions src/native-automation/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import GetResponseBodyResponse = Protocol.Network.GetResponseBodyResponse;
import HeaderEntry = Protocol.Fetch.HeaderEntry;
import { decodeBufferToString, encodeStringToBuffer } from 'testcafe-hammerhead';

export function getResponseAsString (response: GetResponseBodyResponse, contentType = 'charset=utf-8'): string {
export function getResponseAsString (response: GetResponseBodyResponse, contentType?: string): string {
if (!contentType)
return response.base64Encoded ? Buffer.from(response.body, 'base64').toString() : response.body;

const bufferBody = getResponseAsBuffer(response);

return decodeBufferToString(bufferBody, contentType);
Expand All @@ -15,8 +18,8 @@ export function getResponseAsBuffer (response: GetResponseBodyResponse): Buffer
: Buffer.from(response.body);
}

export function toBase64String (str: string, contentType = 'charset=utf-8'): string {
const bufferBody = encodeStringToBuffer(str, contentType);
export function toBase64String (str: string, contentType?: string): string {
const bufferBody = contentType ? encodeStringToBuffer(str, contentType) : Buffer.from(str);

return bufferBody.toString('base64');
}
Expand Down

0 comments on commit 8478b84

Please sign in to comment.