|
7 | 7 | equalsResponse, |
8 | 8 | it, |
9 | 9 | Method, |
| 10 | + RangeHeader, |
10 | 11 | RepresentationHeader, |
11 | 12 | spy, |
12 | 13 | Status, |
@@ -373,6 +374,92 @@ describe("conditionalRequest", () => { |
373 | 374 | assert(response === initResponse); |
374 | 375 | }); |
375 | 376 |
|
| 377 | + it("should return 206 response if request has if-range and range header and match etag", async () => { |
| 378 | + const ETAG = `""`; |
| 379 | + const selectRepresentation = spy(() => |
| 380 | + new Response("abcdef", { |
| 381 | + headers: { [RepresentationHeader.ETag]: ETAG }, |
| 382 | + }) |
| 383 | + ); |
| 384 | + const middleware = conditionalRequest(selectRepresentation); |
| 385 | + const request = new Request("test:", { |
| 386 | + headers: { |
| 387 | + [ConditionalHeader.IfRange]: ETAG, |
| 388 | + [RangeHeader.Range]: "bytes=-3", |
| 389 | + }, |
| 390 | + }); |
| 391 | + const handler = spy(() => new Response()); |
| 392 | + |
| 393 | + const response = await middleware(request, handler); |
| 394 | + |
| 395 | + assertSpyCalls(selectRepresentation, 1); |
| 396 | + assertSpyCalls(handler, 0); |
| 397 | + |
| 398 | + assert( |
| 399 | + await equalsResponse( |
| 400 | + response, |
| 401 | + new Response("def", { |
| 402 | + status: Status.PartialContent, |
| 403 | + headers: { |
| 404 | + [RangeHeader.ContentRange]: "bytes 3-5/6", |
| 405 | + [RepresentationHeader.ETag]: ETAG, |
| 406 | + }, |
| 407 | + }), |
| 408 | + true, |
| 409 | + ), |
| 410 | + ); |
| 411 | + }); |
| 412 | + |
| 413 | + it("should return 206 response if request has if-range and range header and match last-modified", async () => { |
| 414 | + const lastModified = new Date("2000/1/1").toUTCString(); |
| 415 | + const selectRepresentation = spy(() => |
| 416 | + new Response("abcdef", { |
| 417 | + headers: { [RepresentationHeader.LastModified]: lastModified }, |
| 418 | + }) |
| 419 | + ); |
| 420 | + const middleware = conditionalRequest(selectRepresentation); |
| 421 | + const request = new Request("test:", { |
| 422 | + headers: { |
| 423 | + [ConditionalHeader.IfRange]: lastModified, |
| 424 | + [RangeHeader.Range]: "bytes=4-, -3", |
| 425 | + }, |
| 426 | + }); |
| 427 | + const handler = spy(() => new Response()); |
| 428 | + const response = await middleware(request, handler); |
| 429 | + const BOUNDARY = "1f8ac10f23c5b5bc1167bda84b833e5c057a77d2"; |
| 430 | + |
| 431 | + assertSpyCalls(selectRepresentation, 1); |
| 432 | + assertSpyCalls(handler, 0); |
| 433 | + |
| 434 | + assert( |
| 435 | + await equalsResponse( |
| 436 | + response, |
| 437 | + new Response( |
| 438 | + `--${BOUNDARY} |
| 439 | +Content-Type: text/plain;charset=UTF-8 |
| 440 | +Content-Range: bytes 4-5/6 |
| 441 | +
|
| 442 | +ef |
| 443 | +--${BOUNDARY} |
| 444 | +Content-Type: text/plain;charset=UTF-8 |
| 445 | +Content-Range: bytes 3-5/6 |
| 446 | +
|
| 447 | +def |
| 448 | +--${BOUNDARY}--`, |
| 449 | + { |
| 450 | + status: Status.PartialContent, |
| 451 | + headers: { |
| 452 | + [RepresentationHeader.LastModified]: lastModified, |
| 453 | + [RepresentationHeader.ContentType]: |
| 454 | + `multipart/byteranges; boundary=${BOUNDARY}`, |
| 455 | + }, |
| 456 | + }, |
| 457 | + ), |
| 458 | + true, |
| 459 | + ), |
| 460 | + ); |
| 461 | + }); |
| 462 | + |
376 | 463 | it("should return next response if the filtered preconditions is empty", async () => { |
377 | 464 | const selectedRepresentation = new Response("<body>", { |
378 | 465 | headers: { etag: "<etag>" }, |
|
0 commit comments