Skip to content

Commit

Permalink
Refactor out a separate URL utils file
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Jun 18, 2024
1 parent 1123bd4 commit fbb20d6
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 51 deletions.
6 changes: 3 additions & 3 deletions src/rules/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {
getPathFromAbsoluteUrl,
isRelativeUrl,
getUrlWithoutProtocol,
waitForCompletedRequest
} from '../util/request-utils';
normalizeUrl
} from '../util/url';
import { waitForCompletedRequest } from '../util/request-utils';
import { Serializable, ClientServerChannel } from "../serialization/serialization";
import { withDeserializedBodyReader, withSerializedBodyReader } from '../serialization/body-serialization';
import { MaybePromise, Replace } from '../util/type-utils';
import { normalizeUrl } from '../util/normalize-url';

export interface RequestMatcher extends Explainable, Serializable {
type: keyof typeof MatcherLookup;
Expand Down
5 changes: 2 additions & 3 deletions src/rules/requests/request-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ import {
} from "../../types";

import { MaybePromise } from '../../util/type-utils';
import { isAbsoluteUrl, getEffectivePort } from '../../util/url';
import {
waitForCompletedRequest,
buildBodyReader,
shouldKeepAlive,
isHttp2,
isAbsoluteUrl,
writeHead,
encodeBodyBuffer,
getEffectivePort
encodeBodyBuffer
} from '../../util/request-utils';
import {
h1HeadersToH2,
Expand Down
6 changes: 2 additions & 4 deletions src/rules/websockets/websocket-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import {
ResetConnectionHandler,
TimeoutHandler
} from '../requests/request-handlers';
import {
getEffectivePort,
isHttp2
} from '../../util/request-utils';
import { getEffectivePort } from '../../util/url';
import { isHttp2 } from '../../util/request-utils';
import {
findRawHeader,
findRawHeaders,
Expand Down
3 changes: 1 addition & 2 deletions src/server/mockttp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,16 @@ import { Mutable } from "../util/type-utils";
import { ErrorLike, isErrorLike } from "../util/error";
import { makePropertyWritable } from "../util/util";

import { isAbsoluteUrl, getPathFromAbsoluteUrl } from "../util/url";
import { buildSocketEventData, isSocketLoop, resetOrDestroy } from "../util/socket-util";
import {
parseRequestBody,
waitForCompletedRequest,
trackResponse,
waitForCompletedResponse,
isAbsoluteUrl,
buildInitiatedRequest,
tryToParseHttpRequest,
buildBodyReader,
getPathFromAbsoluteUrl,
parseRawHttpResponse
} from "../util/request-utils";
import { asBuffer } from "../util/buffer-utils";
Expand Down
36 changes: 0 additions & 36 deletions src/util/request-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
RawHeaders
} from "../types";

import { nthIndexOf } from './util';
import {
bufferThenStream,
bufferToStream,
Expand All @@ -40,41 +39,6 @@ import {
rawHeadersToObject
} from './header-utils';

// Is this URL fully qualified?
// Note that this supports only HTTP - no websockets or anything else.
export const isAbsoluteUrl = (url: string) =>
url.toLowerCase().startsWith('http://') ||
url.toLowerCase().startsWith('https://');

export const isRelativeUrl = (url: string) =>
url.startsWith('/');

export const isAbsoluteProtocollessUrl = (url: string) =>
!isAbsoluteUrl(url) && !isRelativeUrl(url);

export const getUrlWithoutProtocol = (url: string): string => {
return url.split('://', 2).slice(-1).join('');
}

export const getPathFromAbsoluteUrl = (url: string) => {
const pathIndex = nthIndexOf(url, '/', 3);
if (pathIndex !== -1) {
return url.slice(pathIndex);
} else {
return '';
}
}

export const getEffectivePort = (url: { protocol: string | null, port: string | null }) => {
if (url.port) {
return parseInt(url.port, 10);
} else if (url.protocol === 'https:' || url.protocol === 'wss:') {
return 443;
} else {
return 80;
}
}

export const shouldKeepAlive = (req: OngoingRequest): boolean =>
req.httpVersion !== '1.0' &&
req.headers['connection'] !== 'close' &&
Expand Down
37 changes: 36 additions & 1 deletion src/util/normalize-url.ts → src/util/url.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
import * as url from 'url';
import * as _ from 'lodash';

import { isAbsoluteProtocollessUrl } from './request-utils';
import { nthIndexOf } from './util';

// Is this URL fully qualified?
// Note that this supports only HTTP - no websockets or anything else.
export const isAbsoluteUrl = (url: string) =>
url.toLowerCase().startsWith('http://') ||
url.toLowerCase().startsWith('https://');

export const isRelativeUrl = (url: string) =>
url.startsWith('/');

export const isAbsoluteProtocollessUrl = (url: string) =>
!isAbsoluteUrl(url) && !isRelativeUrl(url);

export const getUrlWithoutProtocol = (url: string): string => {
return url.split('://', 2).slice(-1).join('');
}

export const getPathFromAbsoluteUrl = (url: string) => {
const pathIndex = nthIndexOf(url, '/', 3);
if (pathIndex !== -1) {
return url.slice(pathIndex);
} else {
return '';
}
}

export const getEffectivePort = (url: { protocol: string | null, port: string | null }) => {
if (url.port) {
return parseInt(url.port, 10);
} else if (url.protocol === 'https:' || url.protocol === 'wss:') {
return 443;
} else {
return 80;
}
}

/**
* Normalizes URLs to the form used when matching them.
Expand Down
2 changes: 1 addition & 1 deletion test/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { Mockttp } from "..";
export { getDeferred, Deferred } from '../src/util/promise';
import { makeDestroyable, DestroyableServer } from "destroyable-server";
import { isNode, isWeb, delay } from '../src/util/util';
import { getEffectivePort } from '../src/util/request-utils';
import { getEffectivePort } from '../src/util/url';
export { isNode, isWeb, delay, makeDestroyable, DestroyableServer };

if (isNode) {
Expand Down
2 changes: 1 addition & 1 deletion test/url-normalization.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { normalizeUrl } from '../src/util/normalize-url';
import { normalizeUrl } from '../src/util/url';

import { expect } from "./test-utils";

Expand Down

0 comments on commit fbb20d6

Please sign in to comment.