...

Source file src/github.com/aws/smithy-go/endpoints/private/rulesfn/uri_test.go

Documentation: github.com/aws/smithy-go/endpoints/private/rulesfn

     1  package rulesfn
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestURIEncode(t *testing.T) {
     8  	cases := map[string]struct {
     9  		input  string
    10  		expect string
    11  	}{
    12  		"no encoding": {
    13  			input:  "a-zA-Z0-9-_.~",
    14  			expect: "a-zA-Z0-9-_.~",
    15  		},
    16  		"with encoding": {
    17  			input:  "🐛 becomes 🦋",
    18  			expect: "%F0%9F%90%9B%20becomes%20%F0%9F%A6%8B",
    19  		},
    20  	}
    21  
    22  	for name, c := range cases {
    23  		t.Run(name, func(t *testing.T) {
    24  			actual := URIEncode(c.input)
    25  			if e, a := c.expect, actual; e != a {
    26  				t.Errorf("expect `%v` encoding, got `%v`", e, a)
    27  			}
    28  		})
    29  	}
    30  }
    31  
    32  func TestParseURL(t *testing.T) {
    33  	cases := map[string]struct {
    34  		input  string
    35  		expect *URL
    36  	}{
    37  		"https hostname with no path": {
    38  			input: "https://example.com",
    39  			expect: &URL{
    40  				Scheme:         "https",
    41  				Authority:      "example.com",
    42  				Path:           "",
    43  				NormalizedPath: "/",
    44  			},
    45  		},
    46  		"http hostname with no path": {
    47  			input: "http://example.com",
    48  			expect: &URL{
    49  				Scheme:         "http",
    50  				Authority:      "example.com",
    51  				Path:           "",
    52  				NormalizedPath: "/",
    53  			},
    54  		},
    55  		"https hostname with port with path": {
    56  			input: "https://example.com:80/foo/bar",
    57  			expect: &URL{
    58  				Scheme:         "https",
    59  				Authority:      "example.com:80",
    60  				Path:           "/foo/bar",
    61  				NormalizedPath: "/foo/bar/",
    62  			},
    63  		},
    64  		"invalid port": {
    65  			input:  "https://example.com:abc",
    66  			expect: nil,
    67  		},
    68  		"with query": {
    69  			input:  "https://example.com:8443?foo=bar&faz=baz",
    70  			expect: nil,
    71  		},
    72  		"ip4 URL": {
    73  			input: "https://127.0.0.1",
    74  			expect: &URL{
    75  				Scheme:         "https",
    76  				Authority:      "127.0.0.1",
    77  				Path:           "",
    78  				NormalizedPath: "/",
    79  				IsIp:           true,
    80  			},
    81  		},
    82  		"ip4 URL with port": {
    83  			input: "https://127.0.0.1:8443",
    84  			expect: &URL{
    85  				Scheme:         "https",
    86  				Authority:      "127.0.0.1:8443",
    87  				Path:           "",
    88  				NormalizedPath: "/",
    89  				IsIp:           true,
    90  			},
    91  		},
    92  		"ip6 short": {
    93  			input: "https://[fe80::1]",
    94  			expect: &URL{
    95  				Scheme:         "https",
    96  				Authority:      "[fe80::1]",
    97  				Path:           "",
    98  				NormalizedPath: "/",
    99  				IsIp:           true,
   100  			},
   101  		},
   102  		"ip6 short with interface": {
   103  			input: "https://[fe80::1%25en0]",
   104  			expect: &URL{
   105  				Scheme:         "https",
   106  				Authority:      "[fe80::1%25en0]",
   107  				Path:           "",
   108  				NormalizedPath: "/",
   109  				IsIp:           true,
   110  			},
   111  		},
   112  		"ip6 short with port": {
   113  			input: "https://[fe80::1]:8443",
   114  			expect: &URL{
   115  				Scheme:         "https",
   116  				Authority:      "[fe80::1]:8443",
   117  				Path:           "",
   118  				NormalizedPath: "/",
   119  				IsIp:           true,
   120  			},
   121  		},
   122  		"ip6 short with port with interface": {
   123  			input: "https://[fe80::1%25en0]:8443",
   124  			expect: &URL{
   125  				Scheme:         "https",
   126  				Authority:      "[fe80::1%25en0]:8443",
   127  				Path:           "",
   128  				NormalizedPath: "/",
   129  				IsIp:           true,
   130  			},
   131  		},
   132  	}
   133  
   134  	for name, c := range cases {
   135  		t.Run(name, func(t *testing.T) {
   136  			actual := ParseURL(c.input)
   137  			if c.expect == nil {
   138  				if actual != nil {
   139  					t.Fatalf("expect no result, got %v", *actual)
   140  				}
   141  				return
   142  			}
   143  
   144  			if actual == nil {
   145  				t.Fatalf("expect result, got none")
   146  			}
   147  
   148  			if *c.expect != *actual {
   149  				t.Errorf("%v != %v", *c.expect, *actual)
   150  			}
   151  		})
   152  	}
   153  }
   154  
   155  func TestIsValidHostLabel(t *testing.T) {
   156  	cases := map[string]struct {
   157  		input           string
   158  		allowSubDomains bool
   159  		expect          bool
   160  	}{
   161  		"single label no split": {
   162  			input:  "abc123-",
   163  			expect: true,
   164  		},
   165  		"single label with split": {
   166  			input:           "abc123-",
   167  			allowSubDomains: true,
   168  			expect:          true,
   169  		},
   170  		"multiple labels no split": {
   171  			input:  "abc.123-",
   172  			expect: false,
   173  		},
   174  		"multiple labels with split": {
   175  			input:           "abc.123-",
   176  			allowSubDomains: true,
   177  			expect:          true,
   178  		},
   179  		"multiple labels with split invalid label": {
   180  			input:           "abc.123-...",
   181  			allowSubDomains: true,
   182  			expect:          false,
   183  		},
   184  		"max length host label": {
   185  			input:  "012345678901234567890123456789012345678901234567890123456789123",
   186  			expect: true,
   187  		},
   188  		"too large host label": {
   189  			input:  "0123456789012345678901234567890123456789012345678901234567891234",
   190  			expect: false,
   191  		},
   192  		"too small host label": {
   193  			input:  "",
   194  			expect: false,
   195  		},
   196  	}
   197  
   198  	for name, c := range cases {
   199  		t.Run(name, func(t *testing.T) {
   200  			actual := IsValidHostLabel(c.input, c.allowSubDomains)
   201  			if e, a := c.expect, actual; e != a {
   202  				t.Fatalf("expect %v valid host label, got %v", e, a)
   203  			}
   204  		})
   205  	}
   206  }
   207  

View as plain text