Skip to content

Commit 3dc709c

Browse files
authored
fix: correctly handle 307 response with location header (#23)
1 parent 25e7671 commit 3dc709c

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

src/main/kotlin/io/github/guidewire/oss/DataSender.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private fun postTestRun(endpoint: String, fernUrl: String, payload: String): Htt
5454

5555
var response = client.send(request, HttpResponse.BodyHandlers.ofString())
5656
if (response.statusCode() == 307) {
57-
val locationHeader = response.headers().firstValue("location")
57+
val locationHeader = response.headers().firstValue("location").orElseThrow { RuntimeException("Location header not found in 307 response") }
5858
response = postTestRun(fernUrl + locationHeader, fernUrl, payload)
5959
}
6060
return response

src/test/kotlin/DataSenderTest.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,44 @@ class DataSenderTest {
6666
)
6767
}
6868

69+
@Test
70+
fun `sendTestRun should follow redirects`() {
71+
// Setup mock server for initial redirect
72+
stubFor(
73+
post(urlEqualTo("/api/testrun/"))
74+
.willReturn(
75+
aResponse()
76+
.withStatus(307)
77+
.withHeader("Location", "/api/testrun/redirect")
78+
)
79+
)
80+
81+
// Setup mock server for the redirected endpoint
82+
stubFor(
83+
post(urlEqualTo("/api/testrun/redirect"))
84+
.withHeader("Content-Type", equalTo("application/json"))
85+
.willReturn(
86+
aResponse()
87+
.withStatus(200)
88+
.withBody("{\"status\":\"success\"}")
89+
)
90+
)
91+
92+
// Test API call
93+
val result = sendTestRun(testRun, "http://localhost:${wireMockServer.port()}", true)
94+
95+
// Verify
96+
assertTrue(result.isSuccess)
97+
verify(
98+
postRequestedFor(urlEqualTo("/api/testrun/"))
99+
.withHeader("Content-Type", equalTo("application/json"))
100+
)
101+
verify(
102+
postRequestedFor(urlEqualTo("/api/testrun/redirect"))
103+
.withHeader("Content-Type", equalTo("application/json"))
104+
)
105+
}
106+
69107
@Test
70108
fun `sendTestRun should handle server errors`() {
71109
// Setup mock server to return error

0 commit comments

Comments
 (0)