-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also covered by tests.
- Loading branch information
Showing
3 changed files
with
101 additions
and
16 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/main/groovy/io/codearte/gradle/nexus/infra/NexusHttpResponseException.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.codearte.gradle.nexus.infra | ||
|
||
import groovy.transform.CompileStatic | ||
|
||
/** | ||
* Custom exception to propagate server errors. | ||
* | ||
* Created as groovyx.net.http.HttpResponseException contains in a message only a reason phrase (e.g. Server Error) without response body | ||
* which in many cases is crucial to determine the resons why error was returned. | ||
* | ||
* It may be made redundant once migrated to other HTTP library. | ||
*/ | ||
@CompileStatic | ||
class NexusHttpResponseException extends NexusStagingException { | ||
|
||
final int statusCode | ||
|
||
NexusHttpResponseException(int statusCode, String message, Throwable cause) { | ||
super(message, cause) | ||
this.statusCode = statusCode | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/test/groovy/io/codearte/gradle/nexus/functional/MockedResponseErrorHandlingSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.codearte.gradle.nexus.functional | ||
|
||
import com.github.tomakehurst.wiremock.junit.WireMockRule | ||
import groovy.transform.NotYetImplemented | ||
import groovyx.net.http.HttpResponseException | ||
import groovyx.net.http.RESTClient | ||
import io.codearte.gradle.nexus.infra.NexusHttpResponseException | ||
import io.codearte.gradle.nexus.infra.SimplifiedHttpJsonRestClient | ||
import io.codearte.gradle.nexus.logic.RepositoryCloser | ||
import org.junit.Rule | ||
import spock.lang.Specification | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse | ||
import static com.github.tomakehurst.wiremock.client.WireMock.containing | ||
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo | ||
import static com.github.tomakehurst.wiremock.client.WireMock.post | ||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo | ||
|
||
class MockedResponseErrorHandlingSpec extends Specification { | ||
|
||
private static final String TEST_MOCKED_USERNAME = '' | ||
private static final String TEST_MOCKED_PASSWORD = '' | ||
private static final String TEST_MOCKED_STAGING_PROFILE_ID = "93c08fdebde1ff" | ||
private static final String TEST_MOCKED_NOT_EXISTING_REPOSITORY_ID = "xxx" | ||
private static final String TEST_MOCKED_SERVER_ERROR_JSON_RESPONSE = """ | ||
{ | ||
"errors": [ | ||
{ | ||
"id": "*", | ||
"msg": "Unhandled: Missing staging repository: $TEST_MOCKED_NOT_EXISTING_REPOSITORY_ID" | ||
} | ||
] | ||
} | ||
""".stripIndent() | ||
|
||
@Rule | ||
public WireMockRule wireMockRule = new WireMockRule(8089) | ||
|
||
//Using private Options as server is not started yet | ||
private String mockedUrl = "http://localhost:${wireMockRule.options.portNumber()}/" | ||
|
||
def "should present response body on 500 server error"() { | ||
given: | ||
SimplifiedHttpJsonRestClient client = new SimplifiedHttpJsonRestClient(new RESTClient(), TEST_MOCKED_USERNAME, TEST_MOCKED_PASSWORD) | ||
RepositoryCloser closer = new RepositoryCloser(client, mockedUrl) | ||
and: | ||
stubFor(post(urlEqualTo("/staging/profiles/$TEST_MOCKED_STAGING_PROFILE_ID/finish")) | ||
.withHeader("Content-Type", equalTo("application/json")) | ||
.withHeader("Accept", containing("application/json")) | ||
.willReturn(aResponse() | ||
.withStatus(500) | ||
.withBody(TEST_MOCKED_SERVER_ERROR_JSON_RESPONSE) | ||
.withHeader("Content-Type", "application/json"))) | ||
when: | ||
closer.closeRepositoryWithIdAndStagingProfileId(TEST_MOCKED_NOT_EXISTING_REPOSITORY_ID, TEST_MOCKED_STAGING_PROFILE_ID) | ||
then: | ||
NexusHttpResponseException e = thrown() | ||
e.statusCode == 500 | ||
e.message.contains("Missing staging repository: $TEST_MOCKED_NOT_EXISTING_REPOSITORY_ID") | ||
e.cause instanceof HttpResponseException | ||
} | ||
|
||
@NotYetImplemented | ||
def "should present response body on 400 or 500 errors with plain text response"() {} | ||
} |