Skip to content

Commit fbb20d6

Browse files
committed
Refactor out a separate URL utils file
1 parent 1123bd4 commit fbb20d6

File tree

8 files changed

+46
-51
lines changed

8 files changed

+46
-51
lines changed

src/rules/matchers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import {
99
getPathFromAbsoluteUrl,
1010
isRelativeUrl,
1111
getUrlWithoutProtocol,
12-
waitForCompletedRequest
13-
} from '../util/request-utils';
12+
normalizeUrl
13+
} from '../util/url';
14+
import { waitForCompletedRequest } from '../util/request-utils';
1415
import { Serializable, ClientServerChannel } from "../serialization/serialization";
1516
import { withDeserializedBodyReader, withSerializedBodyReader } from '../serialization/body-serialization';
1617
import { MaybePromise, Replace } from '../util/type-utils';
17-
import { normalizeUrl } from '../util/normalize-url';
1818

1919
export interface RequestMatcher extends Explainable, Serializable {
2020
type: keyof typeof MatcherLookup;

src/rules/requests/request-handlers.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ import {
2020
} from "../../types";
2121

2222
import { MaybePromise } from '../../util/type-utils';
23+
import { isAbsoluteUrl, getEffectivePort } from '../../util/url';
2324
import {
2425
waitForCompletedRequest,
2526
buildBodyReader,
2627
shouldKeepAlive,
2728
isHttp2,
28-
isAbsoluteUrl,
2929
writeHead,
30-
encodeBodyBuffer,
31-
getEffectivePort
30+
encodeBodyBuffer
3231
} from '../../util/request-utils';
3332
import {
3433
h1HeadersToH2,

src/rules/websockets/websocket-handlers.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ import {
1919
ResetConnectionHandler,
2020
TimeoutHandler
2121
} from '../requests/request-handlers';
22-
import {
23-
getEffectivePort,
24-
isHttp2
25-
} from '../../util/request-utils';
22+
import { getEffectivePort } from '../../util/url';
23+
import { isHttp2 } from '../../util/request-utils';
2624
import {
2725
findRawHeader,
2826
findRawHeaders,

src/server/mockttp-server.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,16 @@ import { Mutable } from "../util/type-utils";
4545
import { ErrorLike, isErrorLike } from "../util/error";
4646
import { makePropertyWritable } from "../util/util";
4747

48+
import { isAbsoluteUrl, getPathFromAbsoluteUrl } from "../util/url";
4849
import { buildSocketEventData, isSocketLoop, resetOrDestroy } from "../util/socket-util";
4950
import {
5051
parseRequestBody,
5152
waitForCompletedRequest,
5253
trackResponse,
5354
waitForCompletedResponse,
54-
isAbsoluteUrl,
5555
buildInitiatedRequest,
5656
tryToParseHttpRequest,
5757
buildBodyReader,
58-
getPathFromAbsoluteUrl,
5958
parseRawHttpResponse
6059
} from "../util/request-utils";
6160
import { asBuffer } from "../util/buffer-utils";

src/util/request-utils.ts

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
RawHeaders
2424
} from "../types";
2525

26-
import { nthIndexOf } from './util';
2726
import {
2827
bufferThenStream,
2928
bufferToStream,
@@ -40,41 +39,6 @@ import {
4039
rawHeadersToObject
4140
} from './header-utils';
4241

43-
// Is this URL fully qualified?
44-
// Note that this supports only HTTP - no websockets or anything else.
45-
export const isAbsoluteUrl = (url: string) =>
46-
url.toLowerCase().startsWith('http://') ||
47-
url.toLowerCase().startsWith('https://');
48-
49-
export const isRelativeUrl = (url: string) =>
50-
url.startsWith('/');
51-
52-
export const isAbsoluteProtocollessUrl = (url: string) =>
53-
!isAbsoluteUrl(url) && !isRelativeUrl(url);
54-
55-
export const getUrlWithoutProtocol = (url: string): string => {
56-
return url.split('://', 2).slice(-1).join('');
57-
}
58-
59-
export const getPathFromAbsoluteUrl = (url: string) => {
60-
const pathIndex = nthIndexOf(url, '/', 3);
61-
if (pathIndex !== -1) {
62-
return url.slice(pathIndex);
63-
} else {
64-
return '';
65-
}
66-
}
67-
68-
export const getEffectivePort = (url: { protocol: string | null, port: string | null }) => {
69-
if (url.port) {
70-
return parseInt(url.port, 10);
71-
} else if (url.protocol === 'https:' || url.protocol === 'wss:') {
72-
return 443;
73-
} else {
74-
return 80;
75-
}
76-
}
77-
7842
export const shouldKeepAlive = (req: OngoingRequest): boolean =>
7943
req.httpVersion !== '1.0' &&
8044
req.headers['connection'] !== 'close' &&

src/util/normalize-url.ts renamed to src/util/url.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
11
import * as url from 'url';
22
import * as _ from 'lodash';
33

4-
import { isAbsoluteProtocollessUrl } from './request-utils';
4+
import { nthIndexOf } from './util';
5+
6+
// Is this URL fully qualified?
7+
// Note that this supports only HTTP - no websockets or anything else.
8+
export const isAbsoluteUrl = (url: string) =>
9+
url.toLowerCase().startsWith('http://') ||
10+
url.toLowerCase().startsWith('https://');
11+
12+
export const isRelativeUrl = (url: string) =>
13+
url.startsWith('/');
14+
15+
export const isAbsoluteProtocollessUrl = (url: string) =>
16+
!isAbsoluteUrl(url) && !isRelativeUrl(url);
17+
18+
export const getUrlWithoutProtocol = (url: string): string => {
19+
return url.split('://', 2).slice(-1).join('');
20+
}
21+
22+
export const getPathFromAbsoluteUrl = (url: string) => {
23+
const pathIndex = nthIndexOf(url, '/', 3);
24+
if (pathIndex !== -1) {
25+
return url.slice(pathIndex);
26+
} else {
27+
return '';
28+
}
29+
}
30+
31+
export const getEffectivePort = (url: { protocol: string | null, port: string | null }) => {
32+
if (url.port) {
33+
return parseInt(url.port, 10);
34+
} else if (url.protocol === 'https:' || url.protocol === 'wss:') {
35+
return 443;
36+
} else {
37+
return 80;
38+
}
39+
}
540

641
/**
742
* Normalizes URLs to the form used when matching them.

test/test-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { Mockttp } from "..";
2929
export { getDeferred, Deferred } from '../src/util/promise';
3030
import { makeDestroyable, DestroyableServer } from "destroyable-server";
3131
import { isNode, isWeb, delay } from '../src/util/util';
32-
import { getEffectivePort } from '../src/util/request-utils';
32+
import { getEffectivePort } from '../src/util/url';
3333
export { isNode, isWeb, delay, makeDestroyable, DestroyableServer };
3434

3535
if (isNode) {

test/url-normalization.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { normalizeUrl } from '../src/util/normalize-url';
1+
import { normalizeUrl } from '../src/util/url';
22

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

0 commit comments

Comments
 (0)