Skip to content

Commit 8f6deb1

Browse files
committed
test(preconditions): add test case
1 parent 27b56b6 commit 8f6deb1

File tree

7 files changed

+62
-19
lines changed

7 files changed

+62
-19
lines changed

_tools/meta.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ export const makeOptions = (version: string): BuildOptions => ({
6969
version: "1.1.1",
7070
subPath: "number/is_negative_number",
7171
},
72-
"https://deno.land/x/[email protected]/date/is_valid_date.ts": {
73-
name: "@miyauci/isx",
74-
version: "1.1.1",
75-
subPath: "date/is_valid_date",
76-
},
7772
"https://deno.land/x/[email protected]/mod.ts": {
7873
name: "@httpland/http-middleware",
7974
version: "1.0.0",

deps.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export { isNull } from "https://deno.land/x/[email protected]/is_null.ts";
1212
export { isString } from "https://deno.land/x/[email protected]/is_string.ts";
1313
export { isNumber } from "https://deno.land/x/[email protected]/is_number.ts";
1414
export { isNegativeNumber } from "https://deno.land/x/[email protected]/number/is_negative_number.ts";
15-
export { isValidDate } from "https://deno.land/x/[email protected]/date/is_valid_date.ts";
1615
export { trim } from "https://deno.land/x/[email protected]/trim.ts";
1716
export { toLowerCase } from "https://deno.land/x/[email protected]/to_lower_case.ts";
1817
export {

preconditions/if_match_test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ describe("IfMatch", () => {
2929
new Request("test:"),
3030
new Response(null, { headers: { [RepresentationHeader.ETag]: "" } }),
3131
],
32+
[
33+
new Request("test:"),
34+
new Response(null, {
35+
headers: {
36+
[ConditionalHeader.IfMatch]: "",
37+
[RepresentationHeader.ETag]: "",
38+
},
39+
}),
40+
],
3241
];
3342

3443
table.forEach(([request, response]) => {

preconditions/if_modified_since_test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ describe("IfModifiedSince", () => {
7777
headers: { [RepresentationHeader.LastModified]: "" },
7878
}),
7979
],
80+
[
81+
new Request("test:"),
82+
new Response(null, {
83+
headers: {
84+
[ConditionalHeader.IfModifiedSince]: "",
85+
[RepresentationHeader.LastModified]: "",
86+
},
87+
}),
88+
],
8089
];
8190

8291
table.forEach(([request, response]) => {

preconditions/if_none_match_test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ describe("IfNoneMatch", () => {
3030
new Request("test:"),
3131
new Response(null, { headers: { [RepresentationHeader.ETag]: "" } }),
3232
],
33+
[
34+
new Request("test:"),
35+
new Response(null, {
36+
headers: {
37+
[ConditionalHeader.IfNoneMatch]: "",
38+
[RepresentationHeader.ETag]: "",
39+
},
40+
}),
41+
],
3342
];
3443

3544
table.forEach(([request, response]) => {

preconditions/utils.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
import {
55
compareStrong,
66
compareWeak,
7+
isErr,
78
isString,
8-
isValidDate,
99
parseETag,
1010
parseHttpDate,
11+
unsafe,
1112
} from "../deps.ts";
1213
import { parse } from "../if_match.ts";
1314

@@ -50,18 +51,21 @@ export function ifModifiedSince(
5051
fieldValue: string,
5152
lastModified: string,
5253
): boolean {
53-
const date = parseHttpDate(fieldValue);
54+
const dateContainer = unsafe(() => parseHttpDate(fieldValue));
5455

55-
if (!isValidDate(date)) {
56+
if (isErr(dateContainer)) {
5657
throw SyntaxError(Msg.InvalidField);
5758
}
5859

59-
const lastMod = parseHttpDate(lastModified);
60+
const date = dateContainer.value;
61+
const lastModContainer = unsafe(() => parseHttpDate(lastModified));
6062

61-
if (!isValidDate(lastMod)) {
63+
if (isErr(lastModContainer)) {
6264
throw SyntaxError(Msg.InvalidLastModified);
6365
}
6466

67+
const lastMod = lastModContainer.value;
68+
6569
// The origin server SHOULD NOT perform the requested
6670
// method if the selected representation's last modification date is
6771
// earlier than or equal to the date provided in the field-value;
@@ -75,18 +79,21 @@ export function ifUnmodifiedSince(
7579
fieldValue: string,
7680
lastModified: string,
7781
): boolean {
78-
const date = parseHttpDate(fieldValue);
82+
const dateContainer = unsafe(() => parseHttpDate(fieldValue));
7983

80-
if (!isValidDate(date)) {
84+
if (isErr(dateContainer)) {
8185
throw SyntaxError(Msg.InvalidField);
8286
}
8387

84-
const lastMod = parseHttpDate(lastModified);
88+
const lastModContainer = unsafe(() => parseHttpDate(lastModified));
8589

86-
if (!isValidDate(lastMod)) {
90+
if (isErr(lastModContainer)) {
8791
throw SyntaxError(Msg.InvalidLastModified);
8892
}
8993

94+
const date = dateContainer.value;
95+
const lastMod = lastModContainer.value;
96+
9097
// The origin server MUST NOT perform the requested method
9198
// if the selected representation's last modification date is more
9299
// recent than the date provided in the field-value;
@@ -114,18 +121,21 @@ export function ifRange(fieldValue: string, headers: IfRangeHeaders): boolean {
114121

115122
if (!isString(lastModified)) throw Error();
116123

117-
const left = parseHttpDate(fieldValue);
124+
const leftContainer = unsafe(() => parseHttpDate(fieldValue));
118125

119-
if (!isValidDate(left)) {
126+
if (isErr(leftContainer)) {
120127
throw SyntaxError(Msg.InvalidField);
121128
}
122129

123-
const right = parseHttpDate(lastModified);
130+
const rightContainer = unsafe(() => parseHttpDate(lastModified));
124131

125-
if (!isValidDate(right)) {
132+
if (isErr(rightContainer)) {
126133
throw SyntaxError(Msg.InvalidLastModified);
127134
}
128135

136+
const left = leftContainer.value;
137+
const right = rightContainer.value;
138+
129139
return left.getTime() === right.getTime();
130140
}
131141

preconditions/utils_test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ describe("ifModifiedSince", () => {
125125
assertThrows(() => ifModifiedSince(filedValue, lastModified));
126126
});
127127
});
128+
129+
it("should return false if the input is invalid syntax", () => {
130+
const table: [string, string][] = [
131+
["", "Mon, 06 Mar 2023 12:00:00 GMT"],
132+
["Mon, 06 Mar 2023 12:00:00 GMT", ""],
133+
];
134+
135+
table.forEach(([filedValue, etag]) => {
136+
assertThrows(() => ifModifiedSince(filedValue, etag));
137+
});
138+
});
128139
});
129140

130141
describe("ifUnmodifiedSince", () => {
@@ -221,6 +232,7 @@ describe("ifRange", () => {
221232
["", { etag: "" }],
222233
["", { etag: "", lastModified: "" }],
223234
[`""`, { etag: `"` }],
235+
[`""`, {}],
224236
[`Sep`, { lastModified: "" }],
225237
];
226238

0 commit comments

Comments
 (0)