diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2023-02-16 17:13:52 +0100 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2023-02-16 17:13:52 +0100 |
| commit | 7bd8586b2f9d1cf6aa68c88a1ce943e6e610e2b2 (patch) | |
| tree | 25be27cac2d405f43948cbff94530f66e4e08b50 /internal | |
| parent | 0981437192487a30e1ce9b2ed2b2a5fcd6812748 (diff) | |
HTTP: Add test for ensuring valid URL
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/http/http_test.go | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/internal/http/http_test.go b/internal/http/http_test.go new file mode 100644 index 0000000..371b4a8 --- /dev/null +++ b/internal/http/http_test.go @@ -0,0 +1,61 @@ +package http + +import ( + "testing" +) + +func TestEnsureValidURL(t *testing.T) { + _, validErr := EnsureValidURL("%notvalid%") + + if validErr == nil { + t.Fatal("Got nil error, want: non-nil") + } + + testCases := map[string]string{ + // Make sure we set https + "example.com/": "https://example.com/", + // Make sure we do override the scheme to https + "http://example.com/": "https://example.com/", + // This URL is already valid + "https://example.com/": "https://example.com/", + // Make sure to add a trailing slash (/) + "https://example.com": "https://example.com/", + // Cleanup the path 1 + "https://example.com/////": "https://example.com/", + // Cleanup the path 2 + "https://example.com/..": "https://example.com/", + } + + for k, v := range testCases { + valid, validErr := EnsureValidURL(k) + if validErr != nil { + t.Fatalf("Got: %v, want: nil", validErr) + } + if valid != v { + t.Fatalf("Got: %v, want: %v", valid, v) + } + } +} + +func Test_JoinURLPath(t *testing.T) { + cases := []struct { + u string + p string + want string + }{ + {u: "https://example.com", p: "test", want: "https://example.com/test/"}, + {u: "https://example.com", p: "/test", want: "https://example.com/test/"}, + {u: "https://example.com", p: "../test", want: "https://example.com/test/"}, + {u: "https://example.com", p: "../test/", want: "https://example.com/test/"}, + {u: "https://example.com", p: "test/", want: "https://example.com/test/"}, + } + for _, c := range cases { + got, err := JoinURLPath(c.u, c.p) + if err != nil { + t.Fatalf("Failed to parse join url case: %v, err: %v", c, err) + } + if got != c.want { + t.Fatalf("Failed test case for joining URL, want: %v, got: %v", c.want, got) + } + } +} |
