Fix invalid feed characters (#31)
This commit is contained in:
parent
29cb0addd7
commit
eb4304da71
|
|
@ -0,0 +1,16 @@
|
||||||
|
import { assertEquals, assertNotEquals } from "$std/assert/mod.ts";
|
||||||
|
import { responseJSON, responseXML } from "./utils.ts";
|
||||||
|
|
||||||
|
Deno.test("JSON response is stringified", async () => {
|
||||||
|
const body = { message: "Hello, World!" };
|
||||||
|
const response = responseJSON(body, 200);
|
||||||
|
assertEquals(await response.text(), JSON.stringify(body));
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("XML resopnse is _not_ stringified", async () => {
|
||||||
|
const body = "<message>Hello, World!</message>";
|
||||||
|
const response = responseXML(body, 200);
|
||||||
|
const responseBody = await response.text();
|
||||||
|
assertEquals(responseBody, body);
|
||||||
|
assertNotEquals(responseBody, JSON.stringify(body));
|
||||||
|
});
|
||||||
|
|
@ -14,15 +14,16 @@ type EnumValues<T> = T[keyof T];
|
||||||
type Status = EnumValues<typeof STATUS_CODE>;
|
type Status = EnumValues<typeof STATUS_CODE>;
|
||||||
|
|
||||||
export function responseJSON(body: unknown | null, status: Status) {
|
export function responseJSON(body: unknown | null, status: Status) {
|
||||||
return response(body, status, "json");
|
const stringifiedBody = JSON.stringify(body);
|
||||||
|
return response(stringifiedBody, status, "json");
|
||||||
}
|
}
|
||||||
|
|
||||||
export function responseXML(body: unknown | null, status: Status) {
|
export function responseXML(body: string, status: Status) {
|
||||||
return response(body, status, "xml");
|
return response(body, status, "xml");
|
||||||
}
|
}
|
||||||
|
|
||||||
function response(body: unknown | null, status: number, type: "json" | "xml") {
|
function response(body: string, status: number, type: "json" | "xml") {
|
||||||
return new Response(JSON.stringify(body), {
|
return new Response(body, {
|
||||||
status,
|
status,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": `application/${type}`,
|
"Content-Type": `application/${type}`,
|
||||||
|
|
|
||||||
|
|
@ -12,5 +12,7 @@ export const handler = async (_req: Request, ctx: FreshContext): Promise<Respons
|
||||||
}
|
}
|
||||||
const feed = rss.assembleFeed(series);
|
const feed = rss.assembleFeed(series);
|
||||||
|
|
||||||
return responseXML(feed, STATUS_CODE.OK);
|
const xml = responseXML(feed, STATUS_CODE.OK);
|
||||||
|
|
||||||
|
return xml;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue