aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/aws/smithy-go/encoding/httpbinding/uri_test.go
blob: 039e1e5baa85ff98dea06b4faee5c8c917fc8e57 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package httpbinding

import (
	"fmt"
	"math/big"
	"reflect"
	"strconv"
	"testing"
)

func TestURIValue(t *testing.T) {
	const uriKey = "someKey"
	const path = "/some/{someKey}/{path+}"

	type expected struct {
		path string
		raw  string
	}

	cases := map[string]struct {
		path     string
		args     []interface{}
		expected expected
	}{
		"bool": {
			path: path,
			args: []interface{}{true},
			expected: expected{
				path: "/some/true/{path+}",
				raw:  "/some/true/{path+}",
			},
		},
		"string": {
			path: path,
			args: []interface{}{"someValue"},
			expected: expected{
				path: "/some/someValue/{path+}",
				raw:  "/some/someValue/{path+}",
			},
		},
		"byte": {
			path: path,
			args: []interface{}{int8(127)},
			expected: expected{
				path: "/some/127/{path+}",
				raw:  "/some/127/{path+}",
			},
		},
		"short": {
			path: path,
			args: []interface{}{int16(32767)},
			expected: expected{
				path: "/some/32767/{path+}",
				raw:  "/some/32767/{path+}",
			},
		},
		"integer": {
			path: path,
			args: []interface{}{int32(2147483647)},
			expected: expected{
				path: "/some/2147483647/{path+}",
				raw:  "/some/2147483647/{path+}",
			},
		},
		"long": {
			path: path,
			args: []interface{}{int64(9223372036854775807)},
			expected: expected{
				path: "/some/9223372036854775807/{path+}",
				raw:  "/some/9223372036854775807/{path+}",
			},
		},
		"float32": {
			path: path,
			args: []interface{}{float32(3.14159)},
			expected: expected{
				path: "/some/3.14159/{path+}",
				raw:  "/some/3.14159/{path+}",
			},
		},
		"float64": {
			path: path,
			args: []interface{}{float64(3.14159)},
			expected: expected{
				path: "/some/3.14159/{path+}",
				raw:  "/some/3.14159/{path+}",
			},
		},
		"bigInteger": {
			path: path,
			args: []interface{}{new(big.Int).SetInt64(1)},
			expected: expected{
				path: "/some/1/{path+}",
				raw:  "/some/1/{path+}",
			},
		},
		"bigDecimal": {
			path: path,
			args: []interface{}{new(big.Float).SetFloat64(1024.10241024)},
			expected: expected{
				path: "/some/1.02410241024e+03/{path+}",
				raw:  "/some/1.02410241024e%2B03/{path+}",
			},
		},
	}

	buffer := make([]byte, 1024)

	for name, tt := range cases {
		t.Run(name, func(t *testing.T) {
			pBytes, rBytes := []byte(tt.path), []byte(tt.path)

			uv := newURIValue(&pBytes, &rBytes, &buffer, uriKey)

			if err := setURI(uv, tt.args); err != nil {
				t.Fatalf("expected no error, %v", err)
			}

			if e, a := tt.expected.path, string(pBytes); e != a {
				t.Errorf("expected %v, got %v", e, a)
			}

			if e, a := tt.expected.raw, string(rBytes); e != a {
				t.Errorf("expected %v, got %v", e, a)
			}
		})
	}
}

func setURI(uv URIValue, args []interface{}) error {
	value := args[0]

	switch value.(type) {
	case bool:
		return reflectCall(reflect.ValueOf(uv.Boolean), args)
	case string:
		return reflectCall(reflect.ValueOf(uv.String), args)
	case int8:
		return reflectCall(reflect.ValueOf(uv.Byte), args)
	case int16:
		return reflectCall(reflect.ValueOf(uv.Short), args)
	case int32:
		return reflectCall(reflect.ValueOf(uv.Integer), args)
	case int64:
		return reflectCall(reflect.ValueOf(uv.Long), args)
	case float32:
		return reflectCall(reflect.ValueOf(uv.Float), args)
	case float64:
		return reflectCall(reflect.ValueOf(uv.Double), args)
	case *big.Int:
		return reflectCall(reflect.ValueOf(uv.BigInteger), args)
	case *big.Float:
		return reflectCall(reflect.ValueOf(uv.BigDecimal), args)
	default:
		return fmt.Errorf("unhandled value type")
	}
}

func TestParseURI(t *testing.T) {
	cases := []struct {
		Value string
		Path  string
		Query string
	}{
		{
			Value: "/my/uri/foo/bar/baz",
			Path:  "/my/uri/foo/bar/baz",
			Query: "",
		},
		{
			Value: "/path?requiredKey",
			Path:  "/path",
			Query: "requiredKey",
		},
		{
			Value: "/path?",
			Path:  "/path",
			Query: "",
		},
		{
			Value: "?",
			Path:  "",
			Query: "",
		},
	}

	for i, tt := range cases {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			path, query := SplitURI(tt.Value)
			if e, a := tt.Path, path; e != a {
				t.Errorf("expected %v, got %v", e, a)
			}
			if e, a := tt.Query, query; e != a {
				t.Errorf("expected %v, got %v", e, a)
			}
		})
	}
}