From 258e61dd80f50c8b65eddd8a4cae0a216cab3953 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Tue, 6 Oct 2026 00:16:35 +0100 Subject: [PATCH] Test the stated span with a key that covers now The test that checks a recent minute is served from a span the creator stated read the clock, whilst the stand in end point it used answers for no moment after 14 September 2026. From 21 September 2026 the end point therefore answered with a key whose span did not hold the minutes asked about, each one was fetched, and the test failed with three requests where it expects one. The creator in the test now states one key for the day either side of now, as the .NET test does, so the test no longer depends on the dates in the fixture schedule and keeps checking after the last key in the fixture has started. --- .../swancommunity/owid/DatedKeyFetchTest.java | 19 ++++++++++++------- .../com/swancommunity/owid/KeyEndPoint.java | 19 ++++++++++++++----- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/test/java/com/swancommunity/owid/DatedKeyFetchTest.java b/src/test/java/com/swancommunity/owid/DatedKeyFetchTest.java index 7305ac3..bdef059 100644 --- a/src/test/java/com/swancommunity/owid/DatedKeyFetchTest.java +++ b/src/test/java/com/swancommunity/owid/DatedKeyFetchTest.java @@ -871,13 +871,18 @@ void aKeyAnsweredWithItsSpanIsHeldForTheWholeSpan() @Test void aRecentMinuteIsServedWhereTheCreatorStatedTheSpan() throws IOException, OwidException { - KeyEndPoint endPoint = endPoint(KeyEndPoint.Answer.SCHEDULE); - Instant now = Instant.now(); - DatedPublicKey current = KeyFixtures.schedule().keyInForce(now); - assumeTrue(current != null - && KeyFixtures.schedule().nextStartAfter(current) != null, - "the fixture schedule has no key after the one in force now"); - long started = Io.minutesSinceBase(now); + long started = Io.minutesSinceBase(Instant.now()); + long day = 24 * 60; + // One key covering the day either side of now, so the span the + // creator states holds every minute asked about whenever the test + // runs. + KeyEndPoint endPoint = KeyEndPoint.start(KeyEndPoint.Answer.GIVEN, + Endpoints.publicKeyAnswer( + KeyFixtures.scheduledKeys().get(0).pem(), + Io.baseDate().plus(Duration.ofMinutes(started - day)), + Io.baseDate().plus(Duration.ofMinutes(started + day)), + null)); + this.started.add(endPoint); pemAt(urlFor(endPoint, started - 1), KeyFixtures.IDENTIFIER_DOMAIN); pemAt(urlFor(endPoint, started), KeyFixtures.IDENTIFIER_DOMAIN); pemAt(urlFor(endPoint, started - 10), KeyFixtures.IDENTIFIER_DOMAIN); diff --git a/src/test/java/com/swancommunity/owid/KeyEndPoint.java b/src/test/java/com/swancommunity/owid/KeyEndPoint.java index 7722d64..2c20553 100644 --- a/src/test/java/com/swancommunity/owid/KeyEndPoint.java +++ b/src/test/java/com/swancommunity/owid/KeyEndPoint.java @@ -81,7 +81,13 @@ enum Answer { * A redirect to somewhere else, which a client must not follow. * Started with {@link #start(Answer, String)} naming where. */ - REDIRECT + REDIRECT, + + /** + * The body given, whatever the request asks for. Started with + * {@link #start(Answer, String)} naming the body. + */ + GIVEN } private final HttpServer server; @@ -107,9 +113,10 @@ static KeyEndPoint start(final Answer answer) /** * Starts an end point serving what the answer says, redirecting to - * the URL given where the answer is {@link Answer#REDIRECT}. + * the URL given where the answer is {@link Answer#REDIRECT} and + * answering with the body given where it is {@link Answer#GIVEN}. */ - static KeyEndPoint start(final Answer answer, final String redirectTo) + static KeyEndPoint start(final Answer answer, final String given) throws IOException, OwidException { final PublicKeySchedule schedule = KeyFixtures.schedule(); HttpServer server = HttpServer.create(new InetSocketAddress( @@ -124,14 +131,16 @@ public void handle(HttpExchange exchange) throws IOException { String format = parameter(query, "format"); endPoint.dates.add(date); if (answer == Answer.REDIRECT) { - exchange.getResponseHeaders().set("Location", redirectTo); + exchange.getResponseHeaders().set("Location", given); exchange.sendResponseHeaders(302, -1); exchange.close(); return; } Endpoints.Response response; try { - response = body(schedule, answer, date, format); + response = answer == Answer.GIVEN + ? new Endpoints.Response(200, given) + : body(schedule, answer, date, format); } catch (OwidException fault) { exchange.sendResponseHeaders(500, -1); exchange.close();