Fix invalid feed characters #31
|
|
@ -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>;
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
function response(body: unknown | null, status: number, type: "json" | "xml") {
|
||||
return new Response(JSON.stringify(body), {
|
||||
function response(body: string, status: number, type: "json" | "xml") {
|
||||
return new Response(body, {
|
||||
status,
|
||||
headers: {
|
||||
"Content-Type": `application/${type}`,
|
||||
|
|
|
|||
|
|
@ -11,6 +11,10 @@ export const handler = async (_req: Request, ctx: FreshContext): Promise<Respons
|
|||
return responseJSON({ message: "Series not found" }, STATUS_CODE.NotFound);
|
||||
|
olaven
commented
Review
```suggestion
```
olaven
commented
Review
```suggestion
```
|
||||
}
|
||||
const feed = rss.assembleFeed(series);
|
||||
console.log(feed);
|
||||
|
||||
return responseXML(feed, STATUS_CODE.OK);
|
||||
const xml = responseXML(feed, STATUS_CODE.OK);
|
||||
console.log({ feed, xml });
|
||||
|
||||
return xml;
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue