File: util_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.30.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 662,428 kB
  • sloc: java: 16,875; makefile: 432; sh: 175
file content (158 lines) | stat: -rw-r--r-- 3,209 bytes parent folder | download | duplicates (5)
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
package v4

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

func lazyURLParse(v string) func() (*url.URL, error) {
	return func() (*url.URL, error) {
		return url.Parse(v)
	}
}

func TestGetURIPath(t *testing.T) {
	cases := map[string]struct {
		getURL func() (*url.URL, error)
		expect string
	}{
		// Cases
		"with scheme": {
			getURL: lazyURLParse("https://localhost:9000"),
			expect: "/",
		},
		"no port, with scheme": {
			getURL: lazyURLParse("https://localhost"),
			expect: "/",
		},
		"without scheme": {
			getURL: lazyURLParse("localhost:9000"),
			expect: "/",
		},
		"without scheme, with path": {
			getURL: lazyURLParse("localhost:9000/abc123"),
			expect: "/abc123",
		},
		"without scheme, with separator": {
			getURL: lazyURLParse("//localhost:9000"),
			expect: "/",
		},
		"no port, without scheme, with separator": {
			getURL: lazyURLParse("//localhost"),
			expect: "/",
		},
		"without scheme, with separator, with path": {
			getURL: lazyURLParse("//localhost:9000/abc123"),
			expect: "/abc123",
		},
		"no port, without scheme, with separator, with path": {
			getURL: lazyURLParse("//localhost/abc123"),
			expect: "/abc123",
		},
		"opaque with query string": {
			getURL: lazyURLParse("localhost:9000/abc123?efg=456"),
			expect: "/abc123",
		},
		"failing test": {
			getURL: func() (*url.URL, error) {
				endpoint := "https://service.region.amazonaws.com"
				req, _ := http.NewRequest("POST", endpoint, nil)
				u := req.URL

				u.Opaque = "//example.org/bucket/key-._~,!@#$%^&*()"

				query := u.Query()
				query.Set("some-query-key", "value")
				u.RawQuery = query.Encode()

				return u, nil
			},
			expect: "/bucket/key-._~,!@#$%^&*()",
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			u, err := c.getURL()
			if err != nil {
				t.Fatalf("failed to get URL, %v", err)
			}

			actual := GetURIPath(u)
			if e, a := c.expect, actual; e != a {
				t.Errorf("expect %v path, got %v", e, a)
			}
		})
	}
}

func TestStripExcessHeaders(t *testing.T) {
	vals := []string{
		"",
		"123",
		"1 2 3",
		"1 2 3 ",
		"  1 2 3",
		"1  2 3",
		"1  23",
		"1  2  3",
		"1  2  ",
		" 1  2  ",
		"12   3",
		"12   3   1",
		"12           3     1",
		"12     3       1abc123",
	}

	expected := []string{
		"",
		"123",
		"1 2 3",
		"1 2 3",
		"1 2 3",
		"1 2 3",
		"1 23",
		"1 2 3",
		"1 2",
		"1 2",
		"12 3",
		"12 3 1",
		"12 3 1",
		"12 3 1abc123",
	}

	for i := 0; i < len(vals); i++ {
		r := StripExcessSpaces(vals[i])
		if e, a := expected[i], r; e != a {
			t.Errorf("%d, expect %v, got %v", i, e, a)
		}
	}
}

var stripExcessSpaceCases = []string{
	`AWS4-HMAC-SHA256 Credential=AKIDFAKEIDFAKEID/20160628/us-west-2/s3/aws4_request, SignedHeaders=host;x-amz-date, Signature=1234567890abcdef1234567890abcdef1234567890abcdef`,
	`123   321   123   321`,
	`   123   321   123   321   `,
	`   123    321    123          321   `,
	"123",
	"1 2 3",
	"  1 2 3",
	"1  2 3",
	"1  23",
	"1  2  3",
	"1  2  ",
	" 1  2  ",
	"12   3",
	"12   3   1",
	"12           3     1",
	"12     3       1abc123",
}

func BenchmarkStripExcessSpaces(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, v := range stripExcessSpaceCases {
			StripExcessSpaces(v)
		}
	}
}