|
| 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