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
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@ private String getResponseBody(CloseableHttpResponse httpResponse) throws
case HttpStatus.SC_SERVICE_UNAVAILABLE:
throw new ServiceUnavailableException(errorMessage);
default:
if (httpResponse.getCode() >= 400 && httpResponse.getCode() < 500) {
throw new BadRequestException(errorMessage);
} else if (httpResponse.getCode() >= 500) {
throw new InternalServerErrorException(errorMessage);
}
return responseBody;
}
}
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/se/michaelthelin/spotify/SpotifyHttpManagerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package se.michaelthelin.spotify;

import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import org.apache.hc.core5.http.Header;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import se.michaelthelin.spotify.SpotifyHttpManager.Builder;
import se.michaelthelin.spotify.exceptions.SpotifyWebApiException;

import java.net.URI;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;

@WireMockTest(httpPort = 9090)
class SpotifyHttpManagerTest {

private final SpotifyHttpManager spotifyHttpManager = new SpotifyHttpManager(new Builder());

@ParameterizedTest
@ValueSource(ints = {405, 409, 410, 414, 422, 431, 499})
public void throwsSpotifyWebApiExceptionForAll4xxStatusCodes(int statusCode) {

stubFor(get("/test/foo/")
.willReturn(aResponse()
.withStatus(statusCode)));

Assertions.assertThrows(SpotifyWebApiException.class, () ->
spotifyHttpManager.get(URI.create("http://localhost:9090/test/foo/"), new Header[0]));
}

@ParameterizedTest
@ValueSource(ints = {501, 504, 599})
public void throwsSpotifyWebApiExceptionForAll5xxStatusCodes(int statusCode) {

stubFor(get("/test/foo/")
.willReturn(aResponse()
.withStatus(statusCode)));

Assertions.assertThrows(SpotifyWebApiException.class, () ->
spotifyHttpManager.get(URI.create("http://localhost:9090/test/foo/"), new Header[0]));
}
}