-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathheaders_test.go
238 lines (200 loc) · 6.88 KB
/
headers_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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package http_test
import (
"net/http"
commonhttp "code.cloudfoundry.org/gorouter/common/http"
"code.cloudfoundry.org/gorouter/common/http/fakes"
"code.cloudfoundry.org/lager"
"code.cloudfoundry.org/lager/lagertest"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
)
const uuid_regex = `^[[:xdigit:]]{8}(-[[:xdigit:]]{4}){3}-[[:xdigit:]]{12}$`
// 64-bit random hexadecimal string
const b3_id_regex = `^[[:xdigit:]]{16}$`
var _ = Describe("Headers", func() {
Describe("SetVcapRequestIdHeader", func() {
var (
logger lager.Logger
req *http.Request
)
BeforeEach(func() {
logger = lagertest.NewTestLogger("headers-test")
var err error
req, err = http.NewRequest("GET", "test.endpoint", nil)
Expect(err).ToNot(HaveOccurred())
})
JustBeforeEach(func() {
commonhttp.SetVcapRequestIdHeader(req, logger)
})
Context("when X-Vcap-Request-Id is not set", func() {
It("sets the X-Vcap-Request-Id header", func() {
reqID := req.Header.Get(commonhttp.VcapRequestIdHeader)
Expect(reqID).ToNot(BeEmpty())
Expect(reqID).To(MatchRegexp(uuid_regex))
})
It("logs the header", func() {
reqID := req.Header.Get(commonhttp.VcapRequestIdHeader)
Expect(logger).To(gbytes.Say("vcap-request-id-header-set"))
Expect(logger).To(gbytes.Say(reqID))
})
})
Context("when X-Vcap-Request-Id is set", func() {
BeforeEach(func() {
req.Header.Set(commonhttp.VcapRequestIdHeader, "BOGUS-HEADER")
})
It("overwrites the X-Vcap-Request-Id header", func() {
reqID := req.Header.Get(commonhttp.VcapRequestIdHeader)
Expect(reqID).ToNot(BeEmpty())
Expect(reqID).ToNot(Equal("BOGUS-HEADER"))
Expect(reqID).To(MatchRegexp(uuid_regex))
})
It("logs the header", func() {
reqID := req.Header.Get(commonhttp.VcapRequestIdHeader)
Expect(logger).To(gbytes.Say("vcap-request-id-header-set"))
Expect(logger).To(gbytes.Say(reqID))
})
})
})
Describe("SetTraceHeaders", func() {
var respWriter http.ResponseWriter
BeforeEach(func() {
respWriter = httpfakes.NewFakeResponseWriter()
})
JustBeforeEach(func() {
commonhttp.SetTraceHeaders(respWriter, "1.1.1.1", "example.com")
})
It("sets the trace headers on the response", func() {
Expect(respWriter.Header().Get(commonhttp.VcapRouterHeader)).To(Equal("1.1.1.1"))
Expect(respWriter.Header().Get(commonhttp.VcapBackendHeader)).To(Equal("example.com"))
Expect(respWriter.Header().Get(commonhttp.CfRouteEndpointHeader)).To(Equal("example.com"))
})
})
Describe("SetB3Headers", func() {
var (
logger lager.Logger
req *http.Request
)
BeforeEach(func() {
var err error
req, err = http.NewRequest("GET", "test.endpoint", nil)
Expect(err).ToNot(HaveOccurred())
})
JustBeforeEach(func() {
commonhttp.SetB3Headers(req, logger)
})
Context("when logger is set", func() {
BeforeEach(func() {
logger = lagertest.NewTestLogger("headers-test")
})
It("generates a new b3 span id", func() {
reqID := req.Header.Get(commonhttp.B3SpanIdHeader)
Expect(reqID).ToNot(BeEmpty())
Expect(reqID).To(MatchRegexp(b3_id_regex))
})
Context("when X-B3-TraceId is not set", func() {
It("generates a new b3 id and sets the X-B3-TraceId header and X-B3-SpanId to the same value", func() {
traceID := req.Header.Get(commonhttp.B3TraceIdHeader)
spanID := req.Header.Get(commonhttp.B3SpanIdHeader)
parentSpanID := req.Header.Get(commonhttp.B3ParentSpanIdHeader)
Expect(traceID).ToNot(BeEmpty())
Expect(spanID).ToNot(BeEmpty())
Expect(parentSpanID).ToNot(BeEmpty())
Expect(traceID).To(MatchRegexp(b3_id_regex))
Expect(traceID).To(Equal(spanID))
})
})
Context("when X-B3-TraceId is set", func() {
BeforeEach(func() {
req.Header.Set(commonhttp.B3TraceIdHeader, "BOGUS-HEADER")
})
It("should override the X-B3-TraceId header", func() {
reqID := req.Header.Get(commonhttp.B3TraceIdHeader)
Expect(reqID).ToNot(BeEmpty())
Expect(reqID).To(MatchRegexp(b3_id_regex))
})
Context("when X-B3-SpanId is set", func() {
BeforeEach(func() {
req.Header.Set(commonhttp.B3SpanIdHeader, "BOGUS-SpanId-HEADER")
})
It("should set the X-B3-ParentSpanId header", func() {
Expect(req.Header.Get(commonhttp.B3ParentSpanIdHeader)).To(Equal("BOGUS-SpanId-HEADER"))
})
It("should not override the X-B3-TraceId header", func() {
Expect(req.Header.Get(commonhttp.B3TraceIdHeader)).To(Equal("BOGUS-HEADER"))
})
It("logs the header", func() {
Expect(logger).To(gbytes.Say("b3-trace-id-header-exists"))
Expect(logger).To(gbytes.Say("BOGUS-HEADER"))
})
})
Context("when X-B3-SpanId is not set", func() {
BeforeEach(func() {
req.Header.Set(commonhttp.B3SpanIdHeader, "")
})
It("should set the X-B3-ParentSpanId header", func() {
Expect(req.Header.Get(commonhttp.B3ParentSpanIdHeader)).To(Equal("-"))
})
})
})
})
Context("when logger is nil", func() {
It("does not fail when X-B3-Span is not set", func() {
reqID := req.Header.Get(commonhttp.B3SpanIdHeader)
Expect(reqID).ToNot(BeEmpty())
Expect(reqID).To(MatchRegexp(b3_id_regex))
})
It("does not fail when X-B3-TraceId is not set", func() {
reqID := req.Header.Get(commonhttp.B3TraceIdHeader)
Expect(reqID).ToNot(BeEmpty())
Expect(reqID).To(MatchRegexp(b3_id_regex))
})
Context("when X-B3-TraceId and X-B3-SpanId are set", func() {
BeforeEach(func() {
req.Header.Set(commonhttp.B3TraceIdHeader, "BOGUS-HEADER")
req.Header.Set(commonhttp.B3SpanIdHeader, "SPAN-HEADER")
})
It("does not fail when X-B3-TraceId is set", func() {
Expect(req.Header.Get(commonhttp.B3TraceIdHeader)).To(Equal("BOGUS-HEADER"))
})
It("should set the X-B3-ParentSpanId header", func() {
Expect(req.Header.Get(commonhttp.B3ParentSpanIdHeader)).To(Equal("SPAN-HEADER"))
})
})
})
})
Describe("ValidateCfAppInstance", func() {
var (
appInstanceHeader string
)
Context("when given a complete app instance header", func() {
BeforeEach(func() {
appInstanceHeader = "app-id:1"
})
It("returns the app id and app index", func() {
appID, appIndex, err := commonhttp.ValidateCfAppInstance(appInstanceHeader)
Expect(err).ToNot(HaveOccurred())
Expect(appID).To(Equal("app-id"))
Expect(appIndex).To(Equal("1"))
})
})
Context("when given an incomplete app instance header", func() {
BeforeEach(func() {
appInstanceHeader = "app-id:"
})
It("returns an error", func() {
_, _, err := commonhttp.ValidateCfAppInstance(appInstanceHeader)
Expect(err).To(HaveOccurred())
})
})
Context("when only the app id is given", func() {
BeforeEach(func() {
appInstanceHeader = "app-id"
})
It("returns an error", func() {
_, _, err := commonhttp.ValidateCfAppInstance(appInstanceHeader)
Expect(err).To(HaveOccurred())
})
})
})
})