Skip to content

Commit dea088f

Browse files
author
Matt Thompson
committed
Make Response body generic as per todo hexagontk#369
Existing uses retain Response as type String
1 parent 4653294 commit dea088f

File tree

13 files changed

+38
-36
lines changed

13 files changed

+38
-36
lines changed

http_client/src/main/kotlin/Client.kt

+18-16
Original file line numberDiff line numberDiff line change
@@ -19,92 +19,94 @@ class Client(
1919
/**
2020
* Synchronous execution.
2121
*/
22-
fun send(request: Request): Response =
22+
fun send(request: Request): Response<String> =
2323
adapter.send(this, request)
2424

2525
fun get(
2626
path: String,
2727
headers: Map<String, List<String>> = emptyMap(),
2828
body: Any? = null,
29-
contentType: String? = settings.contentType): Response =
29+
contentType: String? = settings.contentType): Response<String> =
3030
send(Request(GET, Path(path), body, headers = headers, contentType = contentType))
3131

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

3535
fun post(
36-
path: String, body: Any? = null, contentType: String? = settings.contentType): Response =
36+
path: String,
37+
body: Any? = null,
38+
contentType: String? = settings.contentType): Response<String> =
3739
send(Request(POST, Path(path), body, contentType = contentType))
3840

3941
fun put(
4042
path: String,
4143
body: Any? = null,
42-
contentType: String? = settings.contentType): Response =
44+
contentType: String? = settings.contentType): Response<String> =
4345
send(Request(PUT, Path(path), body, contentType = contentType))
4446

4547
fun delete(
4648
path: String,
4749
body: Any? = null,
48-
contentType: String? = settings.contentType): Response =
50+
contentType: String? = settings.contentType): Response<String> =
4951
send(Request(DELETE, Path(path), body, contentType = contentType))
5052

5153
fun trace(
5254
path: String,
5355
body: Any? = null,
54-
contentType: String? = settings.contentType): Response =
56+
contentType: String? = settings.contentType): Response<String> =
5557
send(Request(TRACE, Path(path), body, contentType = contentType))
5658

5759
fun options(
5860
path: String,
5961
body: Any? = null,
6062
contentType: String? = settings.contentType,
61-
headers: Map<String, List<String>> = emptyMap()): Response =
63+
headers: Map<String, List<String>> = emptyMap()): Response<String> =
6264
send(Request(OPTIONS, Path(path), body, headers, contentType = contentType))
6365

6466
fun patch(
6567
path: String,
6668
body: Any? = null,
67-
contentType: String? = settings.contentType): Response =
69+
contentType: String? = settings.contentType): Response<String> =
6870
send(Request(PATCH, Path(path), body, contentType = contentType))
6971

7072
fun get(
7173
path: String,
7274
headers: Map<String, List<String>> = emptyMap(),
7375
body: Any,
74-
format: SerializationFormat): Response =
76+
format: SerializationFormat): Response<String> =
7577
get(path, headers, body, format.contentType)
7678

77-
fun post(path: String, body: Any, format: SerializationFormat): Response =
79+
fun post(path: String, body: Any, format: SerializationFormat): Response<String> =
7880
post(path, body, format.contentType)
7981

8082
fun put(
8183
path: String,
8284
body: Any,
83-
format: SerializationFormat): Response =
85+
format: SerializationFormat): Response<String> =
8486
put(path, body, format.contentType)
8587

8688
fun delete(
8789
path: String,
8890
body: Any,
89-
format: SerializationFormat): Response =
91+
format: SerializationFormat): Response<String> =
9092
delete(path, body, format.contentType)
9193

9294
fun trace(
9395
path: String,
9496
body: Any,
95-
format: SerializationFormat): Response =
97+
format: SerializationFormat): Response<String> =
9698
trace(path, body, format.contentType)
9799

98100
fun options(
99101
path: String,
100102
body: Any,
101103
format: SerializationFormat,
102-
headers: Map<String, List<String>> = emptyMap()): Response =
104+
headers: Map<String, List<String>> = emptyMap()): Response<String> =
103105
options(path, body, format.contentType, headers)
104106

105107
fun patch(
106108
path: String,
107109
body: Any,
108-
format: SerializationFormat): Response =
110+
format: SerializationFormat): Response<String> =
109111
patch(path, body, format.contentType)
110112
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.hexagonkt.http.client
22

33
interface ClientPort {
4-
fun send(client: Client, request: Request): Response
4+
fun send(client: Client, request: Request): Response<String>
55
}

