Skip to content

Commit

Permalink
Merge pull request #448 from mattTea/todo_hunting_369
Browse files Browse the repository at this point in the history
Make http_client Response body property generic - part of #369
  • Loading branch information
jaguililla authored Nov 11, 2021
2 parents 52e55e3 + dea088f commit 2fce52b
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 36 deletions.
34 changes: 18 additions & 16 deletions http_client/src/main/kotlin/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,92 +19,94 @@ class Client(
/**
* Synchronous execution.
*/
fun send(request: Request): Response =
fun send(request: Request): Response<String> =
adapter.send(this, request)

fun get(
path: String,
headers: Map<String, List<String>> = emptyMap(),
body: Any? = null,
contentType: String? = settings.contentType): Response =
contentType: String? = settings.contentType): Response<String> =
send(Request(GET, Path(path), body, headers = headers, contentType = contentType))

fun head(path: String, headers: Map<String, List<String>> = emptyMap()): Response =
fun head(path: String, headers: Map<String, List<String>> = emptyMap()): Response<String> =
send(Request(HEAD, Path(path), null, headers = headers))

fun post(
path: String, body: Any? = null, contentType: String? = settings.contentType): Response =
path: String,
body: Any? = null,
contentType: String? = settings.contentType): Response<String> =
send(Request(POST, Path(path), body, contentType = contentType))

fun put(
path: String,
body: Any? = null,
contentType: String? = settings.contentType): Response =
contentType: String? = settings.contentType): Response<String> =
send(Request(PUT, Path(path), body, contentType = contentType))

fun delete(
path: String,
body: Any? = null,
contentType: String? = settings.contentType): Response =
contentType: String? = settings.contentType): Response<String> =
send(Request(DELETE, Path(path), body, contentType = contentType))

fun trace(
path: String,
body: Any? = null,
contentType: String? = settings.contentType): Response =
contentType: String? = settings.contentType): Response<String> =
send(Request(TRACE, Path(path), body, contentType = contentType))

fun options(
path: String,
body: Any? = null,
contentType: String? = settings.contentType,
headers: Map<String, List<String>> = emptyMap()): Response =
headers: Map<String, List<String>> = emptyMap()): Response<String> =
send(Request(OPTIONS, Path(path), body, headers, contentType = contentType))

fun patch(
path: String,
body: Any? = null,
contentType: String? = settings.contentType): Response =
contentType: String? = settings.contentType): Response<String> =
send(Request(PATCH, Path(path), body, contentType = contentType))

fun get(
path: String,
headers: Map<String, List<String>> = emptyMap(),
body: Any,
format: SerializationFormat): Response =
format: SerializationFormat): Response<String> =
get(path, headers, body, format.contentType)

fun post(path: String, body: Any, format: SerializationFormat): Response =
fun post(path: String, body: Any, format: SerializationFormat): Response<String> =
post(path, body, format.contentType)

fun put(
path: String,
body: Any,
format: SerializationFormat): Response =
format: SerializationFormat): Response<String> =
put(path, body, format.contentType)

fun delete(
path: String,
body: Any,
format: SerializationFormat): Response =
format: SerializationFormat): Response<String> =
delete(path, body, format.contentType)

fun trace(
path: String,
body: Any,
format: SerializationFormat): Response =
format: SerializationFormat): Response<String> =
trace(path, body, format.contentType)

fun options(
path: String,
body: Any,
format: SerializationFormat,
headers: Map<String, List<String>> = emptyMap()): Response =
headers: Map<String, List<String>> = emptyMap()): Response<String> =
options(path, body, format.contentType, headers)

fun patch(
path: String,
body: Any,
format: SerializationFormat): Response =
format: SerializationFormat): Response<String> =
patch(path, body, format.contentType)
}
2 changes: 1 addition & 1 deletion http_client/src/main/kotlin/ClientPort.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.hexagonkt.http.client

