HTTP utility collection.
Compliant with RFC 9110.
Utilities for Request
object.
@deprecated Move to request-utils.
Check two Request
fields equality.
import { equalsRequest } from "https://deno.land/x/http_utils@$VERSION/request.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";
declare const url: URL;
assert(
equalsRequest(
new Request(url, { method: "HEAD" }),
new Request(url, { method: "HEAD" }),
),
);
If you also want to check the equivalence of the body, set the mode to strict.
import { equalsRequest } from "https://deno.land/x/http_utils@$VERSION/request.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";
declare const url: URL;
assert(
await equalsRequest(
new Request(url, { body: "", method: "POST" }),
new Request(url, { body: "", method: "POST" }),
true,
),
);
In strict mode, if request body has already been read.
import { equalsRequest } from "https://deno.land/x/http_utils@$VERSION/request.ts";
import { assert, assertThrows } from "https://deno.land/std/testing/asserts.ts";
declare const url: URL;
const request = new Request(url, { body: "" });
await request.text();
assert(request.bodyUsed);
assertThrows(() => equalsRequest(request, request, true));
Whether the input is Request
or not.
import { isRequest } from "https://deno.land/x/http_utils@$VERSION/request.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(isRequest(new Request("http://localhost")), true);
assertEquals(isRequest({}), false);
assertEquals(isRequest(null), false);
Utilities for Response
object.
@deprecated Move to response-utils.
Check two Response
fields equality.
import { equalsResponse } from "https://deno.land/x/http_utils@$VERSION/response.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(
equalsResponse(
new Response(null, { status: 204, headers: { "content-length": "0" } }),
new Response(null, { status: 204, headers: { "content-length": "0" } }),
),
);
If you also want to check the equivalence of the body, set the mode to strict.
import { equalsResponse } from "https://deno.land/x/http_utils@$VERSION/response.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(
await equalsResponse(
new Response("test1", { status: 200, headers: { "content-length": "5" } }),
new Response("test2", { status: 200, headers: { "content-length": "5" } }),
true,
),
);
In strict mode, if response body has already been read.
import { equalsResponse } from "https://deno.land/x/http_utils@$VERSION/response.ts";
import {
assert,
assertThrows,
} from "https://deno.land/std@$VERSION/testing/asserts.ts";
const response = new Response("");
await response.text();
assert(response.bodyUsed);
assertThrows(() => equalsResponse(response, response, true));
Whether the input is Response
or not.
import { isResponse } from "https://deno.land/x/http_utils@$VERSION/response.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(isResponse(new Response()), true);
assertEquals(isResponse({}), false);
assertEquals(isResponse(null), false);
Utilities for Headers
object.
@deprecated Move to headers-utils.
Check two Headers
field name and field value equality.
import { equalsHeaders } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(
equalsHeaders(new Headers({ a: "b" }), new Headers({ a: "b" })),
true,
);
assertEquals(
equalsHeaders(new Headers({ a: "b" }), new Headers({ c: "d" })),
false,
);
@deprecated Move to headers-utils.
Returns a new Headers
with all entries of the given headers except the ones
that have a key(header name or field name) that does not match the given
predicate.
import { filterKeys } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
const headers = filterKeys(
new Headers({
"date": "<date>",
"content-type": "<content-type>",
}),
(key) => key.startsWith("content"),
);
assert(headers.has("content-type"));
assert(!headers.has("date"));
Whether the input is MessageMetadataHeader or not.
import { isMessageMetadataHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isMessageMetadataHeader("date"));
assert(!isMessageMetadataHeader("<others>"));
Whether the input is MessageForwardingHeader or not.
import { isMessageForwardingHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isMessageForwardingHeader("connection"));
assert(!isMessageForwardingHeader("<others>"));
Whether the input is RepresentationHeader or not.
import { isRepresentationHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isRepresentationHeader("content-type"));
assert(!isRepresentationHeader("<others>"));
Whether the input is AuthenticationHeader or not.
import { isAuthenticationHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isAuthenticationHeader("authorization"));
assert(!isAuthenticationHeader("<others>"));
Whether the input is ContentNegotiationHeader or not.
import { isContentNegotiationHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isContentNegotiationHeader("accept"));
assert(!isContentNegotiationHeader("<others>"));
Whether the input is ConditionalHeader or not.
import { isConditionalHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isConditionalHeader("if-match"));
assert(!isConditionalHeader("<others>"));
Whether the input is RangeHeader or not.
import { isRangeHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isRangeHeader("range"));
assert(!isRangeHeader("<others>"));
Whether the input is CachingHeader or not.
import { isCachingHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isCachingHeader("age"));
assert(!isCachingHeader("<others>"));
HTTP Message Metadata header fields.
Compliant with RFC 9110, 6.6. Message Metadata.
- Date
- Trailer
import { MessageMetadataHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(MessageMetadataHeader.Date, "date");
HTTP Message Forwarding header fields.
Compliant with RFC 9110, 7.6. Message Forwarding.
- Connection
- Max-Forwards
- Via
import { MessageForwardingHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(MessageForwardingHeader.Via, "via");
HTTP representation data and metadata header fields.
Compliant with RFC 9110, 8. Representations.
- Content-Type
- Content-Encoding
- Content-Language
- Content-Length
- Content-Location
- Last-Modified
- ETag
import { RepresentationHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(RepresentationHeader.ContentType, "content-type");
HTTP Authentication header fields.
Compliant with RFC 9110, 11. HTTP Authentication.
- WWW-Authenticate
- Authorization
- Authentication-Info
- Proxy-Authenticate
- Proxy-Authorization
- Proxy-Authentication-Info
import { AuthenticationHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(AuthenticationHeader.Authorization, "authorization");
HTTP content negotiation header fields.
Compliant with RFC 9110, 12. Content Negotiation.
- Accept
- Accept-Charset
- Accept-Encoding
- Accept-Language
- Vary
import { ContentNegotiationHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(ContentNegotiationHeader.Accept, "accept");
HTTP conditional requests header fields.
Compliant with RFC 9110, 13. Conditional Requests.
- If-Match
- If-None-Match
- If-Modified-Since
- If-Unmodified-Since
- If-Range
import { ConditionalHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(ConditionalHeader.IfNoneMatch, "if-none-match");
HTTP range requests header fields.
Compliant with RFC 9110, 14. Range Requests.
- Range
- Accept-Ranges
- Content-Range
import { RangeHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(RangeHeader.Range, "range");
HTTP Caching header fields.
Compliant with RFC 9111, HTTP Caching.
- Age
- Cache-Control
- Expires
import { CachingHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(CachingHeader.CacheControl, "cache-control");
Utilities for HTTP method.
Whether the method is safe method or not.
Defined in RFC 9110, 9.2.1. Safe Methods.
import { isSafeMethod } from "https://deno.land/x/http_utils@$VERSION/method.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isSafeMethod("GET"));
assert(isSafeMethod("HEAD"));
assert(isSafeMethod("OPTIONS"));
assert(isSafeMethod("TRACE"));
Whether the method is idempotent method or not.
Defined in RFC 9110, 9.2.2 Idempotent Methods.
import { isIdempotentMethod } from "https://deno.land/x/http_utils@$VERSION/method.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isIdempotentMethod("GET"));
assert(isIdempotentMethod("PUT"));
assert(isIdempotentMethod("DELETE"));
Whether the method is retrieve method or not.
Retrieve method is following:
- GET
- HEAD
import { isRetrieveMethod } from "https://deno.land/x/http_utils@$VERSION/method.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isRetrieveMethod("GET"));
assert(isRetrieveMethod("HEAD"));
assert(!isRetrieveMethod("POST"));
Utilities for HTTP message.
HTTP message is following union types:
Request
Response
@deprecated Move to request-utils. @deprecated Move to response-utils.
Return an instance with the provided value replacing the specified header. There are no side effects on the original target.
This was inspired by PSR-7: HTTP message interfaces.
Request
:
import { withHeader } from "https://deno.land/x/http_utils@$VERSION/message.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
declare const init: Request;
declare const header: string;
declare const value: string;
const request = withHeader(init, header, value);
assert(request.headers.get(header), value);
assert(init !== request);
Response
:
import { withHeader } from "https://deno.land/x/http_utils@$VERSION/message.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
declare const init: Response;
declare const header: string;
declare const value: string;
const response = withHeader(init, header, value);
assert(response.headers.get(header), value);
assert(init !== response);
Compliant with RFC 9110, 5.6.1. Lists.
Parse list-based fields into array.
Strings enclosed in double quotes are safely handled.
import { parseListFields } from "https://deno.land/x/http_utils@$VERSION/list.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(parseListFields("foo , ,bar,charlie"), [
"foo",
"bar",
"charlie",
]);
assertEquals(parseListFields(`"Sat, 04 May 1996", "Wed, 14 Sep 2005"`), [
`"Sat, 04 May 1996"`,
`"Wed, 14 Sep 2005"`,
]);
Compliant with RFC 9110, 5.6.2. Tokens.
Whether the input is tchar or not.
import { isTchar } from "https://deno.land/x/http_utils@$VERSION/token.ts";
import {
assert,
assertFalse,
} from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isTchar("!"));
assert(isTchar("a"));
assert(isTchar("Z"));
assertFalse(isTchar(""));
Whether the input is token or not.
import { isToken } from "https://deno.land/x/http_utils@$VERSION/token.ts";
import {
assert,
assertFalse,
} from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isToken("token"));
assert(isToken("*!~"));
assertFalse(isToken(""));
Compliant with RFC 9110, 5.6.4. Quoted Strings.
Whether the input is qdtext or not.
import { isQdtext } from "https://deno.land/x/http_utils@$VERSION/quoted_string.ts";
import {
assert,
assertFalse,
} from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isQdtext("\t"));
assert(isQdtext("\xFF"));
assertFalse(isQdtext(`"`));
Whether the input is quoted-pair or not.
import { isQuotedPair } from "https://deno.land/x/http_utils@$VERSION/quoted_string.ts";
import {
assert,
assertFalse,
} from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isQuotedPair("\\\t"));
assert(isQuotedPair("\\\xFF"));
assertFalse(isQuotedPair("\\"));
Whether the input is quoted-string or not.
import { isQuotedString } from "https://deno.land/x/http_utils@$VERSION/quoted_string.ts";
import {
assert,
assertFalse,
} from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isQuotedString(`""`));
assert(isQuotedString(`"qdtext"`));
assert(isQuotedString(`"quoted-pair"`));
assertFalse(isQuotedString(""));
Copyright © 2023-present httpland.
Released under the MIT license