|
| 1 | +package restclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/url" |
| 6 | + "reflect" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestBuildPath_Basic(t *testing.T) { |
| 11 | + baseUrl := "https://api.example.com/v1" |
| 12 | + path := "/users/{userId}/posts/{postId}" |
| 13 | + parameters := map[string]string{ |
| 14 | + "userId": "42", |
| 15 | + "postId": "99", |
| 16 | + } |
| 17 | + query := map[string]string{ |
| 18 | + "sort": "desc", |
| 19 | + "page": "2", |
| 20 | + } |
| 21 | + |
| 22 | + got := buildPath(baseUrl, path, parameters, query) |
| 23 | + if got == nil { |
| 24 | + t.Fatalf("buildPath returned nil") |
| 25 | + } |
| 26 | + |
| 27 | + got, err := url.Parse(got.String()) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("failed to parse base URL: %v", err) |
| 30 | + } |
| 31 | + |
| 32 | + expectedPath := "/v1/users/42/posts/99" |
| 33 | + if got.Path != "/v1/users/42/posts/99" { |
| 34 | + t.Errorf("expected path %q, got %q", expectedPath, got.Path) |
| 35 | + } |
| 36 | + |
| 37 | + wantQuery := url.Values{"sort": {"desc"}, "page": {"2"}}.Encode() |
| 38 | + if got.RawQuery != wantQuery && got.RawQuery != "page=2&sort=desc" { |
| 39 | + t.Errorf("expected query %q, got %q", wantQuery, got.RawQuery) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestBuildPath_WithPathParamsWithSlashes(t *testing.T) { |
| 44 | + baseUrl := "https://api.example.com/v1" |
| 45 | + path := "/users/{userId}/posts/{postId}" |
| 46 | + parameters := map[string]string{ |
| 47 | + "userId": "42/123", |
| 48 | + "postId": "99", |
| 49 | + } |
| 50 | + query := map[string]string{} |
| 51 | + |
| 52 | + got := buildPath(baseUrl, path, parameters, query) |
| 53 | + if got == nil { |
| 54 | + t.Fatalf("buildPath returned nil") |
| 55 | + } |
| 56 | + |
| 57 | + fmt.Println("Got Path:", got.RawQuery) |
| 58 | + |
| 59 | + wantPath := baseUrl + "/users/42%2F123/posts/99" |
| 60 | + if got.String() != wantPath { |
| 61 | + t.Errorf("expected path %q, got %q", wantPath, got.String()) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestBuildPath_NoParams(t *testing.T) { |
| 66 | + baseUrl := "https://api.example.com" |
| 67 | + path := "/status" |
| 68 | + parameters := map[string]string{} |
| 69 | + query := map[string]string{} |
| 70 | + |
| 71 | + got := buildPath(baseUrl, path, parameters, query) |
| 72 | + if got == nil { |
| 73 | + t.Fatalf("buildPath returned nil") |
| 74 | + } |
| 75 | + got, err := url.Parse(got.String()) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("failed to parse base URL: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + wantPath := "/status" |
| 81 | + if got.Path != wantPath { |
| 82 | + t.Errorf("expected path %q, got %q", wantPath, got.Path) |
| 83 | + } |
| 84 | + if got.RawQuery != "" { |
| 85 | + t.Errorf("expected empty query, got %q", got.RawQuery) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestBuildPath_InvalidBaseURL(t *testing.T) { |
| 90 | + baseUrl := "://bad-url" |
| 91 | + path := "/foo" |
| 92 | + parameters := map[string]string{} |
| 93 | + query := map[string]string{} |
| 94 | + |
| 95 | + got := buildPath(baseUrl, path, parameters, query) |
| 96 | + if got != nil { |
| 97 | + t.Errorf("expected nil for invalid baseUrl, got %v", got) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestBuildPath_MultipleQueryParams(t *testing.T) { |
| 102 | + baseUrl := "https://api.example.com" |
| 103 | + path := "/search" |
| 104 | + parameters := map[string]string{} |
| 105 | + query := map[string]string{ |
| 106 | + "q": "golang", |
| 107 | + "lang": "en", |
| 108 | + } |
| 109 | + |
| 110 | + got := buildPath(baseUrl, path, parameters, query) |
| 111 | + if got == nil { |
| 112 | + t.Fatalf("buildPath returned nil") |
| 113 | + } |
| 114 | + |
| 115 | + got, err := url.Parse(got.String()) |
| 116 | + if err != nil { |
| 117 | + t.Fatalf("failed to parse base URL: %v", err) |
| 118 | + } |
| 119 | + |
| 120 | + wantPath := "/search" |
| 121 | + if got.Path != wantPath { |
| 122 | + t.Errorf("expected path %q, got %q", wantPath, got.Path) |
| 123 | + } |
| 124 | + |
| 125 | + parsedQuery, _ := url.ParseQuery(got.RawQuery) |
| 126 | + expectedQuery := url.Values{"q": {"golang"}, "lang": {"en"}} |
| 127 | + if !reflect.DeepEqual(parsedQuery, expectedQuery) { |
| 128 | + t.Errorf("expected query %v, got %v", expectedQuery, parsedQuery) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func TestBuildPath_PathWithNoLeadingSlash(t *testing.T) { |
| 133 | + baseUrl := "https://api.example.com/api" |
| 134 | + path := "foo/bar" |
| 135 | + parameters := map[string]string{} |
| 136 | + query := map[string]string{} |
| 137 | + |
| 138 | + got := buildPath(baseUrl, path, parameters, query) |
| 139 | + if got == nil { |
| 140 | + t.Fatalf("buildPath returned nil") |
| 141 | + } |
| 142 | + |
| 143 | + got, err := url.Parse(got.String()) |
| 144 | + if err != nil { |
| 145 | + t.Fatalf("failed to parse base URL: %v", err) |
| 146 | + } |
| 147 | + |
| 148 | + wantPath := "/api/foo/bar" |
| 149 | + if got.Path != wantPath { |
| 150 | + t.Errorf("expected path %q, got %q", wantPath, got.Path) |
| 151 | + } |
| 152 | +} |
0 commit comments