File: fallback_value_test.go

package info (click to toggle)
golang-github-vulcand-oxy 2.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 728 kB
  • sloc: makefile: 14
file content (218 lines) | stat: -rw-r--r-- 5,871 bytes parent folder | download
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package stickycookie

import (
	"fmt"
	"net/url"
	"path"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/vulcand/oxy/v2/internal/holsterv4/clock"
)

func TestFallbackValue_FindURL(t *testing.T) {
	servers := []*url.URL{
		{Scheme: "https", Host: "10.10.10.42", Path: "/"},
		{Scheme: "http", Host: "10.10.10.10", Path: "/foo"},
		{Scheme: "http", Host: "10.10.10.11", Path: "/", User: url.User("John Doe")},
		{Scheme: "http", Host: "10.10.10.10", Path: "/"},
	}

	aesValue, err := NewAESValue([]byte("95Bx9JkKX3xbd7z3"), 5*clock.Second)
	require.NoError(t, err)

	values := []struct {
		Name        string
		CookieValue CookieValue
	}{
		{Name: "rawValue", CookieValue: &RawValue{}},
		{Name: "hashValue", CookieValue: &HashValue{Salt: "foo"}},
		{Name: "aesValue", CookieValue: aesValue},
	}

	for _, from := range values {
		from := from
		for _, to := range values {
			to := to
			t.Run(fmt.Sprintf("From: %s, To %s", from.Name, to.Name), func(t *testing.T) {
				t.Parallel()

				value, err := NewFallbackValue(from.CookieValue, to.CookieValue)
				if from.CookieValue == nil && to.CookieValue == nil {
					assert.Error(t, err)
					return
				}
				require.NoError(t, err)

				if from.CookieValue != nil {
					// URL found From value
					findURL, err := value.FindURL(from.CookieValue.Get(servers[0]), servers)
					require.NoError(t, err)
					assert.Equal(t, servers[0], findURL)

					// URL not found From value
					findURL, _ = value.FindURL(from.CookieValue.Get(mustJoin(t, servers[0], "bar")), servers)
					assert.Nil(t, findURL)
				}

				if to.CookieValue != nil {
					// URL found To Value
					findURL, err := value.FindURL(to.CookieValue.Get(servers[0]), servers)
					require.NoError(t, err)
					assert.Equal(t, servers[0], findURL)

					// URL not found To value
					findURL, _ = value.FindURL(to.CookieValue.Get(mustJoin(t, servers[0], "bar")), servers)
					assert.Nil(t, findURL)
				}
			})
		}
	}
}

func TestFallbackValue_FindURL_error(t *testing.T) {
	servers := []*url.URL{
		{Scheme: "http", Host: "10.10.10.10", Path: "/"},
		{Scheme: "https", Host: "10.10.10.42", Path: "/"},
		{Scheme: "http", Host: "10.10.10.10", Path: "/foo"},
		{Scheme: "http", Host: "10.10.10.11", Path: "/", User: url.User("John Doe")},
	}

	hashValue := &HashValue{Salt: "foo"}
	rawValue := &RawValue{}
	aesValue, err := NewAESValue([]byte("95Bx9JkKX3xbd7z3"), 5*clock.Second)
	require.NoError(t, err)

	tests := []struct {
		name             string
		From             CookieValue
		To               CookieValue
		rawValue         string
		want             *url.URL
		expectError      bool
		expectErrorOnNew bool
	}{
		{
			name:     "From RawValue To HashValue with RawValue value",
			From:     rawValue,
			To:       hashValue,
			rawValue: "http://10.10.10.10/",
			want:     servers[0],
		},
		{
			name:     "From RawValue To HashValue with RawValue non matching value",
			From:     rawValue,
			To:       hashValue,
			rawValue: "http://24.10.10.10/",
		},
		{
			name:     "From RawValue To HashValue with HashValue value",
			From:     rawValue,
			To:       hashValue,
			rawValue: hashValue.Get(mustParse(t, "http://10.10.10.10/")),
			want:     servers[0],
		},
		{
			name:     "From RawValue To HashValue with HashValue non matching value",
			From:     rawValue,
			To:       hashValue,
			rawValue: hashValue.Get(mustParse(t, "http://24.10.10.10/")),
		},
		{
			name:     "From HashValue To AESValue with AESValue value",
			From:     hashValue,
			To:       aesValue,
			rawValue: aesValue.Get(mustParse(t, "http://10.10.10.10/")),
			want:     servers[0],
		},
		{
			name:     "From HashValue To AESValue with AESValue non matching value",
			From:     hashValue,
			To:       aesValue,
			rawValue: aesValue.Get(mustParse(t, "http://24.10.10.10/")),
		},
		{
			name:     "From HashValue To AESValue with HashValue value",
			From:     hashValue,
			To:       aesValue,
			rawValue: hashValue.Get(mustParse(t, "http://10.10.10.10/")),
			want:     servers[0],
		},
		{
			name:     "From HashValue To AESValue with AESValue non matching value",
			From:     hashValue,
			To:       aesValue,
			rawValue: aesValue.Get(mustParse(t, "http://24.10.10.10/")),
		},
		{
			name:     "From AESValue To AESValue with AESValue value",
			From:     aesValue,
			To:       aesValue,
			rawValue: aesValue.Get(mustParse(t, "http://10.10.10.10/")),
			want:     servers[0],
		},
		{
			name:     "From AESValue To AESValue with AESValue non matching value",
			From:     aesValue,
			To:       aesValue,
			rawValue: aesValue.Get(mustParse(t, "http://24.10.10.10/")),
		},
		{
			name:     "From AESValue To HashValue with HashValue non matching value",
			From:     aesValue,
			To:       hashValue,
			rawValue: hashValue.Get(mustParse(t, "http://24.10.10.10/")),
		},
		{
			name:             "From nil To RawValue",
			To:               hashValue,
			rawValue:         "http://24.10.10.10/",
			expectErrorOnNew: true,
		},
		{
			name:             "From RawValue To nil",
			From:             rawValue,
			rawValue:         "http://24.10.10.10/",
			expectErrorOnNew: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			value, err := NewFallbackValue(tt.From, tt.To)
			if tt.expectErrorOnNew {
				assert.Error(t, err)
				return
			}
			require.NoError(t, err)

			findURL, err := value.FindURL(tt.rawValue, servers)
			if tt.expectError {
				assert.Error(t, err)
				return
			}
			require.NoError(t, err)

			assert.Equal(t, tt.want, findURL)
		})
	}
}

func mustJoin(t *testing.T, u *url.URL, part string) *url.URL {
	t.Helper()

	nu, err := u.Parse(path.Join(u.Path, part))
	require.NoError(t, err)

	return nu
}

func mustParse(t *testing.T, raw string) *url.URL {
	t.Helper()

	u, err := url.Parse(raw)
	require.NoError(t, err)

	return u
}