| | 545 | /** |
| | 546 | * A cache entry with an ETag is checked with If-None-Match only. |
| | 547 | * <p> |
| | 548 | * Non-regression test: If-Modified-Since was sent too, with the time of the download. Apache serving tiles without |
| | 549 | * a Last-Modified (mod_tile) takes the file to be modified now, so it answered 200 although the ETag matched, and |
| | 550 | * the host was then wrongly switched to HEAD requests. |
| | 551 | * @throws IOException exception |
| | 552 | */ |
| | 553 | @Test |
| | 554 | void testCheckUsingETagOnly() throws IOException { |
| | 555 | final String eTag = "\"tile-etag\""; |
| | 556 | ICacheAccess<String, CacheEntry> cache = getCache(); |
| | 557 | long expires = TimeUnit.DAYS.toMillis(1); |
| | 558 | long testStart = System.currentTimeMillis(); |
| | 559 | cache.put("etag-only", |
| | 560 | new CacheEntry("cached dummy".getBytes(StandardCharsets.UTF_8)), |
| | 561 | createEntryAttributes(-1 * expires, 200, testStart, eTag) |
| | 562 | ); |
| | 563 | // like Apache with mod_tile: any If-Modified-Since wins over a matching If-None-Match |
| | 564 | tileServer.getWireMock().register(get(urlEqualTo("/etag-only")) |
| | 565 | .atPriority(1) |
| | 566 | .withHeader("If-Modified-Since", matching(".*")) |
| | 567 | .willReturn(aResponse().withHeader("ETag", eTag).withBody("mock entry"))); |
| | 568 | tileServer.getWireMock().register(get(urlEqualTo("/etag-only")) |
| | 569 | .atPriority(2) |
| | 570 | .withHeader("If-None-Match", equalTo(eTag)) |
| | 571 | .willReturn(status(304).withHeader("ETag", eTag) |
| | 572 | .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires)))); |
| | 573 | |
| | 574 | // the host of the other tests may already be switched to HEAD requests (the switch is static), so use another |
| | 575 | TestCachedTileLoaderJob job = new TestCachedTileLoaderJob( |
| | 576 | "http://127.0.0.1:" + tileServer.getHttpPort() + "/etag-only", "etag-only"); |
| | 577 | Listener listener = submitJob(job, false); |
| | 578 | tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/etag-only")) |
| | 579 | .withHeader("If-None-Match", equalTo(eTag)) |
| | 580 | .withoutHeader("If-Modified-Since")); |
| | 581 | assertArrayEquals("cached dummy".getBytes(StandardCharsets.UTF_8), listener.data); |
| | 582 | } |
| | 583 | |
| | 584 | /** |
| | 585 | * A cache entry without an ETag is still checked with If-Modified-Since. |
| | 586 | * @throws IOException exception |
| | 587 | */ |
| | 588 | @Test |
| | 589 | void testCheckUsingIfModifiedSinceWithoutETag() throws IOException { |
| | 590 | ICacheAccess<String, CacheEntry> cache = getCache(); |
| | 591 | long expires = TimeUnit.DAYS.toMillis(1); |
| | 592 | long testStart = System.currentTimeMillis(); |
| | 593 | cache.put("no-etag", |
| | 594 | new CacheEntry("cached dummy".getBytes(StandardCharsets.UTF_8)), |
| | 595 | createEntryAttributes(-1 * expires, 200, testStart, null) |
| | 596 | ); |
| | 597 | tileServer.getWireMock().register(get(urlEqualTo("/no-etag")) |
| | 598 | .withHeader("If-Modified-Since", matching(".*")) |
| | 599 | .willReturn(status(304).withHeader("Expires", TestUtils.getHTTPDate(testStart + expires)))); |
| | 600 | |
| | 601 | TestCachedTileLoaderJob job = new TestCachedTileLoaderJob( |
| | 602 | "http://127.0.0.1:" + tileServer.getHttpPort() + "/no-etag", "no-etag"); |
| | 603 | Listener listener = submitJob(job, false); |
| | 604 | tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/no-etag")) |
| | 605 | .withHeader("If-Modified-Since", matching(".*")) |
| | 606 | .withoutHeader("If-None-Match")); |
| | 607 | assertArrayEquals("cached dummy".getBytes(StandardCharsets.UTF_8), listener.data); |
| | 608 | } |
| | 609 | |