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

     1  //go:build go1.7
     2  // +build go1.7
     3  
     4  package protocol
     5  
     6  import (
     7  	"net/http"
     8  	"net/url"
     9  	"testing"
    10  
    11  	"github.com/aavshr/aws-sdk-go/aws"
    12  	"github.com/aavshr/aws-sdk-go/aws/request"
    13  )
    14  
    15  func TestHostPrefixBuilder(t *testing.T) {
    16  	cases := map[string]struct {
    17  		URLHost  string
    18  		ReqHost  string
    19  		Prefix   string
    20  		LabelsFn func() map[string]string
    21  		Disabled bool
    22  
    23  		ExpectURLHost string
    24  		ExpectReqHost string
    25  	}{
    26  		"no labels": {
    27  			URLHost:       "service.region.amazonaws.com",
    28  			Prefix:        "data-",
    29  			ExpectURLHost: "data-service.region.amazonaws.com",
    30  		},
    31  		"with labels": {
    32  			URLHost: "service.region.amazonaws.com",
    33  			Prefix:  "{first}-{second}.",
    34  			LabelsFn: func() map[string]string {
    35  				return map[string]string{
    36  					"first":  "abc",
    37  					"second": "123",
    38  				}
    39  			},
    40  			ExpectURLHost: "abc-123.service.region.amazonaws.com",
    41  		},
    42  		"with host prefix disabled": {
    43  			Disabled: true,
    44  			URLHost:  "service.region.amazonaws.com",
    45  			Prefix:   "{first}-{second}.",
    46  			LabelsFn: func() map[string]string {
    47  				return map[string]string{
    48  					"first":  "abc",
    49  					"second": "123",
    50  				}
    51  			},
    52  			ExpectURLHost: "service.region.amazonaws.com",
    53  		},
    54  		"with duplicate labels": {
    55  			URLHost: "service.region.amazonaws.com",
    56  			Prefix:  "{first}-{second}-{first}.",
    57  			LabelsFn: func() map[string]string {
    58  				return map[string]string{
    59  					"first":  "abc",
    60  					"second": "123",
    61  				}
    62  			},
    63  			ExpectURLHost: "abc-123-abc.service.region.amazonaws.com",
    64  		},
    65  		"with unbracketed labels": {
    66  			URLHost: "service.region.amazonaws.com",
    67  			Prefix:  "first-{second}.",
    68  			LabelsFn: func() map[string]string {
    69  				return map[string]string{
    70  					"first":  "abc",
    71  					"second": "123",
    72  				}
    73  			},
    74  			ExpectURLHost: "first-123.service.region.amazonaws.com",
    75  		},
    76  		"with req host": {
    77  			URLHost:       "service.region.amazonaws.com:1234",
    78  			ReqHost:       "service.region.amazonaws.com",
    79  			Prefix:        "data-",
    80  			ExpectURLHost: "data-service.region.amazonaws.com:1234",
    81  			ExpectReqHost: "data-service.region.amazonaws.com",
    82  		},
    83  	}
    84  
    85  	for name, c := range cases {
    86  		t.Run(name, func(t *testing.T) {
    87  			builder := HostPrefixBuilder{
    88  				Prefix: c.Prefix, LabelsFn: c.LabelsFn,
    89  			}
    90  			req := &request.Request{
    91  				Config: aws.Config{
    92  					DisableEndpointHostPrefix: aws.Bool(c.Disabled),
    93  				},
    94  				HTTPRequest: &http.Request{
    95  					Host: c.ReqHost,
    96  					URL: &url.URL{
    97  						Host: c.URLHost,
    98  					},
    99  				},
   100  			}
   101  
   102  			builder.Build(req)
   103  			if e, a := c.ExpectURLHost, req.HTTPRequest.URL.Host; e != a {
   104  				t.Errorf("expect URL host %v, got %v", e, a)
   105  			}
   106  			if e, a := c.ExpectReqHost, req.HTTPRequest.Host; e != a {
   107  				t.Errorf("expect request host %v, got %v", e, a)
   108  			}
   109  		})
   110  	}
   111  }