Skip to content

Commit

Permalink
test: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Apr 19, 2023
1 parent db11173 commit ef4bb62
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 24 deletions.
13 changes: 0 additions & 13 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,3 @@ export function mergeHeaders(left: Headers, right: Headers): Headers {

return left;
}

export function createResponse(
response: Response,
init?: ResponseInit,
): Response {
const { body, headers, status, statusText } = response;

return new Response(body, {
headers: init?.headers ?? headers,
status: init?.status ?? status,
statusText: init?.statusText ?? statusText,
});
}
24 changes: 13 additions & 11 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import {
append,
CORSHeader,
createResponse,
type Handler,
isNumber,
isString,
Expand All @@ -16,6 +15,7 @@ import {
import {
assertNonNegativeInteger,
assertTokenFormat,
fromResponse,
isCORSPreflightRequest,
isCORSRequest,
match,
Expand All @@ -41,6 +41,7 @@ export interface CORSHeaders {
readonly exposeHeaders?: readonly string[];
}

/** Headers for CORS preflight request. */
export interface CORSPreflightHeaders
extends Omit<CORSHeaders, "exposeHeaders"> {
/** `Access-Control-Allow-Methods`.
Expand Down Expand Up @@ -100,13 +101,6 @@ export function cors(options: CORSHeaders = {}): Middleware {
});
}

function createOriginMatcher(
allowOrigins: readonly (string | RegExp)[],
): (origin: string) => boolean {
return (origin: string) =>
allowOrigins.some((pattern) => match(origin, pattern));
}

/**
* @internal
*/
Expand All @@ -127,7 +121,7 @@ export async function _cors(
new Headers({ [Header.Vary]: finalVary }),
);

if (!isCORSRequest(request)) return createResponse(response, { headers });
if (!isCORSRequest(request)) return fromResponse(response, { headers });

const { matchOrigin, allowCredentials, exposeHeaders } = context;
const origin = request.headers.get(Header.Origin);
Expand All @@ -147,9 +141,10 @@ export async function _cors(

const finalHeaders = mergeHeaders(left, headers);

return createResponse(response, { headers: finalHeaders });
return fromResponse(response, { headers: finalHeaders });
}

/** Preflight middleware options. */
export interface PreflightOptions extends CORSPreflightHeaders {
/** Preflight response status code.
* @default 204
Expand Down Expand Up @@ -257,7 +252,7 @@ export async function _preflight(
new Headers({ [Header.Vary]: finalVary }),
);

return createResponse(response, { headers });
return fromResponse(response, { headers });
}

const {
Expand Down Expand Up @@ -336,3 +331,10 @@ function createAssertTokens(subject: string, expected: string) {
);
};
}

function createOriginMatcher(
allowOrigins: readonly (string | RegExp)[],
): (origin: string) => boolean {
return (origin: string) =>
allowOrigins.some((pattern) => match(origin, pattern));
}
14 changes: 14 additions & 0 deletions utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,17 @@ export function assertNonNegativeInteger(
): asserts input {
if (!isNonNegativeInteger(input)) throw Error(msg);
}

/** Create {@link Response} from response. */
export function fromResponse(
response: Response,
init?: ResponseInit,
): Response {
const { body, headers, status, statusText } = response;

return new Response(body, {
headers: init?.headers ?? headers,
status: init?.status ?? status,
statusText: init?.statusText ?? statusText,
});
}
48 changes: 48 additions & 0 deletions utils_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { fromResponse } from "./utils.ts";
import { assert, describe, equalsResponse, it, Status } from "./_dev_deps.ts";

describe("fromResponse", () => {
it("should return new response what change partial properties", async () => {
assert(
await equalsResponse(fromResponse(new Response()), new Response(), true),
);
assert(
await equalsResponse(
fromResponse(new Response(null, { status: Status.NoContent })),
new Response(null, { status: Status.NoContent }),
true,
),
);
assert(
await equalsResponse(
fromResponse(new Response(null, { status: Status.NoContent }), {
status: Status.OK,
}),
new Response(null, { status: Status.OK }),
true,
),
);
assert(
await equalsResponse(
fromResponse(
new Response("test", {
headers: { "x-test": "test" },
}),
{
headers: {
"content-type": "text/plain",
"x-test2": "test",
},
},
),
new Response("test", {
headers: {
"content-type": "text/plain",
"x-test2": "test",
},
}),
true,
),
);
});
});

0 comments on commit ef4bb62

Please sign in to comment.