Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display HTTP error response to the user #92

Merged
merged 5 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add test that asserts failure with message
  • Loading branch information
hiddewie committed Apr 14, 2021
commit 6d35283422c0fafad50ec231c94170883d2c0d3a
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.github.tomakehurst.wiremock.client.WireMock.get
import com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor
import com.github.tomakehurst.wiremock.client.WireMock.matching
import com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath
import com.github.tomakehurst.wiremock.client.WireMock.notFound
import com.github.tomakehurst.wiremock.client.WireMock.post
import com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor
import com.github.tomakehurst.wiremock.client.WireMock.put
Expand Down Expand Up @@ -231,6 +232,47 @@ class NexusPublishPluginTests {
assertUploadedToStagingRepo("/org/example/sample/0.0.1/sample-0.0.1.jar")
}

@Test
fun `displays the error response to the user when a request fails`() {
projectDir.resolve("settings.gradle").write("""
rootProject.name = 'sample'
""")
projectDir.resolve("build.gradle").write("""
plugins {
id('java-library')
id('maven-publish')
id('io.github.gradle-nexus.publish-plugin')
}
group = 'org.example'
version = '0.0.1'
publishing {
publications {
mavenJava(MavenPublication) {
from(components.java)
}
}
}
nexusPublishing {
repositories {
myNexus {
nexusUrl = uri('${server.baseUrl()}')
snapshotRepositoryUrl = uri('${server.baseUrl()}/snapshots/')
allowInsecureProtocol = true
username = 'username'
password = 'password'
}
}
}
""")

stubMissingStagingProfileRequest("/staging/profiles")

val result = runAndFail("publishToMyNexus")

assertFailure(result, ":initializeMyNexusStagingRepository")
assertThat(result.output).contains("""{"failure":"message"}""")
}

@Test
fun `publishes to two Nexus repositories`(@MethodScopeWiremockResolver.MethodScopedWiremockServer @Wiremock otherServer: WireMockServer) {
projectDir.resolve("settings.gradle").write("""
Expand Down Expand Up @@ -810,9 +852,11 @@ class NexusPublishPluginTests {
""")
}

private fun run(vararg arguments: String): BuildResult {
return gradleRunner(*arguments).build()
}
private fun run(vararg arguments: String): BuildResult =
gradleRunner(*arguments).build()

private fun runAndFail(vararg arguments: String): BuildResult =
gradleRunner(*arguments).buildAndFail()

private fun gradleRunner(vararg arguments: String): GradleRunner {
return gradleRunner
Expand All @@ -833,6 +877,12 @@ class NexusPublishPluginTests {
.willReturn(aResponse().withBody(gson.toJson(mapOf("data" to listOf(*stagingProfiles))))))
}

private fun stubMissingStagingProfileRequest(url: String, wireMockServer: WireMockServer = server) {
wireMockServer.stubFor(get(urlEqualTo(url))
.withHeader("User-Agent", matching("gradle-nexus-publish-plugin/.*"))
.willReturn(notFound().withBody(gson.toJson(mapOf("failure" to "message")))))
}

private fun stubCreateStagingRepoRequest(url: String, stagedRepositoryId: String, wireMockServer: WireMockServer = server) {
wireMockServer.stubFor(post(urlEqualTo(url))
.willReturn(aResponse().withBody(gson.toJson(mapOf("data" to mapOf("stagedRepositoryId" to stagedRepositoryId))))))
Expand Down Expand Up @@ -903,6 +953,10 @@ class NexusPublishPluginTests {
assertOutcome(result, taskPath, SUCCESS)
}

private fun assertFailure(result: BuildResult, taskPath: String) {
assertOutcome(result, taskPath, FAILED)
}

private fun assertSkipped(result: BuildResult, taskPath: String) {
assertOutcome(result, taskPath, SKIPPED)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ open class NexusClient(private val baseUrl: URI, username: String?, password: St
private fun failure(action: String, response: Response<*>): RuntimeException {
var message = "Failed to $action, server at $baseUrl responded with status code ${response.code()}"
val errorBody = response.errorBody()
if (errorBody != null && errorBody.contentLength() > 0) {
if (errorBody != null) {
message += try {
", body: ${errorBody.string()}"
} catch (exception: IOException) {
", body: (error during read body of error response, message: ${exception.message})"
", body: <error while reading body of error response, message: ${exception.message}>"
}
}
return RuntimeException(message)
Expand Down