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