Skip to content

Commit

Permalink
fix(artifacts): Make HttpCredentials fetchUrl throws exception on non…
Browse files Browse the repository at this point in the history
…-successful response (#3946) (#3953)
  • Loading branch information
spinnakerbot authored and ezimanyi committed Aug 16, 2019
1 parent d0d32ce commit 411afdd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ protected ResponseBody fetchUrl(HttpUrl url) throws IOException {
Request request = new Request.Builder().headers(headers).url(url).build();

Response downloadResponse = okHttpClient.newCall(request).execute();
if (!downloadResponse.isSuccessful()) {
throw new IOException(
String.format("Received %d status code from %s", downloadResponse.code(), url.host()));
}
return downloadResponse.body();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.MappingBuilder;
Expand Down Expand Up @@ -73,6 +74,24 @@ void downloadWithNoAuth(@WiremockResolver.Wiremock WireMockServer server) throws
runTestCase(server, account, m -> m.withHeader("Authorization", absent()));
}

@Test
void throwExceptionOnNonSuccessfulResponse(@WiremockResolver.Wiremock WireMockServer server) {
HttpArtifactAccount account = new HttpArtifactAccount();
HttpArtifactCredentials credentials = new HttpArtifactCredentials(account, okHttpClient);
Artifact artifact =
Artifact.builder().reference(server.baseUrl() + URL).type("http/file").build();
account.setName("my-http-account");
server.stubFor(any(urlPathEqualTo(URL)).willReturn(aResponse().withStatus(404)));

Throwable thrown = catchThrowable(() -> credentials.download(artifact));

assertThat(thrown)
.isInstanceOf(IOException.class)
.hasMessageContaining("404")
.hasMessageContaining(server.baseUrl());
assertThat(server.findUnmatchedRequests().getRequests()).isEmpty();
}

private void runTestCase(
WireMockServer server,
HttpArtifactAccount account,
Expand Down

0 comments on commit 411afdd

Please sign in to comment.