github.com/Hashicorp/terraform@v0.11.12-beta1/registry/regsrc/friendly_host_test.go (about)

     1  package regsrc
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestFriendlyHost(t *testing.T) {
     9  	tests := []struct {
    10  		name        string
    11  		source      string
    12  		wantHost    string
    13  		wantDisplay string
    14  		wantNorm    string
    15  		wantValid   bool
    16  	}{
    17  		{
    18  			name:        "simple ascii",
    19  			source:      "registry.terraform.io",
    20  			wantHost:    "registry.terraform.io",
    21  			wantDisplay: "registry.terraform.io",
    22  			wantNorm:    "registry.terraform.io",
    23  			wantValid:   true,
    24  		},
    25  		{
    26  			name:        "mixed-case ascii",
    27  			source:      "Registry.TerraForm.io",
    28  			wantHost:    "Registry.TerraForm.io",
    29  			wantDisplay: "registry.terraform.io", // Display case folded
    30  			wantNorm:    "registry.terraform.io",
    31  			wantValid:   true,
    32  		},
    33  		{
    34  			name:        "IDN",
    35  			source:      "ʎɹʇsıƃǝɹ.ɯɹoɟɐɹɹǝʇ.io",
    36  			wantHost:    "ʎɹʇsıƃǝɹ.ɯɹoɟɐɹɹǝʇ.io",
    37  			wantDisplay: "ʎɹʇsıƃǝɹ.ɯɹoɟɐɹɹǝʇ.io",
    38  			wantNorm:    "xn--s-fka0wmm0zea7g8b.xn--o-8ta85a3b1dwcda1k.io",
    39  			wantValid:   true,
    40  		},
    41  		{
    42  			name:        "IDN TLD",
    43  			source:      "zhongwen.中国",
    44  			wantHost:    "zhongwen.中国",
    45  			wantDisplay: "zhongwen.中国",
    46  			wantNorm:    "zhongwen.xn--fiqs8s",
    47  			wantValid:   true,
    48  		},
    49  		{
    50  			name:        "IDN Case Folding",
    51  			source:      "Испытание.com",
    52  			wantHost:    "Испытание.com", // Raw input retains case
    53  			wantDisplay: "испытание.com", // Display form is unicode but case-folded
    54  			wantNorm:    "xn--80akhbyknj4f.com",
    55  			wantValid:   true,
    56  		},
    57  		{
    58  			name:        "Punycode is invalid as an input format",
    59  			source:      "xn--s-fka0wmm0zea7g8b.xn--o-8ta85a3b1dwcda1k.io",
    60  			wantHost:    "xn--s-fka0wmm0zea7g8b.xn--o-8ta85a3b1dwcda1k.io",
    61  			wantDisplay: "ʎɹʇsıƃǝɹ.ɯɹoɟɐɹɹǝʇ.io",
    62  			wantNorm:    InvalidHostString,
    63  			wantValid:   false,
    64  		},
    65  		{
    66  			name:        "non-host prefix is left alone",
    67  			source:      "foo/bar/baz",
    68  			wantHost:    "",
    69  			wantDisplay: "",
    70  			wantNorm:    "",
    71  			wantValid:   false,
    72  		},
    73  	}
    74  	for _, tt := range tests {
    75  		// Matrix each test with prefix and total match variants
    76  		for _, sfx := range []string{"", "/", "/foo/bar/baz"} {
    77  			t.Run(tt.name+" suffix:"+sfx, func(t *testing.T) {
    78  				gotHost, gotRest := ParseFriendlyHost(tt.source + sfx)
    79  
    80  				if gotHost == nil {
    81  					if tt.wantHost != "" {
    82  						t.Fatalf("ParseFriendlyHost() gotHost = nil, want %v", tt.wantHost)
    83  					}
    84  					// If we return nil host, the whole input string should be in rest
    85  					if gotRest != (tt.source + sfx) {
    86  						t.Fatalf("ParseFriendlyHost() was nil rest = %s, want %v", gotRest, tt.source+sfx)
    87  					}
    88  					return
    89  				}
    90  
    91  				if tt.wantHost == "" {
    92  					t.Fatalf("ParseFriendlyHost() gotHost.Raw = %v, want nil", gotHost.Raw)
    93  				}
    94  
    95  				if v := gotHost.String(); v != tt.wantHost {
    96  					t.Fatalf("String() = %v, want %v", v, tt.wantHost)
    97  				}
    98  				if v := gotHost.Display(); v != tt.wantDisplay {
    99  					t.Fatalf("Display() = %v, want %v", v, tt.wantDisplay)
   100  				}
   101  				if v := gotHost.Normalized(); v != tt.wantNorm {
   102  					t.Fatalf("Normalized() = %v, want %v", v, tt.wantNorm)
   103  				}
   104  				if v := gotHost.Valid(); v != tt.wantValid {
   105  					t.Fatalf("Valid() = %v, want %v", v, tt.wantValid)
   106  				}
   107  				if gotRest != strings.TrimLeft(sfx, "/") {
   108  					t.Fatalf("ParseFriendlyHost() rest = %v, want %v", gotRest, strings.TrimLeft(sfx, "/"))
   109  				}
   110  
   111  				// Also verify that host compares equal with all the variants.
   112  				if gotHost.Valid() && !gotHost.Equal(&FriendlyHost{Raw: tt.wantDisplay}) {
   113  					t.Fatalf("Equal() should be true for %s and %s", tt.wantHost, tt.wantDisplay)
   114  				}
   115  			})
   116  		}
   117  	}
   118  }
   119  
   120  func TestInvalidHostEquals(t *testing.T) {
   121  	invalid := NewFriendlyHost("NOT_A_HOST_NAME")
   122  	valid := PublicRegistryHost
   123  
   124  	// invalid hosts are not comparable
   125  	if invalid.Equal(invalid) {
   126  		t.Fatal("invalid host names are not comparable")
   127  	}
   128  
   129  	if valid.Equal(invalid) {
   130  		t.Fatalf("%q is not equal to %q", valid, invalid)
   131  	}
   132  
   133  	puny := NewFriendlyHost("xn--s-fka0wmm0zea7g8b.xn--o-8ta85a3b1dwcda1k.io")
   134  	display := NewFriendlyHost("ʎɹʇsıƃǝɹ.ɯɹoɟɐɹɹǝʇ.io")
   135  
   136  	// The pre-normalized host is not a valid source, and therefore not
   137  	// comparable to the display version.
   138  	if display.Equal(puny) {
   139  		t.Fatalf("invalid host %q should not be comparable", puny)
   140  	}
   141  }