mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
fix(http): use non-locale-sensitive string methods for comparison (#6029)
This commit is contained in:
parent
4830d4d4ac
commit
a1f50b4cba
@ -71,7 +71,7 @@ function specify(
|
||||
return;
|
||||
}
|
||||
let s = 0;
|
||||
if (spec.encoding.toLocaleLowerCase() === encoding.toLocaleLowerCase()) {
|
||||
if (spec.encoding.toLowerCase() === encoding.toLowerCase()) {
|
||||
s = 1;
|
||||
} else if (spec.encoding !== "*") {
|
||||
return;
|
||||
|
@ -358,7 +358,7 @@ function parseSetCookie(value: string): Cookie | null {
|
||||
};
|
||||
|
||||
for (const [key, value] of attrs.slice(1)) {
|
||||
switch (key.toLocaleLowerCase()) {
|
||||
switch (key.toLowerCase()) {
|
||||
case "expires":
|
||||
cookie.expires = new Date(value);
|
||||
break;
|
||||
|
@ -1,4 +1,5 @@
|
||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||
import { stub } from "@std/testing/mock";
|
||||
import {
|
||||
deleteCookie,
|
||||
getCookies,
|
||||
@ -615,3 +616,36 @@ Deno.test({
|
||||
assertEquals(getSetCookies(headers), []);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "getSetCookies() is locale independent",
|
||||
fn() {
|
||||
const setCookie = "a=b; EXPIRES=Thu, 19 Sep 2024 07:47:28 GMT";
|
||||
const headers = new Headers({ "set-cookie": setCookie });
|
||||
const expected = [{
|
||||
"name": "a",
|
||||
"value": "b",
|
||||
"expires": new Date("2024-09-19T07:47:28.000Z"),
|
||||
}];
|
||||
|
||||
assertEquals(getSetCookies(headers), expected);
|
||||
|
||||
{
|
||||
/**
|
||||
* Use of locale-sensitive methods with undefined locale may cause
|
||||
* environment-sensitive bugs -
|
||||
* [issue](https://github.com/denoland/std/issues/6016)
|
||||
*/
|
||||
const toLocaleLowerCase = String.prototype.toLocaleLowerCase;
|
||||
using _ = stub(
|
||||
String.prototype,
|
||||
"toLocaleLowerCase",
|
||||
function (locale) {
|
||||
return toLocaleLowerCase.call(this, locale ?? "tr-TR");
|
||||
},
|
||||
);
|
||||
|
||||
assertEquals(getSetCookies(headers), expected);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { accepts, acceptsEncodings, acceptsLanguages } from "./negotiation.ts";
|
||||
import { stub } from "@std/testing/mock";
|
||||
|
||||
Deno.test({
|
||||
name: "accepts() handles no args",
|
||||
@ -109,6 +110,36 @@ Deno.test({
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "acceptsEncodings() is locale independent",
|
||||
fn() {
|
||||
const req = new Request("https://example.com/", {
|
||||
headers: { "accept-encoding": "GZIP" },
|
||||
});
|
||||
const encoding = "gzip";
|
||||
|
||||
assertEquals(acceptsEncodings(req, encoding), encoding);
|
||||
|
||||
{
|
||||
/**
|
||||
* Use of locale-sensitive methods with undefined locale may cause
|
||||
* environment-sensitive bugs -
|
||||
* [issue](https://github.com/denoland/std/issues/6016)
|
||||
*/
|
||||
const toLocaleLowerCase = String.prototype.toLocaleLowerCase;
|
||||
using _ = stub(
|
||||
String.prototype,
|
||||
"toLocaleLowerCase",
|
||||
function (locale) {
|
||||
return toLocaleLowerCase.call(this, locale ?? "tr-TR");
|
||||
},
|
||||
);
|
||||
|
||||
assertEquals(acceptsEncodings(req, encoding), encoding);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "acceptsLanguages() handles no args",
|
||||
fn() {
|
||||
|
Loading…
Reference in New Issue
Block a user