| 1 |
import { Server, Response } from "miragejs"; |
| 2 |
|
| 3 |
describe("Integration | Server | Custom responses", function () { |
| 4 |
let server; |
| 5 |
|
| 6 |
beforeEach(function () { |
| 7 |
server = new Server({ |
| 8 |
environment: "test", |
| 9 |
}); |
| 10 |
server.timing = 0; |
| 11 |
server.logging = false; |
| 12 |
}); |
| 13 |
|
| 14 |
afterEach(function () { |
| 15 |
server.shutdown(); |
| 16 |
}); |
| 17 |
|
| 18 |
test("GET to an empty Response defaults to 200 and an empty json object", async () => { |
| 19 |
server.get("/example", function () { |
| 20 |
return new Response(); |
| 21 |
}); |
| 22 |
|
| 23 |
let res = await fetch("/example"); |
| 24 |
let data = await res.json(); |
| 25 |
|
| 26 |
expect(data).toEqual({}); |
| 27 |
expect(res.status).toEqual(200); |
| 28 |
expect([...res.headers.entries()]).toEqual([ |
| 29 |
["content-type", "application/json"], |
| 30 |
]); |
| 31 |
}); |
| 32 |
|
| 33 |
test("GET to a 200 Response responds with an empty json object", async () => { |
| 34 |
server.get("/example", function () { |
| 35 |
return new Response(200); |
| 36 |
}); |
| 37 |
|
| 38 |
let res = await fetch("/example"); |
| 39 |
let data = await res.json(); |
| 40 |
|
| 41 |
expect(data).toEqual({}); |
| 42 |
expect(res.status).toEqual(200); |
| 43 |
expect([...res.headers.entries()]).toEqual([ |
| 44 |
["content-type", "application/json"], |
| 45 |
]); |
| 46 |
}); |
| 47 |
|
| 48 |
test("a 204 Response responds with an empty body", async () => { |
| 49 |
server.post("/example", function () { |
| 50 |
return new Response(204); |
| 51 |
}); |
| 52 |
|
| 53 |
let res = await fetch("/example", { method: "POST" }); |
| 54 |
let text = await res.text(); |
| 55 |
|
| 56 |
expect(text).toEqual(""); |
| 57 |
expect(res.status).toEqual(204); |
| 58 |
expect([...res.headers.entries()]).toEqual([ |
| 59 |
["content-type", "text/plain;charset=UTF-8"], |
| 60 |
]); |
| 61 |
}); |
| 62 |
}); |