@@ -2,17 +2,126 @@ package com.adamratzman.spotify.utilities
22
33import com.adamratzman.spotify.utils.HttpConnection
44import com.adamratzman.spotify.utils.HttpRequestMethod
5+ import org.json.JSONObject
56import org.junit.jupiter.api.Assertions.assertEquals
67import org.spekframework.spek2.Spek
78import org.spekframework.spek2.style.specification.describe
89
910class 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