interface ClientPort {
fun send(client: Client, request: Request): Response
fun send(client: Client, request: Request): Response<String>
}
4 changes: 2 additions & 2 deletions http_client/src/main/kotlin/Response.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import java.io.InputStream
/**
* HTTP response fetched from a server request.
*/
data class Response(
data class Response <T> (
var status: Int,
var body: String?, // TODO Change by generic T
var body: T?,
val headers: MutableMap<String, List<String>>,
var contentType: String?,
val inputStream: InputStream
Expand Down
2 changes: 1 addition & 1 deletion http_client/src/test/kotlin/ClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ abstract class ClientTest(private val adapter: () -> ClientPort) {
}

private fun checkResponse(
response: Response, parameter: Map<String, String>?, format: SerializationFormat = Json) {
response: Response<String>, parameter: Map<String, String>?, format: SerializationFormat = Json) {

assert(response.status == 200)
assert(response.body?.trim() == parameter?.serialize(format)?.trim() ?: "")
Expand Down
2 changes: 1 addition & 1 deletion http_client_ahc/src/main/kotlin/AhcAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class AhcAdapter : ClientPort {
return keyStore
}

override fun send(client: Client, request: Request): Response {
override fun send(client: Client, request: Request): Response<String> {

val settings: ClientSettings = client.settings
val ahcRequest = createRequest(client, request)
Expand Down
4 changes: 2 additions & 2 deletions http_server/src/test/kotlin/PortHttpServerSamplesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,13 @@ abstract class PortHttpServerSamplesTest(val adapter: ServerPort) {
}

@Test fun filters() {
fun assertResponse(response: ClientResponse, body: String, vararg headers: String) {
fun assertResponse(response: ClientResponse<String>, body: String, vararg headers: String) {
assert(response.status == 200)
(headers.toList() + "b_all" + "a_all").forEach { assert(response.headers.contains(it)) }
assert(response.body == body)
}

fun assertFail(code: Int, response: ClientResponse, body: String, vararg headers: String) {
fun assertFail(code: Int, response: ClientResponse<String>, body: String, vararg headers: String) {
assert(response.status == code)
(headers.toList() + "b_all" + "a_all").forEach { assert(response.headers.contains(it)) }
assert(response.body == body)
Expand Down
4 changes: 2 additions & 2 deletions http_server/src/test/kotlin/examples/BooksTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ abstract class BooksTest(adapter: ServerPort) {
assert(405 == result.status)
}

private fun assertResponseContains(response: Response?, status: Int, vararg content: String) {
private fun assertResponseContains(response: Response<String>?, status: Int, vararg content: String) {
assert(response?.status == status)
content.forEach {
assert(response?.body?.contains(it) ?: false)
}
}

private fun assertResponseContains(response: Response?, vararg content: String) {
private fun assertResponseContains(response: Response<String>?, vararg content: String) {
assertResponseContains(response, 200, *content)
}
}
4 changes: 2 additions & 2 deletions http_server/src/test/kotlin/examples/ErrorsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ abstract class ErrorsTest(adapter: ServerPort) {
assertResponseContains(response, 500, "Root handler")
}

private fun assertResponseEquals(response: Response?, content: String, status: Int = 200) {
private fun assertResponseEquals(response: Response<String>?, content: String, status: Int = 200) {
assert (response?.status == status)
assert (response?.body == content)
}

private fun assertResponseContains(response: Response?, status: Int, vararg content: String) {
private fun assertResponseContains(response: Response<String>?, status: Int, vararg content: String) {
assert (response?.status == status)
content.forEach {
assert (response?.body?.contains (it) ?: false)
Expand Down
4 changes: 2 additions & 2 deletions http_server/src/test/kotlin/examples/FilesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ abstract class FilesTest(adapter: ServerPort) {
}
}

private fun assertResponseEquals(response: Response?, content: String, status: Int = 200) {
private fun assertResponseEquals(response: Response<String>?, content: String, status: Int = 200) {
assertEquals(status, response?.status)
assertEquals(content, response?.body?.trim())
}

private fun assertResponseContains(response: Response?, status: Int, vararg content: String) {
private fun assertResponseContains(response: Response<String>?, status: Int, vararg content: String) {
assert(response?.status == status)
content.forEach {
assert (response?.body?.contains (it) ?: false)
Expand Down
2 changes: 1 addition & 1 deletion http_server/src/test/kotlin/examples/FiltersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ abstract class FiltersTest(adapter: ServerPort) {
assert(response.headers["time"]?.first()?.toLong() ?: 0 > 0)
}

private fun assertResponseEquals(response: Response?, content: String, status: Int = 200) {
private fun assertResponseEquals(response: Response<String>?, content: String, status: Int = 200) {
assert (response?.status == status)
assert (response?.body == content)
}
Expand Down
6 changes: 3 additions & 3 deletions http_server/src/test/kotlin/examples/GenericTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -338,21 +338,21 @@ abstract class GenericTest(adapter: ServerPort) {
assert(200 == response.status)
}

private fun assertResponseEquals(response: Response?, content: String, status: Int = 200) {
private fun assertResponseEquals(response: Response<String>?, content: String, status: Int = 200) {
assert(response?.headers?.get("before")?.first() == "filter")
assert(response?.status == status)
assert(response?.body == content)
}

private fun assertResponseContains(response: Response?, status: Int, vararg content: String) {
private fun assertResponseContains(response: Response<String>?, status: Int, vararg content: String) {
assert(response?.headers?.get("before")?.first() == "filter")
assert(response?.status == status)
content.forEach {
assert(response?.body?.contains (it) ?: false)
}
}

private fun assertResponseContains(response: Response?, vararg content: String) {
private fun assertResponseContains(response: Response<String>?, vararg content: String) {
assertResponseContains(response, 200, *content)
}
}
2 changes: 1 addition & 1 deletion http_server/src/test/kotlin/examples/SessionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ abstract class SessionTest(adapter: ServerPort) {
assert(client.get("/session/access").body == "null")
}

private fun assertResponseEquals(response: Response?, content: String, status: Int = 200) {
private fun assertResponseEquals(response: Response<String>?, content: String, status: Int = 200) {
assert (response?.status == status)
assert (response?.body == content)
}
Expand Down
4 changes: 2 additions & 2 deletions web/src/test/kotlin/examples/TodoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ abstract class TodoTest(adapter: ServerPort) {
assertResponseContains(result, 404, "not found")
}

private fun assertResponseContains(response: Response?, status: Int, vararg content: String) {
private fun assertResponseContains(response: Response<String>?, status: Int, vararg content: String) {
assert(response?.status == status)
content.forEach {
assert(response?.body?.contains(it) ?: false)
}
}

private fun assertResponseContains(response: Response?, vararg content: String) {
private fun assertResponseContains(response: Response<String>?, vararg content: String) {
assertResponseContains(response, 200, *content)
}
}

0 comments on commit 2fce52b

Please sign in to comment.