-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathheader_test.go
121 lines (100 loc) · 3.77 KB
/
header_test.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
// +build e2e
/*
Copyright 2019 The Knative 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 conformance
import (
"net"
"net/http"
"regexp"
"strings"
"testing"
"github.com/knative/pkg/test/logging"
"github.com/knative/serving/test"
)
// TestMustHaveHeadersSet verified that all headers declared as "MUST" in the runtime
// contract are present from the point of view of the user container.
func TestMustHaveHeadersSet(t *testing.T) {
logger := logging.GetContextLogger(t.Name())
clients := setup(t)
ri, err := fetchRuntimeInfo(clients, logger, &test.Options{})
if err != nil {
t.Fatalf("Error fetching runtime info: %v", err)
}
// For incoming requests, the Host header is promoted to the
// Request.Host field and removed from the Header map. Therefore we
// check against the Host field instead of the map.
if ri.Request.Host == "" {
// We just check that the host header exists and is non-empty as the request
// may be made internally or externally which will result in a different host.
t.Error("Header host was not present on request")
}
// TODO(#3112): Validate Forwarded header once it is enabled.
}
type checkIPList struct {
expected string
}
// MatchString returns true if the passed string is a list of IPv4 or IPv6 Addresses. Otherwise returns false.
func (*checkIPList) MatchString(s string) bool {
for _, ip := range strings.Split(s, ",") {
if net.ParseIP(strings.TrimSpace(ip)) == nil {
return false
}
}
return true
}
// String returns the expected string from the object.
func (c *checkIPList) String() string {
return c.expected
}
// TestMustHaveHeadersSet verified that all headers declared as "SHOULD" in the runtime
// contract are present from the point of view of the user container.
func TestShouldHaveHeadersSet(t *testing.T) {
logger := logging.GetContextLogger(t.Name())
clients := setup(t)
expectedHeaders := map[string]interface {
MatchString(string) bool
String() string
}{
// We expect the protocol to be http for our test image.
"x-forwarded-proto": regexp.MustCompile("https?"),
// We expect the value to be a list of at least one comma separated IP addresses (IPv4 or IPv6).
"x-forwarded-for": &checkIPList{expected: "comma separated IPv4 or IPv6 addresses"},
// Trace Headers
// See https://github.com/openzipkin/b3-propagation#overall-process
// We use the multiple header variant for tracing. We do not validate the single header variant.
// We expect the value to be a 64-bit hex string
"x-b3-spanid": regexp.MustCompile("[0-9a-f]{16}"),
// We expect the value to be a 64-bit or 128-bit hex string
"x-b3-traceid": regexp.MustCompile("[0-9a-f]{16}|[0-9a-f]{32}"),
// "x-b3-parentspanid" and "x-b3-sampled" are often present for tracing, but are not
// required for tracing so we do not validate them.
}
ri, err := fetchRuntimeInfo(clients, logger, &test.Options{})
if err != nil {
t.Fatalf("Error fetching runtime info: %v", err)
}
headers := ri.Request.Headers
for header, match := range expectedHeaders {
hvl, ok := headers[http.CanonicalHeaderKey(header)]
if !ok {
t.Errorf("Header %s was not present on request", header)
continue
}
// Check against each value for the header key
for _, hv := range hvl {
if !match.MatchString(hv) {
t.Errorf("%s = %s; want: %s", header, hv, match.String())
}
}
}
}