github.com/jameswoolfenden/terraform@v0.11.12-beta1/svchost/svchost_test.go (about)

     1  package svchost
     2  
     3  import "testing"
     4  
     5  func TestForDisplay(t *testing.T) {
     6  	tests := []struct {
     7  		Input string
     8  		Want  string
     9  	}{
    10  		{
    11  			"",
    12  			"",
    13  		},
    14  		{
    15  			"example.com",
    16  			"example.com",
    17  		},
    18  		{
    19  			"invalid",
    20  			"invalid",
    21  		},
    22  		{
    23  			"localhost",
    24  			"localhost",
    25  		},
    26  		{
    27  			"localhost:1211",
    28  			"localhost:1211",
    29  		},
    30  		{
    31  			"HashiCorp.com",
    32  			"hashicorp.com",
    33  		},
    34  		{
    35  			"Испытание.com",
    36  			"испытание.com",
    37  		},
    38  		{
    39  			"münchen.de", // this is a precomposed u with diaeresis
    40  			"münchen.de", // this is a precomposed u with diaeresis
    41  		},
    42  		{
    43  			"münchen.de", // this is a separate u and combining diaeresis
    44  			"münchen.de",  // this is a precomposed u with diaeresis
    45  		},
    46  		{
    47  			"example.com:443",
    48  			"example.com",
    49  		},
    50  		{
    51  			"example.com:81",
    52  			"example.com:81",
    53  		},
    54  		{
    55  			"example.com:boo",
    56  			"example.com:boo", // invalid, but tolerated for display purposes
    57  		},
    58  		{
    59  			"example.com:boo:boo",
    60  			"example.com:boo:boo", // invalid, but tolerated for display purposes
    61  		},
    62  		{
    63  			"example.com:081",
    64  			"example.com:81",
    65  		},
    66  	}
    67  
    68  	for _, test := range tests {
    69  		t.Run(test.Input, func(t *testing.T) {
    70  			got := ForDisplay(test.Input)
    71  			if got != test.Want {
    72  				t.Errorf("wrong result\ninput: %s\ngot:   %s\nwant:  %s", test.Input, got, test.Want)
    73  			}
    74  		})
    75  	}
    76  }
    77  
    78  func TestForComparison(t *testing.T) {
    79  	tests := []struct {
    80  		Input string
    81  		Want  string
    82  		Err   bool
    83  	}{
    84  		{
    85  			"",
    86  			"",
    87  			true,
    88  		},
    89  		{
    90  			"example.com",
    91  			"example.com",
    92  			false,
    93  		},
    94  		{
    95  			"example.com:443",
    96  			"example.com",
    97  			false,
    98  		},
    99  		{
   100  			"example.com:81",
   101  			"example.com:81",
   102  			false,
   103  		},
   104  		{
   105  			"example.com:081",
   106  			"example.com:81",
   107  			false,
   108  		},
   109  		{
   110  			"invalid",
   111  			"invalid",
   112  			false, // the "invalid" TLD is, confusingly, a valid hostname syntactically
   113  		},
   114  		{
   115  			"localhost", // supported for local testing only
   116  			"localhost",
   117  			false,
   118  		},
   119  		{
   120  			"localhost:1211", // supported for local testing only
   121  			"localhost:1211",
   122  			false,
   123  		},
   124  		{
   125  			"HashiCorp.com",
   126  			"hashicorp.com",
   127  			false,
   128  		},
   129  		{
   130  			"Испытание.com",
   131  			"xn--80akhbyknj4f.com",
   132  			false,
   133  		},
   134  		{
   135  			"münchen.de", // this is a precomposed u with diaeresis
   136  			"xn--mnchen-3ya.de",
   137  			false,
   138  		},
   139  		{
   140  			"münchen.de", // this is a separate u and combining diaeresis
   141  			"xn--mnchen-3ya.de",
   142  			false,
   143  		},
   144  		{
   145  			"blah..blah",
   146  			"",
   147  			true,
   148  		},
   149  		{
   150  			"example.com:boo",
   151  			"",
   152  			true,
   153  		},
   154  		{
   155  			"example.com:80:boo",
   156  			"",
   157  			true,
   158  		},
   159  	}
   160  
   161  	for _, test := range tests {
   162  		t.Run(test.Input, func(t *testing.T) {
   163  			got, err := ForComparison(test.Input)
   164  			if (err != nil) != test.Err {
   165  				if test.Err {
   166  					t.Error("unexpected success; want error")
   167  				} else {
   168  					t.Errorf("unexpected error; want success\nerror: %s", err)
   169  				}
   170  			}
   171  			if string(got) != test.Want {
   172  				t.Errorf("wrong result\ninput: %s\ngot:   %s\nwant:  %s", test.Input, got, test.Want)
   173  			}
   174  		})
   175  	}
   176  }
   177  
   178  func TestHostnameForDisplay(t *testing.T) {
   179  	tests := []struct {
   180  		Input string
   181  		Want  string
   182  	}{
   183  		{
   184  			"example.com",
   185  			"example.com",
   186  		},
   187  		{
   188  			"example.com:81",
   189  			"example.com:81",
   190  		},
   191  		{
   192  			"xn--80akhbyknj4f.com",
   193  			"испытание.com",
   194  		},
   195  		{
   196  			"xn--80akhbyknj4f.com:8080",
   197  			"испытание.com:8080",
   198  		},
   199  		{
   200  			"xn--mnchen-3ya.de",
   201  			"münchen.de", // this is a precomposed u with diaeresis
   202  		},
   203  	}
   204  
   205  	for _, test := range tests {
   206  		t.Run(test.Input, func(t *testing.T) {
   207  			got := Hostname(test.Input).ForDisplay()
   208  			if got != test.Want {
   209  				t.Errorf("wrong result\ninput: %s\ngot:   %s\nwant:  %s", test.Input, got, test.Want)
   210  			}
   211  		})
   212  	}
   213  }