|
| 1 | +import { jest } from "@jest/globals" |
| 2 | +import express from "express" |
| 3 | +import request from "supertest" |
| 4 | +import rest from '../../rest.js' |
| 5 | + |
| 6 | +// Set up a minimal Express app with the Content-Type validation middleware |
| 7 | +const routeTester = new express() |
| 8 | +routeTester.use(express.json({ type: ["application/json", "application/ld+json"] })) |
| 9 | +routeTester.use(express.text()) |
| 10 | +routeTester.use(express.urlencoded({ extended: false })) |
| 11 | + |
| 12 | +// Mount the validateContentType middleware on /api just like api-routes.js |
| 13 | +routeTester.use("/api", rest.validateContentType) |
| 14 | + |
| 15 | +// Simple JSON-only endpoint (like /api/create, /api/query, etc.) |
| 16 | +routeTester.post("/api/create", (req, res) => { |
| 17 | + res.status(200).json({ received: req.body }) |
| 18 | +}) |
| 19 | + |
| 20 | +// Search endpoint that accepts text/plain |
| 21 | +routeTester.post("/api/search", (req, res) => { |
| 22 | + res.status(200).json({ received: req.body }) |
| 23 | +}) |
| 24 | + |
| 25 | +// GET endpoint should pass through without Content-Type validation |
| 26 | +routeTester.get("/api/info", (req, res) => { |
| 27 | + res.status(200).json({ info: true }) |
| 28 | +}) |
| 29 | + |
| 30 | +// Error handler matching the app's pattern |
| 31 | +routeTester.use(rest.messenger) |
| 32 | + |
| 33 | +describe("Content-Type validation middleware", () => { |
| 34 | + |
| 35 | + it("accepts application/json with valid JSON body", async () => { |
| 36 | + const response = await request(routeTester) |
| 37 | + .post("/api/create") |
| 38 | + .set("Content-Type", "application/json") |
| 39 | + .send({ test: "data" }) |
| 40 | + expect(response.statusCode).toBe(200) |
| 41 | + expect(response.body.received.test).toBe("data") |
| 42 | + }) |
| 43 | + |
| 44 | + it("accepts application/ld+json with valid JSON body", async () => { |
| 45 | + const response = await request(routeTester) |
| 46 | + .post("/api/create") |
| 47 | + .set("Content-Type", "application/ld+json") |
| 48 | + .send(JSON.stringify({ "@context": "http://example.org", test: "ld" })) |
| 49 | + expect(response.statusCode).toBe(200) |
| 50 | + expect(response.body.received["@context"]).toBe("http://example.org") |
| 51 | + }) |
| 52 | + |
| 53 | + it("accepts application/json with charset parameter", async () => { |
| 54 | + const response = await request(routeTester) |
| 55 | + .post("/api/create") |
| 56 | + .set("Content-Type", "application/json; charset=utf-8") |
| 57 | + .send({ test: "charset" }) |
| 58 | + expect(response.statusCode).toBe(200) |
| 59 | + }) |
| 60 | + |
| 61 | + it("returns 415 for missing Content-Type header", async () => { |
| 62 | + const response = await request(routeTester) |
| 63 | + .post("/api/create") |
| 64 | + .unset("Content-Type") |
| 65 | + .send(Buffer.from('{"test":"data"}')) |
| 66 | + expect(response.statusCode).toBe(415) |
| 67 | + expect(response.text).toContain("Missing Content-Type header") |
| 68 | + }) |
| 69 | + |
| 70 | + it("returns 415 for text/plain on JSON-only endpoint", async () => { |
| 71 | + const response = await request(routeTester) |
| 72 | + .post("/api/create") |
| 73 | + .set("Content-Type", "text/plain") |
| 74 | + .send("some plain text") |
| 75 | + expect(response.statusCode).toBe(415) |
| 76 | + expect(response.text).toContain("Unsupported Content-Type") |
| 77 | + }) |
| 78 | + |
| 79 | + it("returns 415 for application/xml", async () => { |
| 80 | + const response = await request(routeTester) |
| 81 | + .post("/api/create") |
| 82 | + .set("Content-Type", "application/xml") |
| 83 | + .send("<root/>") |
| 84 | + expect(response.statusCode).toBe(415) |
| 85 | + expect(response.text).toContain("Unsupported Content-Type") |
| 86 | + }) |
| 87 | + |
| 88 | + it("allows text/plain on search endpoint", async () => { |
| 89 | + const response = await request(routeTester) |
| 90 | + .post("/api/search") |
| 91 | + .set("Content-Type", "text/plain") |
| 92 | + .send("search terms") |
| 93 | + expect(response.statusCode).toBe(200) |
| 94 | + expect(response.body.received).toBe("search terms") |
| 95 | + }) |
| 96 | + |
| 97 | + it("allows application/json on search endpoint", async () => { |
| 98 | + const response = await request(routeTester) |
| 99 | + .post("/api/search") |
| 100 | + .set("Content-Type", "application/json") |
| 101 | + .send({ searchText: "hello" }) |
| 102 | + expect(response.statusCode).toBe(200) |
| 103 | + expect(response.body.received.searchText).toBe("hello") |
| 104 | + }) |
| 105 | + |
| 106 | + it("skips validation for GET requests", async () => { |
| 107 | + const response = await request(routeTester) |
| 108 | + .get("/api/info") |
| 109 | + expect(response.statusCode).toBe(200) |
| 110 | + expect(response.body.info).toBe(true) |
| 111 | + }) |
| 112 | + |
| 113 | +}) |
0 commit comments