github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/web/webutil/host_test.go (about)

     1  // Copyright 2018 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache-2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  package webutil_test
     6  
     7  import (
     8  	"fmt"
     9  	"net"
    10  	"path/filepath"
    11  	"runtime"
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/Schaudge/grailbase/web/webutil"
    16  )
    17  
    18  func testPort(t *testing.T, protocol, defaultDomain, defaultPort string) {
    19  	expectedDefaultDomain := strings.TrimPrefix(defaultDomain, ".")
    20  	ad := func(a string) string {
    21  		if len(expectedDefaultDomain) > 0 {
    22  			return a + "." + expectedDefaultDomain
    23  		}
    24  		return a
    25  	}
    26  
    27  	ap := func(a string) string {
    28  		if len(defaultPort) > 0 {
    29  			return net.JoinHostPort(a, defaultPort)
    30  		}
    31  		return a
    32  	}
    33  
    34  	_, file, line, _ := runtime.Caller(1)
    35  	loc := fmt.Sprintf("%s:%d", filepath.Base(file), line)
    36  	for i, c := range []struct {
    37  		srv, fqhp, fqh, hst, dmn, prt string
    38  		err                           bool
    39  	}{
    40  		{"2001:db8::68", ap("2001:db8::68"), "2001:db8::68", "2001:db8::68", "", defaultPort, false},
    41  		{"10.88.224.99", ap("10.88.224.99"), "10.88.224.99", "10.88.224.99", "", defaultPort, false},
    42  		{"www", ap(ad("www")), ad("www"), "www", expectedDefaultDomain, defaultPort, false},
    43  		{"www.b", ap("www.b"), "www.b", "www", "b", defaultPort, false},
    44  		{"2001:db8::68[[", "", "", "", "", "", true},
    45  	} {
    46  		srv := protocol + c.srv
    47  		ptcl, fqhp, fqh, hst, dmn, prt, err := webutil.CanonicalizeHost(srv, defaultDomain, defaultPort)
    48  		testcase := fmt.Sprintf("%s:%d (%q, %q, %q:", loc, i, srv, defaultDomain, defaultPort)
    49  		if got, want := c.err, (err != nil); got != want {
    50  			t.Errorf("%s: got %v, want %v", testcase, got, want)
    51  		}
    52  		if got, want := ptcl, strings.TrimSuffix(protocol, "//"); got != want {
    53  			t.Errorf("%s: got %v, want %v", testcase, got, want)
    54  		}
    55  		if got, want := fqhp, c.fqhp; got != want {
    56  			t.Errorf("%s: got %v, want %v", testcase, got, want)
    57  		}
    58  		if got, want := fqh, c.fqh; got != want {
    59  			t.Errorf("%s: got %v, want %v", testcase, got, want)
    60  		}
    61  		if got, want := hst, c.hst; got != want {
    62  			t.Errorf("%s: got %v, want %v", testcase, got, want)
    63  		}
    64  		if got, want := dmn, c.dmn; got != want {
    65  			t.Errorf("%s: got %v, want %v", testcase, got, want)
    66  		}
    67  		if got, want := prt, c.prt; got != want {
    68  			t.Errorf("%s: got %v, want %v", testcase, got, want)
    69  		}
    70  	}
    71  }
    72  
    73  func TestCanonicalizeHost(t *testing.T) {
    74  	for _, protocol := range []string{"", "https://", "http://", "foo://", "//"} {
    75  		for _, defaultDomain := range []string{"", "x", "x.com", ".", ".x.com"} {
    76  			for _, defaultPort := range []string{"", ":22", "81"} {
    77  				testPort(t, protocol, defaultDomain, defaultPort)
    78  			}
    79  		}
    80  	}
    81  }