diff --git a/src/rules/matchers.ts b/src/rules/matchers.ts index 1160a2d61..4609a2ba4 100644 --- a/src/rules/matchers.ts +++ b/src/rules/matchers.ts @@ -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; diff --git a/src/rules/requests/request-handlers.ts b/src/rules/requests/request-handlers.ts index 00150500a..295db588c 100644 --- a/src/rules/requests/request-handlers.ts +++ b/src/rules/requests/request-handlers.ts @@ -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, diff --git a/src/rules/websockets/websocket-handlers.ts b/src/rules/websockets/websocket-handlers.ts index 973ce184a..536720a9c 100644 --- a/src/rules/websockets/websocket-handlers.ts +++ b/src/rules/websockets/websocket-handlers.ts @@ -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, diff --git a/src/server/mockttp-server.ts b/src/server/mockttp-server.ts index 5f552f9ea..5e210b3f7 100644 --- a/src/server/mockttp-server.ts +++ b/src/server/mockttp-server.ts @@ -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"; diff --git a/src/util/request-utils.ts b/src/util/request-utils.ts index 17597d60e..4ed8c0399 100644 --- a/src/util/request-utils.ts +++ b/src/util/request-utils.ts @@ -23,7 +23,6 @@ import { RawHeaders } from "../types"; -import { nthIndexOf } from './util'; import { bufferThenStream, bufferToStream, @@ -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' && diff --git a/src/util/normalize-url.ts b/src/util/url.ts similarity index 72% rename from src/util/normalize-url.ts rename to src/util/url.ts index f71e8b161..f08be6d0a 100644 --- a/src/util/normalize-url.ts +++ b/src/util/url.ts @@ -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. diff --git a/test/test-utils.ts b/test/test-utils.ts index 29780e374..1395f4243 100644 --- a/test/test-utils.ts +++ b/test/test-utils.ts @@ -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) { diff --git a/test/url-normalization.spec.ts b/test/url-normalization.spec.ts index 9d9b062bb..ba4589269 100644 --- a/test/url-normalization.spec.ts +++ b/test/url-normalization.spec.ts @@ -1,4 +1,4 @@ -import { normalizeUrl } from '../src/util/normalize-url'; +import { normalizeUrl } from '../src/util/url'; import { expect } from "./test-utils";