http_client/src/main/kotlin/Response.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import java.io.InputStream
55
/**
66
* HTTP response fetched from a server request.
77
*/
8-
data class Response(
8+
data class Response <T> (
99
var status: Int,
10-
var body: String?, // TODO Change by generic T
10+
var body: T?,
1111
val headers: MutableMap<String, List<String>>,
1212
var contentType: String?,
1313
val inputStream: InputStream

http_client/src/test/kotlin/ClientTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ abstract class ClientTest(private val adapter: () -> ClientPort) {
299299
}
300300

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

304304
assert(response.status == 200)
305305
assert(response.body?.trim() == parameter?.serialize(format)?.trim() ?: "")

http_client_ahc/src/main/kotlin/AhcAdapter.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class AhcAdapter : ClientPort {
113113
return keyStore
114114
}
115115

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

118118
val settings: ClientSettings = client.settings
119119
val ahcRequest = createRequest(client, request)

http_server/src/test/kotlin/PortHttpServerSamplesTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,13 @@ abstract class PortHttpServerSamplesTest(val adapter: ServerPort) {
323323
}
324324

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

332-
fun assertFail(code: Int, response: ClientResponse, body: String, vararg headers: String) {
332+
fun assertFail(code: Int, response: ClientResponse<String>, body: String, vararg headers: String) {
333333
assert(response.status == code)
334334
(headers.toList() + "b_all" + "a_all").forEach { assert(response.headers.contains(it)) }
335335
assert(response.body == body)

http_server/src/test/kotlin/examples/BooksTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,14 @@ abstract class BooksTest(adapter: ServerPort) {
128128
assert(405 == result.status)
129129
}
130130

131-
private fun assertResponseContains(response: Response?, status: Int, vararg content: String) {
131+
private fun assertResponseContains(response: Response<String>?, status: Int, vararg content: String) {
132132
assert(response?.status == status)
133133
content.forEach {
134134
assert(response?.body?.contains(it) ?: false)
135135
}
136136
}
137137

138-
private fun assertResponseContains(response: Response?, vararg content: String) {
138+
private fun assertResponseContains(response: Response<String>?, vararg content: String) {
139139
assertResponseContains(response, 200, *content)
140140
}
141141
}

http_server/src/test/kotlin/examples/ErrorsTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ abstract class ErrorsTest(adapter: ServerPort) {
8383
assertResponseContains(response, 500, "Root handler")
8484
}
8585

86-
private fun assertResponseEquals(response: Response?, content: String, status: Int = 200) {
86+
private fun assertResponseEquals(response: Response<String>?, content: String, status: Int = 200) {
8787
assert (response?.status == status)
8888
assert (response?.body == content)
8989
}
9090

91-
private fun assertResponseContains(response: Response?, status: Int, vararg content: String) {
91+
private fun assertResponseContains(response: Response<String>?, status: Int, vararg content: String) {
9292
assert (response?.status == status)
9393
content.forEach {
9494
assert (response?.body?.contains (it) ?: false)

http_server/src/test/kotlin/examples/FilesTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ abstract class FilesTest(adapter: ServerPort) {
139139
}
140140
}
141141

142-
private fun assertResponseEquals(response: Response?, content: String, status: Int = 200) {
142+
private fun assertResponseEquals(response: Response<String>?, content: String, status: Int = 200) {
143143
assertEquals(status, response?.status)
144144
assertEquals(content, response?.body?.trim())
145145
}
146146

147-
private fun assertResponseContains(response: Response?, status: Int, vararg content: String) {
147+
private fun assertResponseContains(response: Response<String>?, status: Int, vararg content: String) {
148148
assert(response?.status == status)
149149
content.forEach {
150150
assert (response?.body?.contains (it) ?: false)

http_server/src/test/kotlin/examples/FiltersTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ abstract class FiltersTest(adapter: ServerPort) {
9797
assert(response.headers["time"]?.first()?.toLong() ?: 0 > 0)
9898
}
9999

100-
private fun assertResponseEquals(response: Response?, content: String, status: Int = 200) {
100+
private fun assertResponseEquals(response: Response<String>?, content: String, status: Int = 200) {
101101
assert (response?.status == status)
102102
assert (response?.body == content)
103103
}

http_server/src/test/kotlin/examples/GenericTest.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -338,21 +338,21 @@ abstract class GenericTest(adapter: ServerPort) {
338338
assert(200 == response.status)
339339
}
340340

341-
private fun assertResponseEquals(response: Response?, content: String, status: Int = 200) {
341+
private fun assertResponseEquals(response: Response<String>?, content: String, status: Int = 200) {
342342
assert(response?.headers?.get("before")?.first() == "filter")
343343
assert(response?.status == status)
344344
assert(response?.body == content)
345345
}
346346

347-
private fun assertResponseContains(response: Response?, status: Int, vararg content: String) {
347+
private fun assertResponseContains(response: Response<String>?, status: Int, vararg content: String) {
348348
assert(response?.headers?.get("before")?.first() == "filter")
349349
assert(response?.status == status)
350350
content.forEach {
351351
assert(response?.body?.contains (it) ?: false)
352352
}
353353
}
354354

355-
private fun assertResponseContains(response: Response?, vararg content: String) {
355+
private fun assertResponseContains(response: Response<String>?, vararg content: String) {
356356
assertResponseContains(response, 200, *content)
357357
}
358358
}

http_server/src/test/kotlin/examples/SessionTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ abstract class SessionTest(adapter: ServerPort) {
113113
assert(client.get("/session/access").body == "null")
114114
}
115115

116-
private fun assertResponseEquals(response: Response?, content: String, status: Int = 200) {
116+
private fun assertResponseEquals(response: Response<String>?, content: String, status: Int = 200) {
117117
assert (response?.status == status)
118118
assert (response?.body == content)
119119
}

web/src/test/kotlin/examples/TodoTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,14 @@ abstract class TodoTest(adapter: ServerPort) {
152152
assertResponseContains(result, 404, "not found")
153153
}
154154

155-
private fun assertResponseContains(response: Response?, status: Int, vararg content: String) {
155+
private fun assertResponseContains(response: Response<String>?, status: Int, vararg content: String) {
156156
assert(response?.status == status)
157157
content.forEach {
158158
assert(response?.body?.contains(it) ?: false)
159159
}
160160
}
161161

162-
private fun assertResponseContains(response: Response?, vararg content: String) {
162+
private fun assertResponseContains(response: Response<String>?, vararg content: String) {
163163
assertResponseContains(response, 200, *content)
164164
}
165165
}

0 commit comments

Comments
 (0)