forked from Codearte/gradle-nexus-staging-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MockedResponseErrorHandlingSpec.groovy
66 lines (58 loc) · 3.16 KB
/
MockedResponseErrorHandlingSpec.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package io.codearte.gradle.nexus.functional
import com.github.tomakehurst.wiremock.junit.WireMockRule
import groovy.transform.NotYetImplemented
import io.codearte.gradle.nexus.infra.NexusHttpResponseException
import io.codearte.gradle.nexus.infra.SimplifiedHttpJsonRestClient
import io.codearte.gradle.nexus.logic.RepositoryCloser
import okhttp3.OkHttpClient
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 = "5027d084a01a3a"
private static final String TEST_MOCKED_NOT_EXISTING_REPOSITORY_ID = "xxx"
private static final String TEST_MOCKED_REPOSITORY_DESCRIPTION = "Mocked repository description"
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(MockedFunctionalSpec.WIREMOCK_RANDOM_PORT)
def "should present response body on 500 server error"() {
given:
SimplifiedHttpJsonRestClient client = new SimplifiedHttpJsonRestClient(new OkHttpClient(), TEST_MOCKED_USERNAME, TEST_MOCKED_PASSWORD)
RepositoryCloser closer = new RepositoryCloser(client, getMockedUrl(), TEST_MOCKED_REPOSITORY_DESCRIPTION)
and:
stubFor(post(urlEqualTo("/staging/bulk/close"))
.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")
}
private String getMockedUrl() {
return "http://localhost:${wireMockRule.port()}/"
}
@NotYetImplemented
def "should present response body on 400 or 500 errors with plain text response"() {}
}