Skip to content

Commit 7f78198

Browse files
committed
Test HttpConnection
* Added tests for HttpConnection
1 parent 8988a72 commit 7f78198

File tree

3 files changed

+137
-31
lines changed

3 files changed

+137
-31
lines changed

src/main/kotlin/com/adamratzman/spotify/utils/Endpoints.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ abstract class SpotifyEndpoint(val api: SpotifyAPI) {
5050
cache -= spotifyRequest
5151
}
5252

53-
val document = createConnection(url, body, method, contentType).apply {
54-
if (cacheState?.eTag != null) headers.add(HttpHeader("If-None-Match", cacheState.eTag))
55-
}.execute()
53+
val document = createConnection(url, body, method, contentType).execute(
54+
cacheState?.eTag?.let {
55+
HttpHeader("If-None-Match", it)
56+
}
57+
)
5658

5759
return handleResponse(document, cacheState, spotifyRequest, retry202) ?: execute(
5860
url,

src/main/kotlin/com/adamratzman/spotify/utils/HttpConnection.kt

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,44 @@ package com.adamratzman.spotify.utils
33
import java.net.HttpURLConnection
44
import java.net.URL
55

6-
enum class HttpRequestMethod { GET, POST, PUT, DELETE, }
6+
enum class HttpRequestMethod { GET, POST, PUT, DELETE }
77
data class HttpHeader(val key: String, val value: String)
88

99
class HttpConnection(
1010
private val url: String,
1111
private val method: HttpRequestMethod,
1212
private val body: String?,
1313
private val contentType: String?,
14-
vararg headersVararg: HttpHeader,
15-
val headers: MutableList<HttpHeader> = headersVararg.toMutableList()
14+
private vararg val headers: HttpHeader
1615
) {
1716

18-
fun execute(): HttpResponse {
17+
fun execute(vararg additionalHeaders: HttpHeader?): HttpResponse {
1918
val connection = URL(url).openConnection() as HttpURLConnection
2019
connection.requestMethod = method.toString()
2120

2221
contentType?.let { connection.setRequestProperty("Content-Type", contentType) }
23-
headers.forEach { (key, value) -> connection.setRequestProperty(key, value) }
24-
25-
if (body != null || method != HttpRequestMethod.GET){
26-
connection.doOutput = true
27-
connection.setFixedLengthStreamingMode(body?.length ?: 0)
22+
headers.union(additionalHeaders.filterNotNull()).forEach { (key, value) ->
23+
connection.setRequestProperty(key, value)
2824
}
2925

30-
body?.let { _ ->
31-
connection.outputStream.bufferedWriter().also { it.write(body) }.let { it.close() }
26+
if (body != null || method != HttpRequestMethod.GET) {
27+
connection.doOutput = true
28+
connection.setFixedLengthStreamingMode(body?.toByteArray()?.size ?: 0)
29+
connection.outputStream.bufferedWriter().use {
30+
body?.also(it::write)
31+
}
3232
}
3333

3434
connection.connect()
3535

36-
val responseCode = connection.responseCode
37-
38-
val outputBody = (connection.errorStream ?: connection.inputStream).bufferedReader().let {
39-
val text = it.readText()
40-
it.close()
41-
text
42-
}
43-
44-
val headers = connection.headerFields.keys.filterNotNull().map { HttpHeader(it, connection.getHeaderField(it)) }
45-
connection.disconnect()
46-
47-
return HttpResponse(responseCode, outputBody, headers)
36+
return HttpResponse(
37+
responseCode = connection.responseCode,
38+
body = (connection.errorStream ?: connection.inputStream).bufferedReader().use {
39+
it.readText()
40+
},
41+
headers = connection.headerFields.keys.filterNotNull().map { HttpHeader(it, connection.getHeaderField(it)) }
42+
).also { connection.disconnect() }
4843
}
4944
}
5045

51-
data class HttpResponse(val responseCode: Int, val body: String, val headers: List<HttpHeader>)
46+
data class HttpResponse(val responseCode: Int, val body: String, val headers: List<HttpHeader>)

src/test/kotlin/com/adamratzman/spotify/utilities/HttpConnectionTests.kt

Lines changed: 113 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,126 @@ package com.adamratzman.spotify.utilities
22

33
import com.adamratzman.spotify.utils.HttpConnection
44
import com.adamratzman.spotify.utils.HttpRequestMethod
5+
import org.json.JSONObject
56
import org.junit.jupiter.api.Assertions.assertEquals
67
import org.spekframework.spek2.Spek
78
import org.spekframework.spek2.style.specification.describe
89

910
class HttpConnectionTests : Spek({
1011
describe("http connection testing") {
11-
it("get request") {
12+
describe("get request") {
13+
val (response, body) = HttpConnection(
14+
"https://httpbin.org/get?query=string",
15+
HttpRequestMethod.GET,
16+
null,
17+
"text/html"
18+
).execute().let { it to JSONObject(it.body) }
19+
20+
it("get request response code") {
21+
assertEquals(200, response.responseCode)
22+
}
23+
24+
it("get request header") {
25+
val requestHeader = body.getJSONObject("headers")
26+
assertEquals(
27+
mapOf(
28+
"Accept" to "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
29+
"Connection" to "close",
30+
"Host" to "httpbin.org",
31+
"Content-Type" to "text/html"
32+
),
33+
// ignore the user-agent because of the version in it
34+
requestHeader.toMap().filterKeys { it != "User-Agent" }
35+
)
36+
}
37+
38+
it("get request query string") {
39+
assertEquals("string", body.getJSONObject("args").getString("query"))
40+
}
41+
}
42+
43+
describe("post request") {
44+
val (response, body) = HttpConnection(
45+
"https://httpbin.org/post?query=string",
46+
HttpRequestMethod.POST,
47+
"body",
48+
"text/html"
49+
).execute().let { it to JSONObject(it.body) }
50+
51+
it("post request response code") {
52+
assertEquals(200, response.responseCode)
53+
}
54+
55+
it("post request header") {
56+
val requestHeader = body.getJSONObject("headers")
57+
assertEquals(
58+
mapOf(
59+
"Accept" to "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
60+
"Connection" to "close",
61+
"Host" to "httpbin.org",
62+
"Content-Type" to "text/html",
63+
"Content-Length" to "4"
64+
),
65+
// ignore the user-agent because of the version in it
66+
requestHeader.toMap().filterKeys { it != "User-Agent" }
67+
)
68+
}
69+
70+
it("post request query string") {
71+
assertEquals("string", body.getJSONObject("args").getString("query"))
72+
}
73+
74+
it("post request body") {
75+
assertEquals("body", body.getString("data"))
76+
}
77+
}
78+
79+
describe("delete request") {
80+
val (response, body) = HttpConnection(
81+
"https://httpbin.org/delete?query=string",
82+
HttpRequestMethod.DELETE,
83+
"body",
84+
"text/html"
85+
).execute().let { it to JSONObject(it.body) }
86+
87+
it("delete request response code") {
88+
assertEquals(200, response.responseCode)
89+
}
90+
91+
it("delete request header") {
92+
val requestHeader = body.getJSONObject("headers")
93+
assertEquals(
94+
mapOf(
95+
"Accept" to "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
96+
"Connection" to "close",
97+
"Host" to "httpbin.org",
98+
"Content-Type" to "text/html",
99+
"Content-Length" to "4"
100+
),
101+
// ignore the user-agent because of the version in it
102+
requestHeader.toMap().filterKeys { it != "User-Agent" }
103+
)
104+
}
105+
106+
it("delete request query string") {
107+
assertEquals("string", body.getJSONObject("args").getString("query"))
108+
}
109+
110+
it("delete request body") {
111+
assertEquals("body", body.getString("data"))
112+
}
113+
}
114+
115+
it("status code") {
12116
assertEquals(
13-
200, HttpConnection("https://apple.com", HttpRequestMethod.GET, null, null)
14-
.execute().responseCode
117+
102,
118+
HttpConnection(
119+
"https://httpbin.org/status/102",
120+
HttpRequestMethod.GET,
121+
null,
122+
null
123+
).execute().responseCode
15124
)
16125
}
17126
}
18-
})
127+
})

0 commit comments

Comments
 (0)