Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/test/java/com/swancommunity/owid/DatedKeyFetchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 14 additions & 5 deletions src/test/java/com/swancommunity/owid/KeyEndPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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();
Expand Down
Loading