| 1 |
import { Server } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Browser only | Passthrough", () => { |
| 4 |
let server, originalError; |
| 5 |
|
| 6 |
beforeEach(() => { |
| 7 |
server = new Server({ |
| 8 |
environment: "test", |
| 9 |
}); |
| 10 |
|
| 11 |
/* |
| 12 |
Waiting to hear back on this: |
| 13 |
|
| 14 |
https://stackoverflow.com/questions/57227095/how-can-i-catch-or-suppress-a-rejected-network-request-from-jest |
| 15 |
|
| 16 |
For now, suppress console error messages |
| 17 |
*/ |
| 18 |
originalError = console.error; |
| 19 |
console.error = () => {}; |
| 20 |
}); |
| 21 |
|
| 22 |
afterEach(() => { |
| 23 |
server.shutdown(); |
| 24 |
console.error = originalError; |
| 25 |
}); |
| 26 |
|
| 27 |
test("it can passthrough individual paths", async () => { |
| 28 |
expect.assertions(2); |
| 29 |
|
| 30 |
server.loadConfig(function () { |
| 31 |
this.get("/contacts", function () { |
| 32 |
return 123; |
| 33 |
}); |
| 34 |
this.passthrough("/addresses"); |
| 35 |
}); |
| 36 |
|
| 37 |
let res = await fetch("/contacts"); |
| 38 |
let data = await res.json(); |
| 39 |
expect(data).toEqual(123); |
| 40 |
|
| 41 |
await expect(fetch("/addresses")).rejects.toThrow(`Network request failed`); |
| 42 |
}); |
| 43 |
|
| 44 |
test("it can passthrough certain verbs for individual paths", async () => { |
| 45 |
expect.assertions(3); |
| 46 |
|
| 47 |
server.loadConfig(function () { |
| 48 |
this.get("/contacts", function () { |
| 49 |
return 123; |
| 50 |
}); |
| 51 |
this.passthrough("/addresses", ["post"]); |
| 52 |
}); |
| 53 |
|
| 54 |
let res = await fetch("/contacts"); |
| 55 |
let data = await res.json(); |
| 56 |
expect(data).toEqual(123); |
| 57 |
|
| 58 |
await expect(fetch("/addresses")).rejects.toThrow( |
| 59 |
`Mirage: Your app tried to GET '/addresses', but there was no route defined to handle this request` |
| 60 |
); |
| 61 |
|
| 62 |
await expect(fetch("/addresses", { method: "POST" })).rejects.toThrow( |
| 63 |
`Network request failed` |
| 64 |
); |
| 65 |
}); |
| 66 |
|
| 67 |
test("it can passthrough all verbs by default", async () => { |
| 68 |
let verbs = ["GET", "HEAD", "PUT", "POST", "PATCH", "DELETE", "OPTIONS"]; |
| 69 |
expect.assertions(7); |
| 70 |
|
| 71 |
server.loadConfig(function () { |
| 72 |
this.passthrough("/addresses"); |
| 73 |
}); |
| 74 |
|
| 75 |
for (let method of verbs) { |
| 76 |
await expect(fetch("/addresses", { method })).rejects.toThrow( |
| 77 |
"Network request failed" |
| 78 |
); |
| 79 |
} |
| 80 |
}); |
| 81 |
|
| 82 |
test("it can passthrough multiple paths in a single call", async () => { |
| 83 |
expect.assertions(2); |
| 84 |
|
| 85 |
server.loadConfig(function () { |
| 86 |
this.get("/contacts", function () { |
| 87 |
return 123; |
| 88 |
}); |
| 89 |
this.passthrough("/contacts", "/addresses"); |
| 90 |
}); |
| 91 |
|
| 92 |
await expect(fetch("/contacts")).rejects.toThrow("Network request failed"); |
| 93 |
await expect(fetch("/addresses")).rejects.toThrow("Network request failed"); |
| 94 |
}); |
| 95 |
|
| 96 |
test("user can call passthrough multiple times", async () => { |
| 97 |
expect.assertions(2); |
| 98 |
|
| 99 |
server.loadConfig(function () { |
| 100 |
this.passthrough("/contacts"); |
| 101 |
this.passthrough("/addresses", ["post"]); |
| 102 |
}); |
| 103 |
|
| 104 |
await expect(fetch("/contacts")).rejects.toThrow("Network request failed"); |
| 105 |
await expect(fetch("/addresses", { method: "POST" })).rejects.toThrow( |
| 106 |
"Network request failed" |
| 107 |
); |
| 108 |
}); |
| 109 |
|
| 110 |
test("passthrough without args allows all paths on the current domain to passthrough", async () => { |
| 111 |
expect.assertions(2); |
| 112 |
|
| 113 |
server.loadConfig(function () { |
| 114 |
this.get("/contacts", function () { |
| 115 |
return 123; |
| 116 |
}); |
| 117 |
this.passthrough(); |
| 118 |
}); |
| 119 |
|
| 120 |
let res = await fetch("/contacts"); |
| 121 |
let data = await res.json(); |
| 122 |
expect(data).toEqual(123); |
| 123 |
|
| 124 |
await expect(fetch("/addresses")).rejects.toThrow("Network request failed"); |
| 125 |
}); |
| 126 |
|
| 127 |
test("passthrough without args allows index route on current domain to passthrough", async () => { |
| 128 |
expect.assertions(2); |
| 129 |
|
| 130 |
server.loadConfig(function () { |
| 131 |
this.get("/contacts", function () { |
| 132 |
return 123; |
| 133 |
}); |
| 134 |
this.passthrough(); |
| 135 |
}); |
| 136 |
|
| 137 |
let res = await fetch("/contacts"); |
| 138 |
let data = await res.json(); |
| 139 |
expect(data).toEqual(123); |
| 140 |
|
| 141 |
await expect(fetch("/")).rejects.toThrow("Network request failed"); |
| 142 |
}); |
| 143 |
|
| 144 |
test("it can passthrough other-origin hosts", async () => { |
| 145 |
expect.assertions(1); |
| 146 |
|
| 147 |
server.loadConfig(function () { |
| 148 |
this.passthrough("http://api.foo.bar/**"); |
| 149 |
}); |
| 150 |
|
| 151 |
await expect(fetch("http://api.foo.bar/addresses")).rejects.toThrow( |
| 152 |
"Network request failed" |
| 153 |
); |
| 154 |
}); |
| 155 |
|
| 156 |
test("it can take a function", async () => { |
| 157 |
server.config({ |
| 158 |
routes() { |
| 159 |
this.passthrough((request) => { |
| 160 |
return request.url.match(/users/); |
| 161 |
}); |
| 162 |
}, |
| 163 |
}); |
| 164 |
|
| 165 |
await expect(fetch("/users?test=withQueryParams")).rejects.toThrow( |
| 166 |
"Network request failed" |
| 167 |
); |
| 168 |
|
| 169 |
await expect(fetch("/movies")).rejects.toThrow( |
| 170 |
`Mirage: Your app tried to GET '/movies'` |
| 171 |
); |
| 172 |
}); |
| 173 |
|
| 174 |
test("it passes through common build tool-related paths", async () => { |
| 175 |
await expect(fetch("/abc.hot-update.json")).rejects.toThrow( |
| 176 |
"Network request failed" |
| 177 |
); |
| 178 |
await expect(fetch("/movies")).rejects.toThrow( |
| 179 |
`Mirage: Your app tried to GET '/movies'` |
| 180 |
); |
| 181 |
|
| 182 |
await expect(fetch("/def.hot-update.json")).rejects.toThrow( |
| 183 |
"Network request failed" |
| 184 |
); |
| 185 |
await expect(fetch("/movies")).rejects.toThrow( |
| 186 |
`Mirage: Your app tried to GET '/movies'` |
| 187 |
); |
| 188 |
}); |
| 189 |
}); |
| 190 |
|
| 191 |
test("a new server created with useDefaultPassthroughs set to false ignores default passthrougsh", async () => { |
| 192 |
let server = new Server({ |
| 193 |
useDefaultPassthroughs: false, |
| 194 |
}); |
| 195 |
|
| 196 |
await expect(fetch("/abc.hot-update.json")).rejects.toThrow( |
| 197 |
"Mirage: Your app tried to GET '/abc.hot-update.json'" |
| 198 |
); |
| 199 |
|
| 200 |
server.shutdown(); |
| 201 |
}); |