Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions tests/caching-tests.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before landing this test, we should make sure that running the tests locally doesn't make requests to stage.

Afaik, if we just define the test like this, then node --test . would execute it.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { describe, it } from "node:test";
import assert from "node:assert";

const BASE_URL = "https://developer.allizom.org/";

describe("redirectPreferredLocale", () => {
it("should redirect if preferredlocale is set", async () => {
let response = await fetch(new URL("/en-US/docs/Web", BASE_URL));
assert.ok(response.url.endsWith("/en-US/docs/Web"), response.url);
Comment on lines +8 to +9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to skip the test instead (if that URL is not available)?


response = await fetch(new URL("/en-US/docs/Web", BASE_URL), {
headers: new Headers({
cookie: "preferredlocale=fr",
}),
});
assert.ok(response.url.endsWith("/fr/docs/Web"), response.url);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.ok(response.url.endsWith("/fr/docs/Web"), response.url);
assert.strictEqual("/fr/docs/Web", URL.parse(response.url).pathname);

});

it("shouldn't redirect if page doesn't exist in preferred locale", async () => {
let response = await fetch(
new URL("/en-US/docs/MDN/Kitchensink", BASE_URL)
);
assert.ok(
response.url.endsWith("/en-US/docs/MDN/Kitchensink"),
response.url
);

response = await fetch(new URL("/en-US/docs/MDN/Kitchensink", BASE_URL), {
headers: new Headers({
cookie: "preferredlocale=fr",
}),
});
assert.ok(
response.url.endsWith("/en-US/docs/MDN/Kitchensink"),
response.url
);
});
});

describe("redirectLocale", () => {
it("should redirect to preferredlocale", async () => {
let response = await fetch(new URL("/", BASE_URL), {
headers: new Headers({
cookie: "preferredlocale=en-US",
}),
});
assert.ok(response.url.endsWith("/en-US/"), response.url);

response = await fetch(new URL("/", BASE_URL), {
headers: new Headers({
cookie: "preferredlocale=fr",
}),
});
assert.ok(response.url.endsWith("/fr/"), response.url);
});

it("should redirect to Accept-Language", async () => {
let response = await fetch(new URL("/", BASE_URL), {
headers: new Headers({
"accept-language": "en-US",
}),
});
assert.ok(response.url.endsWith("/en-US/"), response.url);

response = await fetch(new URL("/", BASE_URL), {
headers: new Headers({
"accept-language": "fr",
}),
});
assert.ok(response.url.endsWith("/fr/"), response.url);
});
});