Skip to content

Commit

Permalink
url check test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nothub committed Feb 10, 2024
1 parent 801ed5b commit f2adcc0
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions web/url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package web

import "testing"

func TestIsValidHttpUrl(t *testing.T) {
type testCase struct {
url string
want bool
}

cases := []testCase{
{url: "http://example.org", want: true},
{url: "http://example.org/path/to/page", want: true},
{url: "http://example.org:8080", want: true},
{url: "http://example.org:8080/path/to/page", want: true},

{url: "https://example.org", want: true},
{url: "https://example.org/foo/bar", want: true},
{url: "https://example.org/foo/bar?x=y", want: true},
{url: "https://example.org:8080", want: true},
{url: "https://example.org:8080/path/to/page", want: true},
{url: "https://example.org\n", want: false},

{url: "example.org", want: false},

{url: "ftp://example.org", want: false},

{url: "relative/unix/path", want: false},
{url: "relative/unix/path/", want: false},
{url: "relative/unix/path/file.txt", want: false},

{url: "relative\\windows\\path", want: false},
{url: "relative\\windows\\path\\", want: false},
{url: "relative\\windows\\path\\file.txt", want: false},

{url: "/absolute/unix/path", want: false},
{url: "/absolute/unix/path/", want: false},
{url: "/absolute/unix/path/file.txt", want: false},

{url: "C:\\absolute\\windows\\path", want: false},
{url: "C:\\absolute\\windows\\path\\", want: false},
{url: "C:\\absolute\\windows\\path\\file.txt", want: false},
}

for _, tc := range cases {
t.Run(tc.url, func(t *testing.T) {
if got := IsValidHttpUrl(tc.url); got != tc.want {
t.Errorf("IsValidUrl() = %v, want %v", got, tc.want)
}
})
}
}

0 comments on commit f2adcc0

Please sign in to comment.