Skip to content

Commit

Permalink
Fix problem with empty 'Accept' header in consecutive call
Browse files Browse the repository at this point in the history
  • Loading branch information
szpak committed Mar 1, 2015
1 parent dc294c6 commit aa842f9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,35 @@ class SimplifiedHttpJsonRestClient {
private final RESTClient restClient
private final String username
private final String password
private final Map params

SimplifiedHttpJsonRestClient(RESTClient restClient, String username, String password) {
this.restClient = restClient
this.username = username
this.password = password
params = [:]
params.contentType = ContentType.JSON
// params.requestContentType = ContentType.JSON //Does not set Content-Type header as required by WireMock
restClient.headers["Content-Type"] = "application/json"
}

Map get(String uri) {
setUriAndAuthentication(uri)
Map params = createAndInitializeCallParametersMap()
HttpResponseDecorator response = (HttpResponseDecorator)restClient.get(params)
log.debug("GET response data: ${response.data}")
return (Map)response.data
}

private Map createAndInitializeCallParametersMap() { //New for every call - it is cleared up after call by RESTClient
return [contentType: ContentType.JSON]
}

private void setUriAndAuthentication(String uri) {
restClient.uri = uri
restClient.auth.basic(username, password) //has to be after URI is set
}

void post(String uri, Map content) {
setUriAndAuthentication(uri)
Map params = createAndInitializeCallParametersMap()
params.body = content
log.debug("POST request content: $content")
//TODO: Add better error handling (e.g. display error message received from server, not only 500 + not fail on 404 in 'text/html')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class MockedFunctionalSpec extends BaseNexusStagingFunctionalSpec implements Fet
private void stubGetStagingProfilesWithJson(String responseAsJson) {
stubFor(get(urlEqualTo("/staging/profiles"))
.withHeader("Content-Type", containing("application/json"))
.withHeader("Accept", containing("application/json"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
Expand All @@ -164,6 +165,7 @@ class MockedFunctionalSpec extends BaseNexusStagingFunctionalSpec implements Fet
private void stubGetOneRepositoryWithProfileIdAndContent(String stagingProfileId, Map response) {
stubFor(get(urlEqualTo("/staging/profile_repositories/$stagingProfileId"))
.withHeader("Content-Type", containing("application/json"))
.withHeader("Accept", containing("application/json"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
Expand All @@ -173,6 +175,7 @@ class MockedFunctionalSpec extends BaseNexusStagingFunctionalSpec implements Fet
private void stubSuccessfulCloseRepositoryWithProfileId(String stagingProfileId) {
stubFor(post(urlEqualTo("/staging/profiles/$stagingProfileId/finish"))
.withHeader("Content-Type", equalTo("application/json"))
.withHeader("Accept", containing("application/json"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")));
Expand All @@ -181,6 +184,7 @@ class MockedFunctionalSpec extends BaseNexusStagingFunctionalSpec implements Fet
private void stubSuccessfulPromoteRepositoryWithProfileId(String stagingProfileId) {
stubFor(post(urlEqualTo("/staging/profiles/$stagingProfileId/promote"))
.withHeader("Content-Type", equalTo("application/json"))
.withHeader("Accept", containing("application/json"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")));
Expand All @@ -190,6 +194,7 @@ class MockedFunctionalSpec extends BaseNexusStagingFunctionalSpec implements Fet
stubFor(get(urlEqualTo("/staging/profile_repositories/$stagingProfileId")).inScenario("State")
.whenScenarioStateIs(Scenario.STARTED)
.withHeader("Content-Type", containing("application/json"))
.withHeader("Accept", containing("application/json"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
Expand All @@ -200,6 +205,7 @@ class MockedFunctionalSpec extends BaseNexusStagingFunctionalSpec implements Fet
stubFor(get(urlEqualTo("/staging/profile_repositories/$stagingProfileId")).inScenario("State")
.whenScenarioStateIs("CLOSED")
.withHeader("Content-Type", containing("application/json"))
.withHeader("Accept", containing("application/json"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
Expand Down

0 comments on commit aa842f9

Please sign in to comment.