Skip to content

Commit

Permalink
Fix base URL bug in HTTP client
Browse files Browse the repository at this point in the history
  • Loading branch information
jaguililla committed May 2, 2023
1 parent cde670a commit a5ebce9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,15 @@ open class JettyClientAdapter : HttpClientPort {
val settings = adapterHttpClient.settings
val contentType = request.contentType ?: settings.contentType
val authorization = request.authorization ?: settings.authorization
val baseUrl = settings.baseUrl

if (settings.useCookies) {
val uri = (adapterHttpClient.settings.baseUrl ?: request.url()).toURI()
val uri = (baseUrl ?: request.url()).toURI()
addCookies(uri, adapterJettyClient.cookieStore, request.cookies)
}

val jettyRequest = adapterJettyClient
.newRequest(URI(settings.baseUrl.toString() + request.path))
.newRequest(URI((baseUrl?.toString() ?: "") + request.path))
.method(HttpMethod.valueOf(request.method.toString()))
.headers {
it.remove("accept-encoding") // Don't send encoding by default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ abstract class ClientTest(
assertEquals("qp=qpValue", getResponse.headers["query-parameters"]?.value)
assertEquals("qp=qpValue", response.headers["query-parameters"]?.value)
checkResponse(response, mapOf("body" to "payload"))

val base = client.settings.baseUrl?.toString()
HttpClient(clientAdapter(), client.settings.copy(baseUrl = null)).request {
val secondResponse = get("$base/queryParameters?qp=qpValue")
assertEquals("qp=qpValue", secondResponse.headers["query-parameters"]?.value)
}
}

@Test fun `HTTP methods without body work ok`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ fun main() {
internal class HelloWorldTest {

private val client: HttpClient by lazy {
HttpClient(JettyClientAdapter(), HttpClientSettings(URL("http://localhost:${server.runtimePort}")))
val settings = HttpClientSettings(URL("http://localhost:${server.runtimePort}"))
HttpClient(JettyClientAdapter(), settings)
}

@BeforeAll fun initialize() {
Expand Down

0 comments on commit a5ebce9

Please sign in to comment.