-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
53 changed files
with
803 additions
and
238 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
deno.lock | ||
coverage |
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
File renamed without changes.
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
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
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 |
---|---|---|
@@ -1,3 +1,17 @@ | ||
# HTTP Assertions | ||
|
||
_Docs coming soon_ | ||
Assertion functions for use in HTTP Request/Response related tests. | ||
|
||
An [AssertionError](https://jsr.io/@std/assert/doc/~/AssertionError) will be | ||
thrown for false assertions. | ||
|
||
```ts | ||
import { assertStatus, STATUS_CODE } from "@http/assert"; | ||
|
||
const response = new Response(); | ||
|
||
assertStatus(response, STATE_CODE.OK); | ||
``` | ||
|
||
NOTE: the `STATUS_CODE` object and `StatusCode` type are re-exported from | ||
`@std/http/status` for convenience. |
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,51 @@ | ||
import { assertHeader } from "./assert_header.ts"; | ||
import { AssertionError } from "@std/assert/assertion-error"; | ||
import { assertThrows } from "@std/assert/assert-throws"; | ||
|
||
Deno.test("assertHeader() matches header with value in Headers object", () => { | ||
const headers = new Headers({ "Stuff": "Nonsense" }); | ||
assertHeader(headers, "Stuff", "Nonsense"); | ||
}); | ||
|
||
Deno.test("assertHeader() matches header with value in Request object", () => { | ||
const request = new Request("http://example.com", { | ||
headers: { "Stuff": "Nonsense" }, | ||
}); | ||
assertHeader(request, "Stuff", "Nonsense"); | ||
}); | ||
|
||
Deno.test("assertHeader() matches header with value in Response object", () => { | ||
const response = new Response("http://example.com", { | ||
headers: { "Stuff": "Nonsense" }, | ||
}); | ||
assertHeader(response, "Stuff", "Nonsense"); | ||
}); | ||
|
||
Deno.test("assertHeader() matches header name case-insensitively", () => { | ||
const headers = new Headers({ "Stuff": "Nonsense" }); | ||
assertHeader(headers, "stuFF", "Nonsense"); | ||
}); | ||
|
||
Deno.test("assertHeader() throws when header is missing", () => { | ||
const headers = new Headers({ "Not-Stuff": "Nonsense" }); | ||
assertThrows( | ||
() => { | ||
assertHeader(headers, "Stuff", "Nonsense"); | ||
}, | ||
AssertionError, | ||
undefined, | ||
`Expected header "Stuff" with value "Nonsense", header not present`, | ||
); | ||
}); | ||
|
||
Deno.test("assertHeader() throws when value is not equal", () => { | ||
const headers = new Headers({ "Stuff": "Nonsense" }); | ||
assertThrows( | ||
() => { | ||
assertHeader(headers, "Stuff", "Sense"); | ||
}, | ||
AssertionError, | ||
undefined, | ||
`Expected header "Stuff" with value "Sense", got: "Nonsense"`, | ||
); | ||
}); |
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 |
---|---|---|
@@ -1,20 +1,42 @@ | ||
import { AssertionError } from "@std/assert/assertion-error"; | ||
|
||
/** | ||
* Assert that a HTTP header is present with an expected value. | ||
* | ||
* @param container a Request, Response, or Headers object | ||
* @param headerName the header name | ||
* @param expectedValue the value expected in the header | ||
* @throws an AssertionError if the header is missing or its value is not exactly equal to the expected value | ||
* | ||
* @example | ||
* ```ts | ||
* import { assertHeader } from "@http/assert"; | ||
* | ||
* const resp = new Response(null, { | ||
* headers: { | ||
* "Content-Type": "application/json" | ||
* } | ||
* }); | ||
* | ||
* assertHeader(resp, "Content-Type", "application/json"); | ||
* ``` | ||
*/ | ||
export function assertHeader( | ||
response: Response, | ||
container: Response | Request | Headers, | ||
headerName: string, | ||
expectedValue: string, | ||
) { | ||
if (!response.headers.has(headerName)) { | ||
const headers = container instanceof Headers ? container : container.headers; | ||
if (!headers.has(headerName)) { | ||
throw new AssertionError( | ||
`Expected response header "${headerName}" with value "${expectedValue}", but header not present`, | ||
`Expected header "${headerName}" with value "${expectedValue}", but header not present`, | ||
); | ||
} | ||
|
||
const actualValue = response.headers.get(headerName); | ||
const actualValue = headers.get(headerName); | ||
if (actualValue !== expectedValue) { | ||
throw new AssertionError( | ||
`Expected response header "${headerName}" with value "${expectedValue}", got: "${actualValue}"`, | ||
`Expected header "${headerName}" with value "${expectedValue}", got: "${actualValue}"`, | ||
); | ||
} | ||
} |
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,29 @@ | ||
import { assertOk } from "./assert_ok.ts"; | ||
import { assertThrows } from "@std/assert/assert-throws"; | ||
import { AssertionError } from "@std/assert/assertion-error"; | ||
|
||
Deno.test("assertOk() matches when response.ok is true", () => { | ||
assertOk(new Response()); | ||
}); | ||
|
||
Deno.test("assertOk() throws when response.ok is false", () => { | ||
assertThrows( | ||
() => { | ||
assertOk(new Response(null, { status: 500 })); | ||
}, | ||
AssertionError, | ||
"Expected response ok", | ||
); | ||
}); | ||
|
||
Deno.test("assertOk() thrown error includes status code and text", () => { | ||
assertThrows( | ||
() => { | ||
assertOk( | ||
new Response(null, { status: 599, statusText: "Naughty naughty" }), | ||
); | ||
}, | ||
AssertionError, | ||
"599 Naughty naughty", | ||
); | ||
}); |
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,31 @@ | ||
import { assertStatus, STATUS_CODE } from "./assert_status.ts"; | ||
import { assertThrows } from "@std/assert/assert-throws"; | ||
import { AssertionError } from "@std/assert/assertion-error"; | ||
|
||
Deno.test("assertStatus() matches status code and text", () => { | ||
assertStatus(new Response(null, { status: 200, statusText: "OK" }), 200); | ||
}); | ||
|
||
Deno.test("assertStatus() throws status code doesn't match", () => { | ||
assertThrows( | ||
() => { | ||
assertStatus(new Response(null, { status: 500 }), 200); | ||
}, | ||
AssertionError, | ||
"Expected response status", | ||
); | ||
}); | ||
|
||
Deno.test("assertStatus() throws if status text doesn't match the expected value for the code", () => { | ||
assertThrows( | ||
() => { | ||
assertStatus( | ||
new Response(null, { status: 200, statusText: "Alrighty" }), | ||
STATUS_CODE.OK, | ||
); | ||
}, | ||
AssertionError, | ||
undefined, | ||
`"Expected response status "200 OK", got "200 Alrighty"`, | ||
); | ||
}); |
Oops, something went wrong.