| 3 | import { setupServer } from "msw/node"; |
| 4 | |
| 5 | class MockServer { |
| 6 | etag?: string; |
| 7 | numRequests: number; |
| 8 | lastCache?: string; |
| 9 | lastRequestHeaders: Headers | null; |
| 10 | lastCredentials?: string; |
| 11 | |
| 12 | reset() { |
| 13 | this.numRequests = 0; |
| 14 | this.etag = undefined; |
| 15 | this.lastRequestHeaders = null; |
| 16 | } |
| 17 | |
| 18 | constructor() { |
| 19 | this.numRequests = 0; |
| 20 | this.etag = undefined; |
| 21 | this.lastRequestHeaders = null; |
| 22 | const serverBuffer = fs.readFileSync("test/data/test_fixture_1.pmtiles"); |
| 23 | const serverBufferMlt = fs.readFileSync( |
| 24 | "test/data/test_fixture_mlt.pmtiles" |
| 25 | ); |
| 26 | const server = setupServer( |
| 27 | http.get( |
| 28 | "http://localhost:1337/example.pmtiles", |
| 29 | ({ request, params }) => { |
| 30 | this.lastCache = request.cache; |
| 31 | this.lastRequestHeaders = request.headers; |
| 32 | this.lastCredentials = request.credentials; |
| 33 | this.numRequests++; |
| 34 | const range = request.headers.get("range")?.substr(6).split("-"); |
| 35 | if (!range) { |
| 36 | throw new Error("invalid range"); |
| 37 | } |
| 38 | const offset = +range[0]; |
| 39 | const length = +range[1]; |
| 40 | const body = serverBuffer.slice(offset, offset + length - 1); |
| 41 | return new HttpResponse(body, { |
| 42 | status: 206, |
| 43 | statusText: "OK", |
| 44 | headers: { etag: this.etag } as HeadersInit, |
| 45 | }); |
| 46 | } |
| 47 | ), |
| 48 | http.get("http://localhost:1337/small.pmtiles", ({ request }) => { |
| 49 | this.lastRequestHeaders = request.headers; |
| 50 | this.lastCredentials = request.credentials; |
| 51 | this.numRequests++; |
| 52 | const range = request.headers.get("range")?.substr(6).split("-"); |
| 53 | if (!range) { |
| 54 | throw new Error("invalid range"); |
| 55 | } |
| 56 | const rangeEnd = +range[1]; |
| 57 | if (rangeEnd >= serverBuffer.length) { |
| 58 | return new HttpResponse(null, { |
| 59 | status: 416, |
| 60 | headers: { |
| 61 | "Content-Range": `bytes */${serverBuffer.length}`, |
| 62 | }, |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…