|
| 1 | +defmodule LiveDebugger.Utils.URLTest do |
| 2 | + @moduledoc false |
| 3 | + use ExUnit.Case, async: true |
| 4 | + |
| 5 | + alias LiveDebugger.Utils.URL |
| 6 | + |
| 7 | + describe "to_relative/1" do |
| 8 | + test "converts absolute URL to relative URL" do |
| 9 | + assert URL.to_relative("http://example.com/foo?bar=baz") == "/foo?bar=baz" |
| 10 | + end |
| 11 | + |
| 12 | + test "handles URLs without query parameters" do |
| 13 | + assert URL.to_relative("http://example.com/foo") == "/foo" |
| 14 | + end |
| 15 | + |
| 16 | + test "handles URLs with only query parameters" do |
| 17 | + assert URL.to_relative("http://example.com?bar=baz") == "?bar=baz" |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + describe "update_path/2" do |
| 22 | + test "updates the path of a URL" do |
| 23 | + assert URL.update_path("http://example.com/foo?bar=baz", "/new_path") == |
| 24 | + "http://example.com/new_path?bar=baz" |
| 25 | + end |
| 26 | + |
| 27 | + test "handles URLs without query parameters" do |
| 28 | + assert URL.update_path("http://example.com/foo", "/new_path") == |
| 29 | + "http://example.com/new_path" |
| 30 | + end |
| 31 | + |
| 32 | + test "adds path when no path exists" do |
| 33 | + assert URL.update_path("http://example.com", "/new_path") == |
| 34 | + "http://example.com/new_path" |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + describe "upsert_query_param/3" do |
| 39 | + test "adds a query parameter in a URL" do |
| 40 | + assert URL.upsert_query_param("http://example.com/foo", "key", "value") == |
| 41 | + "http://example.com/foo?key=value" |
| 42 | + end |
| 43 | + |
| 44 | + test "updates a query parameter in a URL with existing parameters" do |
| 45 | + assert URL.upsert_query_param("http://example.com/foo?key1=value1", "key2", "value2") == |
| 46 | + "http://example.com/foo?key1=value1&key2=value2" |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + describe "upsert_query_params/2" do |
| 51 | + test "adds multiple query parameters in a URL" do |
| 52 | + assert URL.upsert_query_params("http://example.com/foo", %{ |
| 53 | + "key1" => "value1", |
| 54 | + "key2" => "value2" |
| 55 | + }) == |
| 56 | + "http://example.com/foo?key1=value1&key2=value2" |
| 57 | + end |
| 58 | + |
| 59 | + test "updates existing query parameters in a URL" do |
| 60 | + assert URL.upsert_query_params("http://example.com/foo?key1=value1", %{ |
| 61 | + "key2" => "value2", |
| 62 | + "key3" => "value3" |
| 63 | + }) == |
| 64 | + "http://example.com/foo?key1=value1&key2=value2&key3=value3" |
| 65 | + end |
| 66 | + |
| 67 | + test "doesn't modify the URL if no parameters are provided" do |
| 68 | + assert URL.upsert_query_params("http://example.com/foo", %{}) == |
| 69 | + "http://example.com/foo" |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + describe "remove_query_param/2" do |
| 74 | + test "removes a query parameter from a URL" do |
| 75 | + assert URL.remove_query_param("http://example.com/foo?key=value", "key") == |
| 76 | + "http://example.com/foo" |
| 77 | + end |
| 78 | + |
| 79 | + test "doesn't modify the URL if the parameter doesn't exist" do |
| 80 | + assert URL.remove_query_param("http://example.com/foo?key=value", "nonexistent") == |
| 81 | + "http://example.com/foo?key=value" |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + describe "remove_query_params/2" do |
| 86 | + test "removes multiple query parameters from a URL" do |
| 87 | + assert URL.remove_query_params("http://example.com/foo?key1=value1&key2=value2", [ |
| 88 | + "key1", |
| 89 | + "key2" |
| 90 | + ]) == |
| 91 | + "http://example.com/foo" |
| 92 | + end |
| 93 | + |
| 94 | + test "doesn't modify the URL if no parameters exist" do |
| 95 | + assert URL.remove_query_params("http://example.com/foo?key=value", ["nonexistent"]) == |
| 96 | + "http://example.com/foo?key=value" |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + describe "modify_query_params/2" do |
| 101 | + test "modifies query parameters in a URL using a function" do |
| 102 | + assert URL.modify_query_params("http://example.com/foo?key=value", fn params -> |
| 103 | + Map.put(params, "new_key", "new_value") |
| 104 | + end) == "http://example.com/foo?key=value&new_key=new_value" |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments