-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(with_header): add
withHeader
function
- Loading branch information
1 parent
1705c47
commit 1619af5
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
/** Return an instance with the provided `Response` replacing the specified header. | ||
* There are no side effects on the original `Response`. | ||
* | ||
* @example | ||
* ```ts | ||
* import { withHeader } from "https://deno.land/x/response_utils@$VERSION/with_header.ts"; | ||
* import { assert } from "https://deno.land/std/testing/asserts.ts"; | ||
* | ||
* declare const init: Response; | ||
* declare const fieldName: string; | ||
* declare const fieldValue: string; | ||
* | ||
* const response = withHeader(init, fieldName, fieldValue); | ||
* | ||
* assert(response.headers.get(fieldName), fieldValue); | ||
* assert(init !== response); | ||
* ``` | ||
*/ | ||
export function withHeader( | ||
response: Response, | ||
fieldName: string, | ||
fieldValue: string, | ||
): Response { | ||
const headers = new Headers([...response.headers.entries(), [ | ||
fieldName, | ||
fieldValue, | ||
]]); | ||
const { status, statusText } = response; | ||
|
||
return new Response(response.body, { headers, status, statusText }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license. | ||
|
||
import { withHeader } from "./with_header.ts"; | ||
import { assert, assertEquals, describe, it } from "./_dev_deps.ts"; | ||
|
||
describe("withHeader", () => { | ||
const FIELD_NAME = "x-x"; | ||
const FIELD_VALUE = "test"; | ||
const BODY = "ok"; | ||
const INIT_HEADER = "x-init"; | ||
const INIT_HEADER_VALUE = "init"; | ||
|
||
it("should return new response instance with specified header", async () => { | ||
const STATUS = 201; | ||
const STATUS_TEXT = "status text"; | ||
|
||
const initResponse = new Response(BODY, { | ||
status: STATUS, | ||
statusText: STATUS_TEXT, | ||
headers: { | ||
[INIT_HEADER]: INIT_HEADER_VALUE, | ||
}, | ||
}); | ||
|
||
const response = withHeader(initResponse, FIELD_NAME, FIELD_VALUE); | ||
|
||
assert(response !== initResponse); | ||
assertEquals(response.headers.get(FIELD_NAME), FIELD_VALUE); | ||
assertEquals(response.headers.get(INIT_HEADER), INIT_HEADER_VALUE); | ||
assertEquals(response.status, STATUS); | ||
assertEquals(response.statusText, STATUS_TEXT); | ||
assertEquals(await response.text(), BODY); | ||
|
||
assert(!initResponse.headers.has(FIELD_NAME)); | ||
assert(initResponse.headers.has(INIT_HEADER)); | ||
}); | ||
}); |