Skip to content

Commit 232307d

Browse files
committed
Changes while reviewing and testing
1 parent f077138 commit 232307d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

rest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const validateContentType = function (req, res, next) {
5959
const isSearchEndpoint = req.path === "/search" || req.path.startsWith("/search/")
6060
if (mimeType === "text/plain" && isSearchEndpoint) return next()
6161
const acceptedTypes = `application/json or application/ld+json${isSearchEndpoint ? ' or text/plain' : ''}`
62-
next(createExpressError({
62+
return next(createExpressError({
6363
statusCode: 415,
6464
statusMessage: `Unsupported Content-Type: ${contentType}. This endpoint requires ${acceptedTypes}.`
6565
}))

routes/__tests__/contentType.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ routeTester.delete("/api/delete/:_id", (req, res) => {
3131
res.status(200).json({ deleted: req.params._id })
3232
})
3333

34+
// PUT endpoint (like /api/update, /api/bulkUpdate)
35+
routeTester.put("/api/update", (req, res) => {
36+
res.status(200).json({ received: req.body })
37+
})
38+
3439
// Release endpoint uses PATCH without a JSON body (uses Slug header)
3540
routeTester.patch("/api/release/:_id", (req, res) => {
3641
res.status(200).json({ released: req.params._id })
@@ -150,4 +155,22 @@ describe("Content-Type validation middleware", () => {
150155
expect(response.body.released).toBe("abc123")
151156
})
152157

158+
it("accepts application/json on PUT endpoint", async () => {
159+
const response = await request(routeTester)
160+
.put("/api/update")
161+
.set("Content-Type", "application/json")
162+
.send({ test: "put-data" })
163+
expect(response.statusCode).toBe(200)
164+
expect(response.body.received.test).toBe("put-data")
165+
})
166+
167+
it("returns 415 for text/plain on PUT endpoint", async () => {
168+
const response = await request(routeTester)
169+
.put("/api/update")
170+
.set("Content-Type", "text/plain")
171+
.send("some text")
172+
expect(response.statusCode).toBe(415)
173+
expect(response.text).toContain("Unsupported Content-Type")
174+
})
175+
153176
})

0 commit comments

Comments
 (0)