-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathUriTests.scala
285 lines (250 loc) · 10.2 KB
/
UriTests.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package sttp.model
import org.scalatest.TryValues
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import sttp.model.Uri._
import sttp.model.internal.UriCompatibility
import java.net.URI
class UriTests extends AnyFunSuite with Matchers with TryValues with UriTestsExtension {
val HS = HostSegment
val PS = PathSegment
val QS = QuerySegment
val wholeUriTestData = List(
Uri.unsafeApply("http", None, "example.com", None, Nil, Nil, None) -> "http://example.com",
Uri.unsafeApply("http", None, "example.com", None, List(""), Nil, None) -> "http://example.com/",
Uri.unsafeApply(
"https",
None,
"sub.example.com",
Some(8080),
List("a", "b", "xyz"),
List(QS.KeyValue("p1", "v1"), QS.KeyValue("p2", "v2")),
Some("f")
) ->
"https://sub.example.com:8080/a/b/xyz?p1=v1&p2=v2#f",
Uri.unsafeApply(
"http",
None,
"example.com",
None,
List(""),
List(QS.KeyValue("p", "v"), QS.KeyValue("p", "v")),
None
) -> "http://example.com/?p=v&p=v",
Uri.unsafeApply(
"http",
None,
"exa mple.com",
None,
List("a b", "z", "ą:ę"),
List(QS.KeyValue("p:1", "v&v"), QS.KeyValue("p2", "v v")),
None
) ->
"http://exa%20mple.com/a%20b/z/%C4%85:%C4%99?p:1=v%26v&p2=v+v",
Uri.unsafeApply("http", Some(UserInfo("us&e/r", Some("pa ss"))), "example.com", None, Nil, Nil, None) ->
"http://us&e%2Fr:pa%20ss@example.com",
Uri.unsafeApply("http", None, "example.com", None, Nil, Nil, Some("f:g/h i")) ->
"http://example.com#f:g/h%20i",
Uri.unsafeApply("http", None, "example.com", None, List("key=value"), Nil, None) ->
"http://example.com/key=value",
Uri.unsafeApply("2001:db8::ff00:42:8329", 8080) -> "http://[2001:db8::ff00:42:8329]:8080",
Uri.unsafeApply(
"http",
Some(Authority("example.com")),
List(Segment("a b", identity)),
Nil,
None
) -> "http://example.com/a b",
Uri.unsafeApply("http", None, Nil, Nil, None) -> "http:",
Uri.unsafeApply("mailto", List("user@example.com")) -> "mailto:user@example.com",
Uri.unsafeApply("http", "sub..domain") -> "http://sub..domain",
Uri.relative(List("x", "y")) -> "/x/y",
Uri.relative(List("x", "y", "")) -> "/x/y/",
Uri.relative(List("")) -> "/",
Uri.relative(List("x"), Some("a")) -> "/x#a",
Uri.relative(List("x"), List(QS.KeyValue("p1", "v1")), Some("a")) -> "/x?p1=v1#a",
Uri.pathRelative(List("x", "y")) -> "x/y",
Uri.pathRelative(List("..", "x", "y")) -> "../x/y"
)
for {
(uri, expected) <- wholeUriTestData
} {
test(s"$uri should serialize to $expected") {
uri.toString should be(expected)
}
}
val testUri = Uri.unsafeApply("http", None, "example.com", None, Nil, Nil, None)
val pathTestData = List(
"a/b/c" -> List("a", "b", "c"),
"/a/b/c" -> List("a", "b", "c"),
"/" -> List(""),
"" -> List("")
)
for {
(path, expected) <- pathTestData
} {
test(s"whole path: $path, should parse as: $expected") {
testUri.withWholePath(path).path.toList should be(expected)
}
}
val querySegmentsTestData = List(
List(
QS.KeyValue("k1", "v1"),
QS.KeyValue("k2", "v2"),
QS.KeyValue("k3", "v3"),
QS.KeyValue("k4", "v4")
) -> "k1=v1&k2=v2&k3=v3&k4=v4",
List(
QS.KeyValue("k1", "v1"),
QS.KeyValue("k2", "v2"),
QS.Plain("-abc-"),
QS.KeyValue("k3", "v3"),
QS.KeyValue("k4", "v4")
) -> "k1=v1&k2=v2-abc-k3=v3&k4=v4",
List(QS.KeyValue("k1", "v1"), QS.Plain("&abc&"), QS.KeyValue("k2", "v2")) -> "k1=v1%26abc%26k2=v2",
List(QS.KeyValue("k1", "v1"), QS.Plain("&abc&", encoding = QuerySegmentEncoding.Relaxed)) -> "k1=v1&abc&",
List(QS.KeyValue("k1&", "v1&", keyEncoding = QuerySegmentEncoding.Relaxed)) -> "k1&=v1%26",
List(QS.KeyValue("k1&", "v1&", valueEncoding = QuerySegmentEncoding.Relaxed)) -> "k1%26=v1&",
List(QS.Plain("ą/ę&+;?", encoding = QuerySegmentEncoding.Relaxed)) -> "%C4%85/%C4%99&+;?",
List(QS.KeyValue("k", "v1,v2", valueEncoding = QuerySegmentEncoding.All)) -> "k=v1%2Cv2",
List(QS.KeyValue("k", "v1-v2", valueEncoding = QuerySegmentEncoding.All)) -> "k=v1-v2",
List(QS.KeyValue("k", "v1_v2", valueEncoding = QuerySegmentEncoding.All)) -> "k=v1_v2",
List(QS.KeyValue("k", "v1.v2", valueEncoding = QuerySegmentEncoding.All)) -> "k=v1.v2",
List(QS.KeyValue("k", "v1,v2")) -> "k=v1,v2",
List(QS.KeyValue("k", "+1234")) -> "k=%2B1234",
List(QS.KeyValue("k", "[]")) -> "k=%5B%5D",
List(QS.KeyValue("k", "[]", valueEncoding = QuerySegmentEncoding.RelaxedWithBrackets)) -> "k=[]"
)
for {
(segments, expected) <- querySegmentsTestData
} {
test(s"$segments should serialize to$expected") {
testUri.copy(querySegments = segments).toString should endWith(expected)
}
}
private val bodyPartEncodingTestData = List(
"v1,v2" -> "v1%2Cv2",
"v1-v2" -> "v1-v2",
"v1~v2" -> "v1~v2",
"v1_v2" -> "v1_v2",
"v1.v2" -> "v1.v2"
)
for {
(segments, expected) <- bodyPartEncodingTestData
} {
test(s"$segments should serialize to$expected") {
UriCompatibility.encodeBodyPart(segments, "utf-8") should endWith(expected)
}
}
val hostTestData = List(
"www.mikołak.net" -> "http://www.xn--mikoak-6db.net",
"192.168.1.0" -> "http://192.168.1.0",
"::1" -> "http://[::1]",
"2001:db8::ff00:42:8329" -> "http://[2001:db8::ff00:42:8329]",
"2001:0db8:0000:0000:0000:ff00:0042:8329" -> "http://[2001:0db8:0000:0000:0000:ff00:0042:8329]"
)
for {
(host, expected) <- hostTestData
} {
test(s"host $host should serialize to $expected") {
Uri.unsafeApply(host).toString should be(s"$expected")
}
}
test("should convert from java URI") {
val uriAsString = "https://sub.example.com:8080/a/b/xyz?p1=v1&p2=v2#f"
Uri(URI.create(uriAsString)).toString should be(uriAsString)
}
test("should parse raw string") {
val uriAsString = "https://sub.example.com:8080/a/b/xyz?p1=v1&p2=v2#f"
Uri.parse(uriAsString).right.map(_.toString) shouldBe Right(uriAsString)
val badString = "xyz://foobar:80:37/?&?"
Uri.parse(badString).isLeft shouldBe true
}
test("should convert to java URI") {
val uriAsString = "https://sub.example.com:8080/a/b/xyz?p1=v1&p2=v2#f"
uri"$uriAsString".toJavaUri.toString should be(uriAsString)
}
test("should have multi params") {
val uriAsString = "https://sub.example.com:8080?p1=v1&p2=v2&p1=v3&p2=v4"
val expectedParams = Map("p1" -> List("v1", "v3"), "p2" -> List("v2", "v4"))
uri"$uriAsString".params.toMultiMap should be(expectedParams)
}
test("should have no multi params") {
val uriAsString = "https://sub.example.com:8080"
uri"$uriAsString".params.toMultiMap should be(Map())
}
test("should have non-empty multi params") {
val uriAsString = "https://sub.example.com:8080?p1&p2"
uri"$uriAsString".params.toMultiMap should be(Map("p1" -> Nil, "p2" -> Nil))
}
test("should have multi params with empty string values") {
val uriAsString = "https://sub.example.com:8080?p1=&p2="
uri"$uriAsString".params.toMultiMap should be(Map("p1" -> List(""), "p2" -> List("")))
}
test("should have multi params with only values") {
val uriAsString = "https://sub.example.com:8080?=v1&=v2"
uri"$uriAsString".params.toMultiMap should be(Map("" -> List("v1", "v2")))
}
test("should replace or append path") {
uri"http://example.org/x/y".addPath("z").path shouldBe List("x", "y", "z")
uri"http://example.org/x/y".withPath("z").path shouldBe List("z")
}
test("should replace or append query parameter") {
uri"http://example.org?x=1".addParam("y", "2").paramsMap shouldBe Map("x" -> "1", "y" -> "2")
uri"http://example.org?x=1".withParam("y", "2").paramsMap shouldBe Map("y" -> "2")
}
test("should parse no-value parameters") {
val uriAsString = "https://sub.example.com:8080?p1&p2=v&p3"
uri"$uriAsString".params.getMulti("p1") shouldBe Some(Nil)
uri"$uriAsString".params.getMulti("p2") shouldBe Some(List("v"))
uri"$uriAsString".params.getMulti("p3") shouldBe Some(Nil)
}
val validationTestData = List(
(() => Uri.unsafeApply("")) -> "host cannot be empty",
(() => Uri.unsafeApply("h ttp", "example.org")) -> "scheme"
)
for {
(createUri, expectedException) <- validationTestData
} {
test(s"""should validate and throw "$expectedException" if not valid""") {
val caught = intercept[IllegalArgumentException] {
createUri()
}
caught.getMessage.toLowerCase() should include(expectedException)
}
}
test("should add path ignoring the trailing empty segment if necessary") {
uri"http://x.com".addPath("a").toString shouldBe "http://x.com/a"
uri"http://x.com/".addPath("a").toString shouldBe "http://x.com/a"
uri"http://x.com/a".addPath("b").toString shouldBe "http://x.com/a/b"
uri"http://x.com".addPath("a", "b").toString shouldBe "http://x.com/a/b"
uri"http://x.com/".addPath("a", "b").toString shouldBe "http://x.com/a/b"
uri"/".addPath("a").toString shouldBe "/a"
uri"/a".addPath("b").toString shouldBe "/a/b"
uri"a".addPath("b").toString shouldBe "a/b"
uri"a/".addPath("b").toString shouldBe "a/b"
}
test("should parse a relative uri with an absolute path") {
def pathSegment(s: String) = Uri.Segment(s, Uri.PathSegmentEncoding.Standard)
uri"/x/y".pathSegments shouldBe Uri.AbsolutePath(List(pathSegment("x"), pathSegment("y")))
uri"${"/x/y"}".pathSegments shouldBe Uri.AbsolutePath(List(pathSegment("x"), pathSegment("y")))
}
test("should serialize path") {
uri"http://x.com/a/b/c".pathToString shouldBe "/a/b/c"
uri"http://x.com".pathToString shouldBe ""
uri"http://x.com/".pathToString shouldBe "/"
uri"http://x.com/a%20c".pathToString shouldBe "/a%20c"
}
test("should serialize query") {
uri"http://x.com/a/b/c".queryToString shouldBe ""
uri"http://x.com?a=b&c=d".queryToString shouldBe "a=b&c=d"
uri"http://x.com/a/b/c?p1=1%202".queryToString shouldBe "p1=1+2"
}
test("should serialize fragment") {
uri"http://x.com/a/b/c".fragmentToString shouldBe ""
uri"http://x.com/a/b/c#d".fragmentToString shouldBe "d"
}
test("should serialize scheme") {
uri"http://x.com/a/b/c".schemeToString shouldBe "http"
}
}