package main import ( "slices" "testing" ) func TestNormaliseURL(t *testing.T) { t.Parallel() wantNormalisedURL := "blog.boot.dev/path" cases := []struct { name string inputURL string }{ { name: "remove HTTPS scheme", inputURL: "https://blog.boot.dev/path", }, { name: "remove HTTP scheme", inputURL: "http://blog.boot.dev/path", }, { name: "remove HTTPS scheme with a trailing slash", inputURL: "https://blog.boot.dev/path/", }, { name: "remove HTTP scheme with a trailing slash", inputURL: "http://blog.boot.dev/path/", }, { name: "remove HTTPS scheme with port 443", inputURL: "https://blog.boot.dev:443/path", }, { name: "remove HTTP scheme with port 80", inputURL: "http://blog.boot.dev:80/path", }, { name: "normalised URL", inputURL: "blog.boot.dev/path", }, } for ind, tc := range slices.All(cases) { t.Run(tc.name, func(t *testing.T) { t.Parallel() got, err := normaliseURL(tc.inputURL) if err != nil { t.Fatalf( "Test %d - '%s' FAILED: unexpected error: %v", ind, tc.name, err, ) } if got != wantNormalisedURL { t.Errorf( "Test %d - %s FAILED: unexpected normalised URL returned: want %s, got %s", ind, tc.name, wantNormalisedURL, got, ) } else { t.Logf( "Test %d - %s PASSED: expected normalised URL returned: got %s", ind, tc.name, got, ) } }) } } func TestEqualDomains(t *testing.T) { t.Parallel() cases := []struct { name string urlA string urlB string want bool }{ { name: "Same domain, different paths", urlA: "https://example.com/news", urlB: "https://example.com/about/contact", want: true, }, { name: "Different domains, same path", urlA: "http://example.com/blog", urlB: "http://example.org/blog", want: false, }, { name: "Same domain, different protocols", urlA: "http://code.person.me.uk/projects/orion", urlB: "https://code.person.me.uk/user/person/README.md", want: true, }, } for ind, tc := range slices.All(cases) { t.Run(tc.name, testEqualDomains(ind+1, tc.name, tc.urlA, tc.urlB, tc.want)) } } func testEqualDomains(testNum int, testName, urlA, urlB string, want bool) func(t *testing.T) { return func(t *testing.T) { t.Parallel() got, err := equalDomains(urlA, urlB) if err != nil { t.Fatalf( "Test %d - '%s' FAILED: unexpected error: %v", testNum, testName, err, ) } if got != want { t.Errorf( "Test %d - '%s' FAILED: unexpected domain comparison received: want %t, got %t", testNum, testName, want, got, ) } else { t.Logf( "Test %d - '%s' PASSED: expected domain comparison received: got %t", testNum, testName, got, ) } } }