package webui import "testing" // TestParseOriginPutsTheOriginInTheBrowserForm keeps what is not an origin // refused, and the named regressions of the generated comparison in // origin_property_test.go. func TestParseOriginPutsTheOriginInTheBrowserForm(t *testing.T) { for _, test := range []struct { name string text string want string wantErr bool }{ // A browser reads the port as a number: https://h:0443 is the origin // https://h, and http://h:080 is http://h. {name: "leading zero default port", text: "https://albedo.example.ts.net:0443", want: "https://albedo.example.ts.net"}, {name: "leading zero other port", text: "http://albedo.example.ts.net:08080", want: "http://albedo.example.ts.net:8080"}, {name: "port out of range", text: "https://albedo.example.ts.net:65536", wantErr: true}, {name: "empty", text: "", wantErr: true}, {name: "no scheme", text: "albedo.example.ts.net", wantErr: true}, {name: "no host", text: "https://", wantErr: true}, {name: "path", text: "https://albedo.example.ts.net/app", wantErr: true}, {name: "query", text: "https://albedo.example.ts.net/?key=abc", wantErr: true}, {name: "fragment", text: "https://albedo.example.ts.net/#top", wantErr: true}, {name: "other scheme", text: "ftp://albedo.example.ts.net", wantErr: true}, {name: "credentials", text: "https://user@albedo.example.ts.net", wantErr: true}, } { t.Run(test.name, func(t *testing.T) { parsed, err := ParseOrigin(test.text) if test.wantErr { if err == nil { t.Errorf("ParseOrigin(%q) = %q, want an error", test.text, parsed) } return } if err != nil { t.Fatalf("ParseOrigin(%q): %v", test.text, err) } if got := parsed.String(); got != test.want { t.Errorf("ParseOrigin(%q) = %q, want %q", test.text, got, test.want) } }) } } // TestOriginHostsTakeTheBrowsersIPv6Form checks every spelling of an IPv6 // address against the form the URL Standard serializes: the longest run of two // or more zero pieces compressed (the first on a tie), lower-case hex without // leading zeros, and an embedded IPv4 address written as hex pieces. func TestOriginHostsTakeTheBrowsersIPv6Form(t *testing.T) { for written, want := range map[string]string{ "[::1]": "[::1]", "[0:0::1]": "[::1]", "[0:0:0:0:0:0:0:0]": "[::]", "[2001:DB8:0:0:1:0:0:1]": "[2001:db8::1:0:0:1]", "[2001:db8:0:1:1:1:1:1]": "[2001:db8:0:1:1:1:1:1]", "[::ffff:127.0.0.1]": "[::ffff:7f00:1]", "[::127.0.0.1]": "[::7f00:1]", "[fe80:0:0:0:0:0:0:abcd]": "[fe80::abcd]", } { parsed, err := ParseOrigin("http://" + written) if err != nil { t.Fatalf("ParseOrigin(%q): %v", written, err) } if parsed.Host != want { t.Errorf("ParseOrigin(%q).Host = %q, want %q", written, parsed.Host, want) } } }