Skip to content

Commit

Permalink
Renamed Apache 4 client classes to avoid JaCoCo blowing up due to a l…
Browse files Browse the repository at this point in the history
…ack of respect for module separation
  • Loading branch information
jshiell authored and s4nchez committed Jun 22, 2020
1 parent 2980d46 commit e15d4fa
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.http4k.core.Status.Companion.SERVICE_UNAVAILABLE
import java.net.SocketTimeoutException
import java.net.URI

object ApacheAsyncClient {
object Apache4AsyncClient {
operator fun invoke(
client: CloseableHttpAsyncClient = defaultApacheAsyncHttpClient(),
responseBodyMode: BodyMode = Memory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import org.apache.http.impl.nio.reactor.IOReactorConfig
import org.http4k.core.BodyMode
import org.http4k.server.SunHttp

class ApacheAsyncClientStreamingTest : AsyncHttpClientContract({ SunHttp(it) },
ApacheAsyncClient(responseBodyMode = BodyMode.Stream),
ApacheAsyncClient(HttpAsyncClients.custom()
class Apache4AsyncClientStreamingTest : AsyncHttpClientContract({ SunHttp(it) },
Apache4AsyncClient(responseBodyMode = BodyMode.Stream),
Apache4AsyncClient(HttpAsyncClients.custom()
.setDefaultIOReactorConfig(IOReactorConfig.custom()
.setConnectTimeout(100)
.setSoTimeout(100)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ import java.util.concurrent.CompletableFuture
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Future

class ApacheAsyncClientTest : AsyncHttpClientContract({ SunHttp(it) }, ApacheAsyncClient(),
ApacheAsyncClient(HttpAsyncClients.custom()
class Apache4AsyncClientTest : AsyncHttpClientContract({ SunHttp(it) }, Apache4AsyncClient(),
Apache4AsyncClient(HttpAsyncClients.custom()
.setDefaultIOReactorConfig(IOReactorConfig.custom()
.setSoTimeout(100)
.build()).build().apply { start() })) {
@Test
fun `connect timeout is handled`() {

val latch = CountDownLatch(1)
ApacheAsyncClient(object : CloseableHttpAsyncClient() {
Apache4AsyncClient(object : CloseableHttpAsyncClient() {
override fun isRunning(): Boolean = false
override fun start() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.impl.client.HttpClients
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager
import org.apache.http.ssl.SSLContextBuilder
import org.http4k.client.PreCannedApacheHttpClients.defaultApacheHttpClient
import org.http4k.client.PreCannedApache4HttpClients.defaultApacheHttpClient
import org.http4k.core.BodyMode
import org.http4k.core.BodyMode.Memory
import org.http4k.core.BodyMode.Stream
Expand All @@ -46,7 +46,7 @@ import java.net.SocketTimeoutException
import java.net.URI
import java.net.UnknownHostException

object ApacheClient {
object Apache4Client {
operator fun invoke(
client: CloseableHttpClient = defaultApacheHttpClient(),
responseBodyMode: BodyMode = Memory,
Expand Down Expand Up @@ -75,7 +75,7 @@ object ApacheClient {
OPTIONS -> HttpOptions(uri)
TRACE -> HttpTrace(uri)
DELETE -> HttpDelete(uri)
else -> ApacheRequest(requestBodyMode, request)
else -> Apache4Request(requestBodyMode, request)
}
request.headers.filter { !it.first.equals("content-length", true) }.map { apacheRequest.addHeader(it.first, it.second) }
return apacheRequest
Expand All @@ -90,7 +90,7 @@ object ApacheClient {
}
}

private class ApacheRequest(requestBodyMode: BodyMode, private val request: Request) : HttpEntityEnclosingRequestBase() {
private class Apache4Request(requestBodyMode: BodyMode, private val request: Request) : HttpEntityEnclosingRequestBase() {
init {
uri = URI(request.uri.toString())
entity = when (requestBodyMode) {
Expand All @@ -102,7 +102,7 @@ private class ApacheRequest(requestBodyMode: BodyMode, private val request: Requ
override fun getMethod() = request.method.name
}

object PreCannedApacheHttpClients {
object PreCannedApache4HttpClients {

/**
* Standard non-redirecting, no Cookies HTTP client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import org.http4k.server.ServerConfig
import org.http4k.server.SunHttp
import org.http4k.streaming.StreamingContract

class ApacheClientStreamingContractTest : StreamingContract() {
class Apache4ClientStreamingContractTest : StreamingContract() {
override fun serverConfig(): ServerConfig = SunHttp(0)

override fun createClient(): HttpHandler = ApacheClient(requestBodyMode = BodyMode.Stream, responseBodyMode = BodyMode.Stream)
override fun createClient(): HttpHandler = Apache4Client(requestBodyMode = BodyMode.Stream, responseBodyMode = BodyMode.Stream)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import org.apache.http.impl.client.HttpClients
import org.http4k.core.BodyMode.Stream
import org.http4k.server.Jetty

class ApacheClientStreamingTest : HttpClientContract({ Jetty(it) },
ApacheClient(requestBodyMode = Stream, responseBodyMode = Stream),
ApacheClient(HttpClients.custom()
class Apache4ClientStreamingTest : HttpClientContract({ Jetty(it) },
Apache4Client(requestBodyMode = Stream, responseBodyMode = Stream),
Apache4Client(HttpClients.custom()
.setDefaultSocketConfig(
SocketConfig.custom()
.setSoTimeout(100)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ import org.http4k.hamkrest.hasStatus
import org.http4k.server.Jetty
import org.junit.jupiter.api.Test

class ApacheClientTest : HttpClientContract({ Jetty(it) }, ApacheClient(),
ApacheClient(HttpClients.custom()
class Apache4ClientTest : HttpClientContract({ Jetty(it) }, Apache4Client(),
Apache4Client(HttpClients.custom()
.setDefaultSocketConfig(
SocketConfig.custom()
.setSoTimeout(100)
.build()
).build()
, responseBodyMode = Stream)) {

@Test
fun `connect timeout is handled`() {
assertThat(ApacheClient(object : CloseableHttpClient() {
assertThat(Apache4Client(object : CloseableHttpClient() {
override fun getParams() = TODO("not implemented")

override fun getConnectionManager() = TODO("not implemented")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import org.apache.http.HttpResponse as ApacheResponse
/**
* Exposed to allow for insertion into a customised Apache WebServer instance
*/
class Http4kRequestHandler(handler: HttpHandler) : HttpRequestHandler {
class Http4kApache4RequestHandler(handler: HttpHandler) : HttpRequestHandler {

private val safeHandler = ServerFilters.CatchAll().then(handler)

Expand Down Expand Up @@ -62,7 +62,7 @@ class Http4kRequestHandler(handler: HttpHandler) : HttpRequestHandler {
private fun Array<Header>.toHttp4kHeaders(): Headers = listOf(*map { it.name to it.value }.toTypedArray())
}

data class ApacheServer(val port: Int = 8000, val address: InetAddress?) : ServerConfig {
data class Apache4Server(val port: Int = 8000, val address: InetAddress?) : ServerConfig {
constructor(port: Int = 8000) : this(port, null)

override fun toServer(httpHandler: HttpHandler): Http4kServer = object : Http4kServer {
Expand All @@ -77,7 +77,7 @@ data class ApacheServer(val port: Int = 8000, val address: InetAddress?) : Serve
.setSoReuseAddress(true)
.setBacklogSize(1000)
.build())
.registerHandler("*", Http4kRequestHandler(httpHandler))
.registerHandler("*", Http4kApache4RequestHandler(httpHandler))

if (address != null)
bootstrap.setLocalAddress(address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package org.http4k.server
import org.http4k.client.ApacheClient
import org.http4k.core.Method

class ApacheServerTest : ServerContract(::ApacheServer, ApacheClient(),
Method.values().filter { it != Method.PURGE }.toTypedArray())
class Apache4ServerTest : ServerContract(::Apache4Server, ApacheClient(),
Method.values().filter { it != Method.PURGE }.toTypedArray())
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package org.http4k.stream
import org.http4k.client.ApacheClient
import org.http4k.core.BodyMode.Stream
import org.http4k.core.HttpHandler
import org.http4k.server.ApacheServer
import org.http4k.server.Apache4Server
import org.http4k.server.ServerConfig
import org.http4k.streaming.StreamingContract

class ApacheServerStreamingTest : StreamingContract() {
override fun serverConfig(): ServerConfig = ApacheServer(0)
class Apache4ServerStreamingTest : StreamingContract() {
override fun serverConfig(): ServerConfig = Apache4Server(0)

override fun createClient(): HttpHandler =
ApacheClient(requestBodyMode = Stream, responseBodyMode = Stream)
}
}
4 changes: 2 additions & 2 deletions src/docs/guide/modules/clients/example_http.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package guide.modules.clients

import org.apache.hc.client5.http.config.RequestConfig
import org.apache.hc.client5.http.cookie.StandardCookieSpec
import org.apache.hc.client5.http.impl.classic.HttpClients
import org.apache.http.client.config.CookieSpecs
import org.http4k.client.ApacheAsyncClient
import org.http4k.client.ApacheClient
import org.http4k.core.BodyMode
Expand Down Expand Up @@ -44,7 +44,7 @@ fun main() {
val customClient = ApacheClient(
client = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom()
.setRedirectsEnabled(false)
.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
.setCookieSpec(StandardCookieSpec.IGNORE)
.build())
.build()
)
Expand Down

0 comments on commit e15d4fa

Please sign in to comment.