-
Notifications
You must be signed in to change notification settings - Fork 25
/
state.go
200 lines (158 loc) · 6.79 KB
/
state.go
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
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package state
import (
"fmt"
net_http "net/http"
net_url "net/url"
"strings"
"github.com/bfenetworks/ingress-bfe/test/e2e/pkg/http"
)
// Scenario holds state for a test scenario
type Scenario struct {
Namespace string
IngressName string
SecretName string
CapturedRequest *http.CapturedRequest
CapturedResponse *http.CapturedResponse
IPOrFQDN map[string]string
NodeIPPort map[string]int32
}
// New creates a new state to use in a test Scenario
func New() *Scenario {
return &Scenario{}
}
// CaptureRoundTrip will perform an HTTP request and return the CapturedRequest and CapturedResponse tuple
func (s *Scenario) CaptureRoundTrip(method, scheme, hostname, path string, query net_url.Values, headerInfo net_http.Header, doRedirect bool) error {
var location string
// scheme == http or https
location, ok := s.IPOrFQDN[scheme]
if !ok {
return fmt.Errorf("scheme is not found in addr: %+v", s.IPOrFQDN)
}
capturedRequest, capturedResponse, err := http.CaptureRoundTrip(method, scheme, hostname, path, location, query, headerInfo, doRedirect)
if err != nil {
return err
}
s.CapturedRequest = capturedRequest
s.CapturedResponse = capturedResponse
return nil
}
// AssertStatusCode returns an error if the captured response status code does not match the expected value
func (s *Scenario) AssertStatusCode(statusCode int) error {
if s.CapturedResponse.StatusCode != statusCode {
return fmt.Errorf("expected status code %v but %v was returned", statusCode, s.CapturedResponse.StatusCode)
}
return nil
}
// AssertServedBy returns an error if the captured request was not served by the expected service
func (s *Scenario) AssertServedBy(service string) error {
if s.CapturedRequest.Service != service {
return fmt.Errorf("expected the request to be served by %v but it was served by %v", service, s.CapturedRequest.Service)
}
return nil
}
// AssertRequestHost returns an error if the captured request host does not match the expected value
func (s *Scenario) AssertRequestHost(host string) error {
if s.CapturedRequest.Host != host {
return fmt.Errorf("expected the request host to be %v but was %v", host, s.CapturedRequest.Host)
}
return nil
}
// AssertTLSHostname returns an error if the captured TLS response hostname does not match the expected value
func (s *Scenario) AssertTLSHostname(hostname string) error {
if s.CapturedResponse.TLSHostname != hostname {
return fmt.Errorf("expected the response TLS hostname to be %v but was %v", hostname, s.CapturedResponse.TLSHostname)
}
return nil
}
// AssertResponseProto returns an error if the captured response proto does not match the expected value
func (s *Scenario) AssertResponseProto(proto string) error {
if s.CapturedResponse.Proto != proto {
return fmt.Errorf("expected the response protocol to be %v but it was %v", proto, s.CapturedResponse.Proto)
}
return nil
}
// AssertRequestProto returns an error if the captured request proto does not match the expected value
func (s *Scenario) AssertRequestProto(proto string) error {
if s.CapturedRequest.Proto != proto {
return fmt.Errorf("expected the request protocol to be %v but it was %v", proto, s.CapturedRequest.Proto)
}
return nil
}
// AssertMethod returns an error if the captured request method does not match the expected value
func (s *Scenario) AssertMethod(method string) error {
if s.CapturedRequest.Method != method {
return fmt.Errorf("expected the request method to be %v but it was %v", method, s.CapturedRequest.Method)
}
return nil
}
// normalizePath returns a normalized path, which query param's order is fixed.
// if parsing the path string failed, return empty string.
func normalizePath(path string) string {
p, err := net_url.Parse(path)
if err != nil {
return ""
}
return p.Path
}
// AssertRequestPath returns an error if the captured request path does not match the expected value
func (s *Scenario) AssertRequestPath(path string) error {
if !strings.HasPrefix(path, "/") {
path = fmt.Sprintf("/%s", path)
}
if normalizePath(path) != normalizePath(s.CapturedRequest.Path) {
return fmt.Errorf("expected the request path to be %v but it was %v", path, s.CapturedRequest.Path)
}
return nil
}
// AssertResponseHeader returns an error if the captured response headers do not contain the expected headerKey,
// or if the matching response header value does not match the expected headerValue.
// If the headerValue string equals `*`, the header value check is ignored.
func (s *Scenario) AssertResponseHeader(headerKey string, headerValue string) error {
if headerValues := s.CapturedResponse.Headers[headerKey]; headerValues == nil {
return fmt.Errorf("expected response headers to contain %v but it only contained %v", headerKey, s.CapturedResponse.Headers)
} else if headerValue != "*" {
for _, value := range headerValues {
if value == headerValue {
return nil
}
}
return fmt.Errorf("expected response headers %v to contain a %v value but it contained %v", headerKey, headerValue, headerValues)
}
return nil
}
// AssertRequestHeader returns an error if the captured request headers do not contain the expected headerKey,
// or if the matching request header value does not match the expected headerValue.
// If the headerValue string equals `*`, the header value check is ignored.
func (s *Scenario) AssertRequestHeader(headerKey string, headerValue string) error {
if headerValues := s.CapturedRequest.Headers[headerKey]; headerValues == nil {
return fmt.Errorf("expected request headers to contain %v but it only contained %v", headerKey, s.CapturedRequest.Headers)
} else if headerValue != "*" {
for _, value := range headerValues {
if value == headerValue {
return nil
}
}
return fmt.Errorf("expected request headers %v to contain a %v value but it contained %v", headerKey, headerValue, headerValues)
}
return nil
}
// AssertResponseCertificate returns nil if the captured certificate for the named host is valid.
// Otherwise it returns an error describing the mismatch.
func (s *Scenario) AssertResponseCertificate(hostname string) error {
if s.CapturedResponse == nil || s.CapturedResponse.Certificate == nil {
return fmt.Errorf("hostname verification requires executing a request and also target an HTTPS URL")
}
return s.CapturedResponse.Certificate.VerifyHostname(hostname)
}