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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog
All notable changes to this project will be documented in this file.

## 2.0.12 - unreleased

### Fixed
* Throw exception on HTTP status != 200 instead of silently returning no results (#97)


## 2.0.11 - 2026-05-14

### Dependencies
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/de/stklcode/pubtrans/ura/UraClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2024 Stefan Kalscheuer
* Copyright 2016-2026 Stefan Kalscheuer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -505,7 +505,11 @@ private InputStream request(String url) throws IOException {
reqBuilder.timeout(config.getTimeout());
}

return clientBuilder.build().send(reqBuilder.build(), HttpResponse.BodyHandlers.ofInputStream()).body();
var response = clientBuilder.build().send(reqBuilder.build(), HttpResponse.BodyHandlers.ofInputStream());
if (response.statusCode() != 200) {
throw new IOException("API request failed with status " + response.statusCode());
}
return response.body();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("API request interrupted", e);
Expand Down
31 changes: 13 additions & 18 deletions src/test/java/de/stklcode/pubtrans/ura/UraClientTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2024 Stefan Kalscheuer
* Copyright 2016-2026 Stefan Kalscheuer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,12 +35,13 @@
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.startsWith;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.core.Is.is;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Unit test for the URA Client.
Expand Down Expand Up @@ -73,13 +74,10 @@ void getStopsTest() throws UraClientException {
// Test Exception handling.
mockHttpToError(500);

try {
new UraClient(wireMock.baseUrl()).getStops();
} catch (RuntimeException e) {
assertThat(e, is(instanceOf(IllegalStateException.class)));
assertThat(e.getCause(), is(instanceOf(IOException.class)));
assertThat(e.getCause().getMessage(), startsWith("Server returned HTTP response code: 500 for URL"));
}
var uraClient = new UraClient(wireMock.baseUrl());
var e = assertThrows(UraClientException.class, uraClient::getStops);
assertThat(e.getCause(), is(instanceOf(IOException.class)));
assertThat(e.getCause().getMessage(), is("API request failed with status 500"));
}

@Test
Expand Down Expand Up @@ -212,18 +210,15 @@ void getTripsTest() throws UraClientException {
trips = new UraClient(wireMock.baseUrl()).getTrips(5);
assertThat(trips, hasSize(5));

// Test mockException handling.
// Test Exception handling.
var uraClient = new UraClient(wireMock.baseUrl());
mockHttpToError(502);
try {
new UraClient(wireMock.baseUrl()).getTrips();
} catch (RuntimeException e) {
assertThat(e, is(instanceOf(IllegalStateException.class)));
assertThat(e.getCause(), is(instanceOf(IOException.class)));
assertThat(e.getCause().getMessage(), startsWith("Server returned HTTP response code: 502 for URL"));
}
var exception = assertThrows(UraClientException.class, uraClient::getTrips);
assertThat(exception.getCause(), is(instanceOf(IOException.class)));
assertThat(exception.getCause().getMessage(), is("API request failed with status 502"));

mockHttpToException();
UraClientException exception = assertThrows(
exception = assertThrows(
UraClientException.class,
() -> new UraClient(wireMock.baseUrl()).getTrips(),
"Expected reader to raise an exception"
Expand Down