Skip to content

Commit 6a9e02e

Browse files
authored
Much more url tests (#363)
1 parent 0fda645 commit 6a9e02e

File tree

5 files changed

+280
-9
lines changed

5 files changed

+280
-9
lines changed

ci/expect_scripts/url.exp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/expect
2+
3+
# uncomment line below for debugging
4+
# exp_internal 1
5+
6+
set timeout 7
7+
8+
source ./ci/expect_scripts/shared-code.exp
9+
10+
spawn $env(TESTS_DIR)url
11+
12+
expect "Testing Url module functions..." {
13+
expect "Created URL: https://example.com" {
14+
expect "Testing Url.append:" {
15+
expect "URL with append: https://example.com/some%20stuff" {
16+
expect "URL with query and fragment, then appended path: https://example.com/stuff?search=blah#fragment" {
17+
expect "URL with multiple appended paths: https://example.com/things/stuff/more/etc/" {
18+
expect "Testing Url.append_param:" {
19+
expect "URL with appended param: https://example.com?email=someone%40example.com" {
20+
expect "URL with multiple appended params: https://example.com?caf%C3%A9=du%20Monde&email=hi%40example.com" {
21+
expect "Testing Url.has_query:" {
22+
expect "URL with query has_query: Bool.true" {
23+
expect "URL without query has_query: Bool.false" {
24+
expect "Testing Url.has_fragment:" {
25+
expect "URL with fragment has_fragment: Bool.true" {
26+
expect "URL without fragment has_fragment: Bool.false" {
27+
expect "Testing Url.query:" {
28+
expect "Query from URL: key1=val1&key2=val2&key3=val3" {
29+
expect "Query from URL without query: " {
30+
expect "Testing Url.fragment:" {
31+
expect "Fragment from URL: stuff" {
32+
expect "Fragment from URL without fragment: " {
33+
expect "Testing Url.reserve:" {
34+
expect "URL with reserved capacity and params: https://example.com/stuff?caf%C3%A9=du%20Monde&email=hi%40example.com" {
35+
expect "Testing Url.with_query:" {
36+
expect "URL with replaced query: https://example.com?newQuery=thisRightHere#stuff" {
37+
expect "URL with removed query: https://example.com#stuff" {
38+
expect "Testing Url.with_fragment:" {
39+
expect "URL with replaced fragment: https://example.com#things" {
40+
expect "URL with added fragment: https://example.com#things" {
41+
expect "URL with removed fragment: https://example.com" {
42+
expect "Testing Url.query_params:" {
43+
expect "params_dict: {\"key1\": \"val1\", \"key2\": \"val2\", \"key3\": \"val3\"}" {
44+
expect "Testing Url.path:" {
45+
expect "Path from URL: example.com/foo/bar" {
46+
expect "Path from relative URL: /foo/bar" {
47+
expect "All tests executed!" {
48+
expect eof {
49+
check_exit_and_segfault
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+
puts stderr "\nExpect script failed: output was different from expected value."
89+
exit 1

flake.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

platform/Url.roc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ to_str = |@Url(str)| str
9292
## |> Url.append("stuff")
9393
##
9494
## # Gives https://example.com/things/stuff/more/etc/"
95-
## Url.from_str "https://example.com/things/"
95+
## Url.from_str("https://example.com/things/")
9696
## |> Url.append("/stuff/")
9797
## |> Url.append("/more/etc/")
9898
##
@@ -102,7 +102,12 @@ to_str = |@Url(str)| str
102102
## ```
103103
append : Url, Str -> Url
104104
append = |@Url(url_str), suffix_unencoded|
105-
suffix = percent_encode(suffix_unencoded)
105+
# percent-encode the suffix but not the slashes
106+
suffix =
107+
suffix_unencoded
108+
|> Str.split_on("/")
109+
|> List.map(percent_encode)
110+
|> Str.join_with("/")
106111

107112
when Str.split_first(url_str, "?") is
108113
Ok({ before, after }) ->

tests/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
sqlite
2-
utc
2+
utc
3+
url

tests/url.roc

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
app [main!] { pf: platform "../platform/main.roc" }
2+
3+
import pf.Stdout
4+
import pf.Url
5+
import pf.Arg exposing [Arg]
6+
7+
main! : List Arg => Result {} _
8+
main! = |_args|
9+
Stdout.line!("Testing Url module functions...")?
10+
11+
# Need to split this up due to high memory consumption bug
12+
test_part_1!({})?
13+
test_part_2!({})?
14+
15+
Stdout.line!("\nAll tests executed!")?
16+
17+
Ok({})
18+
19+
test_part_1! : {} => Result {} _
20+
test_part_1! = |{}|
21+
# Test Url.from_str and Url.to_str
22+
url = Url.from_str("https://example.com")
23+
Stdout.line!("Created URL: ${Url.to_str(url)}")?
24+
# expects "https://example.com"
25+
26+
Stdout.line!("Testing Url.append:")?
27+
28+
urlWithPath = Url.append(url, "some stuff")
29+
Stdout.line!("URL with append: ${Url.to_str(urlWithPath)}")?
30+
# expects "https://example.com/some%20stuff"
31+
32+
url_search = Url.from_str("https://example.com?search=blah#fragment")
33+
url_search_append = Url.append(url_search, "stuff")
34+
Stdout.line!("URL with query and fragment, then appended path: ${Url.to_str(url_search_append)}")?
35+
# expects "https://example.com/stuff?search=blah#fragment"
36+
37+
url_things = Url.from_str("https://example.com/things/")
38+
url_things_append = Url.append(url_things, "/stuff/")
39+
url_things_append_more = Url.append(url_things_append, "/more/etc/")
40+
Stdout.line!("URL with multiple appended paths: ${Url.to_str(url_things_append_more)}")?
41+
# expects "https://example.com/things/stuff/more/etc/")
42+
43+
# Test Url.append_param
44+
Stdout.line!("Testing Url.append_param:")?
45+
46+
url_example = Url.from_str("https://example.com")
47+
url_example_param = Url.append_param(url_example, "email", "[email protected]")
48+
Stdout.line!("URL with appended param: ${Url.to_str(url_example_param)}")?
49+
# expects "https://example.com?email=someone%40example.com"
50+
51+
url_example_2 = Url.from_str("https://example.com")
52+
url_example_2_cafe = Url.append_param(url_example_2, "café", "du Monde")
53+
url_example_2_cafe_email = Url.append_param(url_example_2_cafe, "email", "[email protected]")
54+
Stdout.line!("URL with multiple appended params: ${Url.to_str(url_example_2_cafe_email)}")?
55+
# expects "https://example.com?caf%C3%A9=du%20Monde&email=hi%40example.com")?
56+
57+
# Test Url.has_query
58+
Stdout.line!("\nTesting Url.has_query:")?
59+
60+
url_with_query = Url.from_str("https://example.com?key=value#stuff")
61+
hasQuery1 = Url.has_query(url_with_query)
62+
Stdout.line!("URL with query has_query: ${Inspect.to_str(hasQuery1)}")?
63+
# expects Bool.true
64+
65+
url_hashtag = Url.from_str("https://example.com#stuff")
66+
hasQuery2 = Url.has_query(url_hashtag)
67+
Stdout.line!("URL without query has_query: ${Inspect.to_str(hasQuery2)}")?
68+
# expects Bool.false
69+
70+
Stdout.line!("\nTesting Url.has_fragment:")?
71+
72+
url_key_val_hashtag = Url.from_str("https://example.com?key=value#stuff")
73+
has_fragment = Url.has_fragment(url_key_val_hashtag)
74+
Stdout.line!("URL with fragment has_fragment: ${Inspect.to_str(has_fragment)}")?
75+
# expects Bool.true
76+
77+
url_key_val = Url.from_str("https://example.com?key=value")
78+
has_fragment_2 = Url.has_fragment(url_key_val)
79+
Stdout.line!("URL without fragment has_fragment: ${Inspect.to_str(has_fragment_2)}")?
80+
# expects Bool.false
81+
82+
Stdout.line!("\nTesting Url.query:")?
83+
84+
url_key_val_multi = Url.from_str("https://example.com?key1=val1&key2=val2&key3=val3#stuff")
85+
query = Url.query(url_key_val_multi)
86+
Stdout.line!("Query from URL: ${query}")?
87+
# expects "key1=val1&key2=val2&key3=val3"
88+
89+
url_no_query = Url.from_str("https://example.com#stuff")
90+
query_empty = Url.query(url_no_query)
91+
Stdout.line!("Query from URL without query: ${query_empty}")
92+
# expects ""
93+
94+
test_part_2! : {} => Result {} _
95+
test_part_2! = |{}|
96+
# Test Url.fragment
97+
Stdout.line!("\nTesting Url.fragment:")?
98+
99+
url_with_fragment = Url.from_str("https://example.com#stuff")
100+
fragment = Url.fragment(url_with_fragment)
101+
Stdout.line!("Fragment from URL: ${fragment}")?
102+
# expects "stuff"
103+
104+
url_no_fragment = Url.from_str("https://example.com")
105+
fragment_empty = Url.fragment(url_no_fragment)
106+
Stdout.line!("Fragment from URL without fragment: ${fragment_empty}")?
107+
# expects ""
108+
109+
# Test Url.reserve
110+
Stdout.line!("\nTesting Url.reserve:")?
111+
112+
url_to_reserve = Url.from_str("https://example.com")
113+
url_reserved = Url.reserve(url_to_reserve, 50)
114+
url_with_params = url_reserved
115+
|> Url.append("stuff")
116+
|> Url.append_param("café", "du Monde")
117+
|> Url.append_param("email", "[email protected]")
118+
119+
Stdout.line!("URL with reserved capacity and params: ${Url.to_str(url_with_params)}")?
120+
# expects "https://example.com/stuff?caf%C3%A9=du%20Monde&email=hi%40example.com"
121+
122+
# Test Url.with_query
123+
Stdout.line!("\nTesting Url.with_query:")?
124+
125+
url_replace_query = Url.from_str("https://example.com?key1=val1&key2=val2#stuff")
126+
url_with_new_query = Url.with_query(url_replace_query, "newQuery=thisRightHere")
127+
Stdout.line!("URL with replaced query: ${Url.to_str(url_with_new_query)}")?
128+
# expects "https://example.com?newQuery=thisRightHere#stuff"
129+
130+
url_remove_query = Url.from_str("https://example.com?key1=val1&key2=val2#stuff")
131+
url_with_empty_query = Url.with_query(url_remove_query, "")
132+
Stdout.line!("URL with removed query: ${Url.to_str(url_with_empty_query)}")?
133+
# expects "https://example.com#stuff"
134+
135+
# Test Url.with_fragment
136+
Stdout.line!("\nTesting Url.with_fragment:")?
137+
138+
url_replace_fragment = Url.from_str("https://example.com#stuff")
139+
url_with_new_fragment = Url.with_fragment(url_replace_fragment, "things")
140+
Stdout.line!("URL with replaced fragment: ${Url.to_str(url_with_new_fragment)}")?
141+
# expects "https://example.com#things"
142+
143+
url_add_fragment = Url.from_str("https://example.com")
144+
url_with_added_fragment = Url.with_fragment(url_add_fragment, "things")
145+
Stdout.line!("URL with added fragment: ${Url.to_str(url_with_added_fragment)}")?
146+
# expects "https://example.com#things"
147+
148+
url_remove_fragment = Url.from_str("https://example.com#stuff")
149+
url_with_empty_fragment = Url.with_fragment(url_remove_fragment, "")
150+
Stdout.line!("URL with removed fragment: ${Url.to_str(url_with_empty_fragment)}")?
151+
# expects "https://example.com"
152+
153+
# Test Url.query_params
154+
Stdout.line!("\nTesting Url.query_params:")?
155+
156+
url_with_many_params = Url.from_str("https://example.com?key1=val1&key2=val2&key3=val3")
157+
params_dict = Url.query_params(url_with_many_params)
158+
159+
# Check if params contains expected key-value pairs
160+
Stdout.line!("params_dict: ${Inspect.to_str(params_dict)}")?
161+
# expects Dict with key1=val1, key2=val2, key3=val3
162+
163+
# Test Url.path
164+
Stdout.line!("\nTesting Url.path:")?
165+
166+
url_with_path = Url.from_str("https://example.com/foo/bar?key1=val1&key2=val2#stuff")
167+
path = Url.path(url_with_path)
168+
Stdout.line!("Path from URL: ${path}")?
169+
# expects "example.com/foo/bar"
170+
171+
url_relative = Url.from_str("/foo/bar?key1=val1&key2=val2#stuff")
172+
path_relative = Url.path(url_relative)
173+
Stdout.line!("Path from relative URL: ${path_relative}")?
174+
# expects "/foo/bar"
175+
176+
Ok({})

0 commit comments

Comments
 (0)