aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/aws/smithy-go/encoding/httpbinding/encode_test.go
blob: 6d972ae5082f894bddc068850f56e643e7257bcb (plain) (blame)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package httpbinding

import (
	"net/http"
	"net/url"
	"reflect"
	"testing"
)

func TestEncoder(t *testing.T) {
	actual := &http.Request{
		Header: http.Header{
			"custom-user-header": {"someValue"},
		},
		URL: &url.URL{
			Path:     "/some/{pathKeyOne}/{pathKeyTwo}",
			RawQuery: "someExistingKeys=foobar",
		},
	}

	expected := &http.Request{
		Header: map[string][]string{
			"custom-user-header": {"someValue"},
			"x-amzn-header-foo":  {"someValue"},
			"x-amzn-meta-foo":    {"someValue"},
		},
		URL: &url.URL{
			Path:     "/some/someValue/path",
			RawPath:  "/some/someValue/path",
			RawQuery: "someExistingKeys=foobar&someKey=someValue&someKey=otherValue",
		},
	}

	encoder, err := NewEncoder(actual.URL.Path, actual.URL.RawQuery, actual.Header)
	if err != nil {
		t.Fatalf("expected no error, got %v", err)
	}

	// Headers
	encoder.AddHeader("x-amzn-header-foo").String("someValue")
	encoder.Headers("x-amzn-meta-").AddHeader("foo").String("someValue")

	// Query
	encoder.SetQuery("someKey").String("someValue")
	encoder.AddQuery("someKey").String("otherValue")

	// URI
	if err := encoder.SetURI("pathKeyOne").String("someValue"); err != nil {
		t.Errorf("expected no err, but got %v", err)
	}

	// URI
	if err := encoder.SetURI("pathKeyTwo").String("path"); err != nil {
		t.Errorf("expected no err, but got %v", err)
	}

	if actual, err = encoder.Encode(actual); err != nil {
		t.Errorf("expected no err, but got %v", err)
	}

	if !reflect.DeepEqual(expected, actual) {
		t.Errorf("expected %v, but got %v", expected, actual)
	}
}

func TestEncoderHasHeader(t *testing.T) {
	encoder, err := NewEncoder("/", "", http.Header{})
	if err != nil {
		t.Fatalf("expected no error, got %v", err)
	}

	if h := "i-dont-exist"; encoder.HasHeader(h) {
		t.Errorf("expect %v not to be set", h)
	}

	encoder.AddHeader("I-do-exist").String("some value")

	if h := "I-do-exist"; !encoder.HasHeader(h) {
		t.Errorf("expect %v to be set", h)
	}

}

func TestEncoderHasQuery(t *testing.T) {
	encoder, err := NewEncoder("/", "", http.Header{})
	if err != nil {
		t.Fatalf("expected no error, got %v", err)
	}

	if q := "i-dont-exist"; encoder.HasQuery(q) {
		t.Errorf("expect %v not to be set", q)
	}

	encoder.AddQuery("I-do-exist").String("some value")

	if q := "I-do-exist"; !encoder.HasQuery(q) {
		t.Errorf("expect %v to be set", q)
	}

}

func TestEncodeContentLength(t *testing.T) {
	cases := map[string]struct {
		headerValue string
		expected    int64
		wantErr     bool
	}{
		"valid number": {
			headerValue: "1024",
			expected:    1024,
		},
		"invalid number": {
			headerValue: "1024.5",
			wantErr:     true,
		},
		"not a number": {
			headerValue: "NaN",
			wantErr:     true,
		},
	}

	for name, tt := range cases {
		t.Run(name, func(t *testing.T) {
			encoder, err := NewEncoder("/", "", http.Header{})
			if err != nil {
				t.Fatalf("expect no error, got %v", err)
			}

			encoder.SetHeader("Content-Length").String(tt.headerValue)

			req := &http.Request{URL: &url.URL{}}
			req, err = encoder.Encode(req)
			if (err != nil) != tt.wantErr {
				t.Fatalf("unexpected error value wantErr=%v", tt.wantErr)
			} else if tt.wantErr {
				return
			}
			if e, a := tt.expected, req.ContentLength; e != a {
				t.Errorf("expect %v, got %v", e, a)
			}
			if v := req.Header.Get("Content-Length"); len(v) > 0 {
				t.Errorf("expect header not to be set")
			}
		})
	}
}