Test missing accept-language endpoint

This commit is contained in:
Zoe Roux 2024-12-19 16:20:35 +01:00
parent 3a7a12bfd3
commit a4853cb186
No known key found for this signature in database
2 changed files with 18 additions and 5 deletions

View File

@ -49,7 +49,8 @@ export const Language = (props?: StringProps) =>
...props, ...props,
}); });
export const processLanguages = (languages: string) => { export const processLanguages = (languages?: string) => {
if (!languages) return ["*"];
return languages return languages
.split(",") .split(",")
.map((x) => { .map((x) => {

View File

@ -9,13 +9,15 @@ import { shows } from "~/db/schema";
import { bubble } from "~/models/examples"; import { bubble } from "~/models/examples";
const app = new Elysia().use(base).use(movies); const app = new Elysia().use(base).use(movies);
const getMovie = async (id: string, langs: string) => { const getMovie = async (id: string, langs?: string) => {
const resp = await app.handle( const resp = await app.handle(
new Request(`http://localhost/movies/${id}`, { new Request(`http://localhost/movies/${id}`, {
method: "GET", method: "GET",
headers: { headers: langs
? {
"Accept-Language": langs, "Accept-Language": langs,
}, }
: {},
}), }),
); );
const body = await resp.json(); const body = await resp.json();
@ -74,6 +76,16 @@ describe("Get movie", () => {
it("Use language fallback", async () => { it("Use language fallback", async () => {
const [resp, body] = await getMovie(bubble.slug, "fr,ja,*"); const [resp, body] = await getMovie(bubble.slug, "fr,ja,*");
expectStatus(resp, body).toBe(200);
expect(body).toMatchObject({
slug: bubble.slug,
name: bubble.translations.en.name,
});
expect(resp.headers.get("Content-Language")).toBe("en");
});
it("Works without accept-language header", async () => {
const [resp, body] = await getMovie(bubble.slug, undefined);
expectStatus(resp, body).toBe(200); expectStatus(resp, body).toBe(200);
expect(body).toMatchObject({ expect(body).toMatchObject({
slug: bubble.slug, slug: bubble.slug,