github.com/aavshr/aws-sdk-go@v1.41.3/private/protocol/host_test.go (about)

     1  //go:build go1.7
     2  // +build go1.7
     3  
     4  package protocol
     5  
     6  import (
     7  	"strconv"
     8  	"testing"
     9  )
    10  
    11  func TestValidPortNumber(t *testing.T) {
    12  	cases := []struct {
    13  		Input string
    14  		Valid bool
    15  	}{
    16  		{Input: "123", Valid: true},
    17  		{Input: "123.0", Valid: false},
    18  		{Input: "-123", Valid: false},
    19  		{Input: "65536", Valid: false},
    20  		{Input: "0", Valid: true},
    21  	}
    22  	for i, c := range cases {
    23  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    24  			valid := ValidPortNumber(c.Input)
    25  			if e, a := c.Valid, valid; e != a {
    26  				t.Errorf("expect valid %v, got %v", e, a)
    27  			}
    28  		})
    29  	}
    30  
    31  }
    32  
    33  func TestValidHostLabel(t *testing.T) {
    34  	cases := []struct {
    35  		Input string
    36  		Valid bool
    37  	}{
    38  		{Input: "abc123", Valid: true},
    39  		{Input: "123", Valid: true},
    40  		{Input: "abc", Valid: true},
    41  		{Input: "123-abc", Valid: true},
    42  		{Input: "{thing}-abc", Valid: false},
    43  		{Input: "abc.123", Valid: false},
    44  		{Input: "abc/123", Valid: false},
    45  		{Input: "012345678901234567890123456789012345678901234567890123456789123", Valid: true},
    46  		{Input: "0123456789012345678901234567890123456789012345678901234567891234", Valid: false},
    47  		{Input: "", Valid: false},
    48  	}
    49  
    50  	for i, c := range cases {
    51  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    52  			valid := ValidHostLabel(c.Input)
    53  			if e, a := c.Valid, valid; e != a {
    54  				t.Errorf("expect valid %v, got %v", e, a)
    55  			}
    56  		})
    57  	}
    58  }
    59  
    60  func TestValidateEndpointHostHandler(t *testing.T) {
    61  	cases := map[string]struct {
    62  		Input string
    63  		Valid bool
    64  	}{
    65  		"valid host":  {Input: "abc.123", Valid: true},
    66  		"fqdn host":   {Input: "abc.123.", Valid: true},
    67  		"empty label": {Input: "abc..", Valid: false},
    68  		"max host len": {
    69  			Input: "123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.12345",
    70  			Valid: true,
    71  		},
    72  		"too long host": {
    73  			Input: "123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456",
    74  			Valid: false,
    75  		},
    76  		"valid host with port number":         {Input: "abd.123:1234", Valid: true},
    77  		"valid host with invalid port number": {Input: "abc.123:99999", Valid: false},
    78  		"empty host with port number":         {Input: ":1234", Valid: false},
    79  		"valid host with empty port number":   {Input: "abc.123:", Valid: false},
    80  	}
    81  
    82  	for name, c := range cases {
    83  		t.Run(name, func(t *testing.T) {
    84  			err := ValidateEndpointHost("OpName", c.Input)
    85  			if e, a := c.Valid, err == nil; e != a {
    86  				t.Errorf("expect valid %v, got %v, %v", e, a, err)
    87  			}
    88  		})
    89  	}
    90  }