Skip to content

Commit 7000fca

Browse files
committed
BAEL-1398 Code for the article: HTTP Requests with Kotlin and khttp
1 parent 4076071 commit 7000fca

2 files changed

Lines changed: 158 additions & 0 deletions

File tree

‎core-kotlin/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
<artifactId>kotlin-stdlib-jre8</artifactId>
5050
<version>${kotlin-stdlib.version}</version>
5151
</dependency>
52+
<dependency>
53+
<groupId>khttp</groupId>
54+
<artifactId>khttp</artifactId>
55+
<version>0.1.0</version>
56+
</dependency>
5257
<dependency>
5358
<groupId>org.jetbrains.kotlin</groupId>
5459
<artifactId>kotlin-test-junit</artifactId>
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package com.baeldung.kotlin.khttp
2+
3+
import khttp.structures.files.FileLike
4+
import org.json.JSONObject
5+
import org.junit.Test
6+
import java.beans.ExceptionListener
7+
import java.beans.XMLEncoder
8+
import java.io.*
9+
import java.lang.Exception
10+
import java.net.ConnectException
11+
import kotlin.test.assertEquals
12+
import kotlin.test.assertTrue
13+
import kotlin.test.fail
14+
15+
class KhttpTest {
16+
17+
@Test
18+
fun whenHttpGetRequestIsMade_thenArgsAreReturned() {
19+
val response = khttp.get(
20+
url = "http://httpbin.org/get",
21+
params = mapOf("p1" to "1", "p2" to "2"))
22+
val args = response.jsonObject.getJSONObject("args")
23+
24+
assertEquals("1", args["p1"])
25+
assertEquals("2", args["p2"])
26+
}
27+
28+
@Test
29+
fun whenAlternateHttpGetRequestIsMade_thenArgsAreReturned() {
30+
val response = khttp.request(
31+
method = "GET",
32+
url = "http://httpbin.org/get",
33+
params = mapOf("p1" to "1", "p2" to "2"))
34+
val args = response.jsonObject.getJSONObject("args")
35+
36+
assertEquals("1", args["p1"])
37+
assertEquals("2", args["p2"])
38+
}
39+
40+
@Test
41+
fun whenHeadersAreSet_thenHeadersAreSent() {
42+
val response = khttp.get(
43+
url = "http://httpbin.org/get",
44+
headers = mapOf("header1" to "1", "header2" to "2"))
45+
val headers = response.jsonObject.getJSONObject("headers")
46+
47+
assertEquals("1", headers["Header1"])
48+
assertEquals("2", headers["Header2"])
49+
}
50+
51+
@Test
52+
fun whenHttpPostRequestIsMadeWithJson_thenBodyIsReturned() {
53+
val response = khttp.post(
54+
url = "http://httpbin.org/post",
55+
params = mapOf("p1" to "1", "p2" to "2"),
56+
json = mapOf("pr1" to "1", "pr2" to "2"))
57+
58+
val args = response.jsonObject.getJSONObject("args")
59+
60+
assertEquals("1", args["p1"])
61+
assertEquals("2", args["p2"])
62+
63+
val json = response.jsonObject.getJSONObject("json")
64+
65+
assertEquals("1", json["pr1"])
66+
assertEquals("2", json["pr2"])
67+
}
68+
69+
@Test
70+
fun whenHttpPostRequestIsMadeWithMapData_thenBodyIsReturned() {
71+
val response = khttp.post(
72+
url = "http://httpbin.org/post",
73+
params = mapOf("p1" to "1", "p2" to "2"),
74+
data = mapOf("pr1" to "1", "pr2" to "2"))
75+
76+
val args = response.jsonObject.getJSONObject("args")
77+
78+
assertEquals("1", args["p1"])
79+
assertEquals("2", args["p2"])
80+
81+
val form = response.jsonObject.getJSONObject("form")
82+
83+
assertEquals("1", form["pr1"])
84+
assertEquals("2", form["pr2"])
85+
}
86+
87+
@Test
88+
fun whenHttpPostRequestIsMadeWithFiles_thenBodyIsReturned() {
89+
val response = khttp.post(
90+
url = "http://httpbin.org/post",
91+
params = mapOf("p1" to "1", "p2" to "2"),
92+
files = listOf(
93+
FileLike("file1", "content1"),
94+
FileLike("file2", javaClass.getResource("KhttpTest.class").openStream().readBytes())))
95+
96+
val args = response.jsonObject.getJSONObject("args")
97+
98+
assertEquals("1", args["p1"])
99+
assertEquals("2", args["p2"])
100+
101+
val files = response.jsonObject.getJSONObject("files")
102+
103+
assertEquals("content1", files["file1"])
104+
}
105+
106+
@Test
107+
fun whenHttpPostRequestIsMadeWithInputStream_thenBodyIsReturned() {
108+
val response = khttp.post(
109+
url = "http://httpbin.org/post",
110+
params = mapOf("p1" to "1", "p2" to "2"),
111+
data = ByteArrayInputStream("content!".toByteArray()))
112+
113+
val args = response.jsonObject.getJSONObject("args")
114+
115+
assertEquals("1", args["p1"])
116+
assertEquals("2", args["p2"])
117+
118+
assertEquals("content!", response.jsonObject["data"])
119+
}
120+
121+
@Test
122+
fun whenHttpPostStreamingRequestIsMade_thenBodyIsReturnedInChunks() {
123+
val response = khttp.post(
124+
url = "http://httpbin.org/post",
125+
stream = true,
126+
json = mapOf("pr1" to "1", "pr2" to "2"))
127+
128+
val baos = ByteArrayOutputStream()
129+
response.contentIterator(chunkSize = 10).forEach { arr : ByteArray -> baos.write(arr) }
130+
val json = JSONObject(String(baos.toByteArray())).getJSONObject("json")
131+
132+
assertEquals("1", json["pr1"])
133+
assertEquals("2", json["pr2"])
134+
}
135+
136+
@Test
137+
fun whenHttpRequestFails_thenExceptionIsThrown() {
138+
try {
139+
khttp.get(url = "http://localhost/nothing/to/see/here")
140+
141+
fail("Should have thrown an exception")
142+
} catch (e : ConnectException) {
143+
//Ok
144+
}
145+
}
146+
147+
@Test
148+
fun whenHttpNotFound_thenExceptionIsThrown() {
149+
val response = khttp.get(url = "http://httpbin.org/nothing/to/see/here")
150+
151+
assertEquals(404, response.statusCode)
152+
}
153+
}

0 commit comments

Comments
 (0)