Skip to content

Commit bc6158e

Browse files
ataleksandrovFelisiaM
authored andcommitted
Apply comments
1 parent cf3240c commit bc6158e

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

api_test.go

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"net/url"
2727
"strings"
2828

29+
"github.com/onsi/ginkgo/extensions/table"
30+
2931
"github.com/pivotal-cf/brokerapi/middlewares"
3032

3133
"code.cloudfoundry.org/lager"
@@ -111,13 +113,6 @@ var _ = Describe("Service Broker API", func() {
111113
return recorder
112114
}
113115

114-
It("has a X-Correlation-ID header", func() {
115-
response := makeRequest()
116-
117-
header := response.Header().Get("X-Correlation-ID")
118-
Expect(header).Should(Not(BeNil()))
119-
})
120-
121116
It("has a Content-Type header", func() {
122117
response := makeRequest()
123118

@@ -2337,6 +2332,11 @@ var _ = Describe("Service Broker API", func() {
23372332
})
23382333

23392334
Describe("CorrelationIDHeader", func() {
2335+
const correlationID = "fake-correlation-id"
2336+
2337+
type testCase struct {
2338+
correlationIDHeaderName string
2339+
}
23402340

23412341
var (
23422342
fakeServiceBroker *fakes.AutoFakeServiceBroker
@@ -2360,20 +2360,33 @@ var _ = Describe("Service Broker API", func() {
23602360
testServer.Close()
23612361
})
23622362

2363-
When("X-Correlation-ID is passed", func() {
2364-
It("Adds correlation id to the context", func() {
2365-
const correlationID = "fake-correlation-id"
2366-
req.Header.Add("X-Correlation-ID", correlationID)
2363+
table.DescribeTable("Adds correlation id to the context", func(tc testCase) {
2364+
req.Header.Add(tc.correlationIDHeaderName, correlationID)
23672365

2368-
_, err := http.DefaultClient.Do(req)
2369-
Expect(err).NotTo(HaveOccurred())
2366+
_, err := http.DefaultClient.Do(req)
2367+
Expect(err).NotTo(HaveOccurred())
23702368

2371-
Expect(fakeServiceBroker.ServicesCallCount()).To(Equal(1), "Services was not called")
2372-
ctx := fakeServiceBroker.ServicesArgsForCall(0)
2373-
Expect(ctx.Value(middlewares.CorrelationIDKey)).To(Equal(correlationID))
2369+
Expect(fakeServiceBroker.ServicesCallCount()).To(Equal(1), "Services was not called")
2370+
ctx := fakeServiceBroker.ServicesArgsForCall(0)
2371+
Expect(ctx.Value(middlewares.CorrelationIDKey)).To(Equal(correlationID))
2372+
},
2373+
table.Entry("X-Correlation-ID", testCase{
2374+
correlationIDHeaderName: "X-Correlation-ID",
2375+
}),
2376+
table.Entry("X-CorrelationID", testCase{
2377+
correlationIDHeaderName: "X-CorrelationID",
2378+
}),
2379+
table.Entry("X-ForRequest-ID", testCase{
2380+
correlationIDHeaderName: "X-ForRequest-ID",
2381+
}),
2382+
table.Entry("X-Request-ID", testCase{
2383+
correlationIDHeaderName: "X-Request-ID",
2384+
}),
2385+
table.Entry("X-Vcap-Request-Id", testCase{
2386+
correlationIDHeaderName: "X-Vcap-Request-Id",
2387+
}),
2388+
)
23742389

2375-
})
2376-
})
23772390
When("X-Correlation-ID is not passed", func() {
23782391
It("Generates correlation id and adds it to the context", func() {
23792392
_, err := http.DefaultClient.Do(req)

middlewares/correlation_id_header.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,22 @@ var correlationIDHeaders = []string{"X-Correlation-ID", "X-CorrelationID", "X-Fo
1313

1414
func AddCorrelationIDToContext(next http.Handler) http.Handler {
1515
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
16-
var correlationID, headerName string
16+
var correlationID string
1717
var found bool
1818

1919
for _, header := range correlationIDHeaders {
2020
headerValue := req.Header.Get(header)
2121
if headerValue != "" {
2222
correlationID = headerValue
23-
headerName = header
2423
found = true
2524
break
2625
}
2726
}
2827

2928
if !found {
3029
correlationID = generateCorrelationID()
31-
headerName = correlationIDHeaders[0]
3230
}
3331

34-
w.Header().Set(headerName, correlationID)
3532
newCtx := context.WithValue(req.Context(), CorrelationIDKey, correlationID)
3633
next.ServeHTTP(w, req.WithContext(newCtx))
3734
})

0 commit comments

Comments
 (0)