github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/header/middleware_test.go (about) 1 /* 2 * Copyright 2020 The Compass Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package header_test 18 19 import ( 20 "net/http" 21 "net/http/httptest" 22 "testing" 23 24 "github.com/kyma-incubator/compass/components/director/pkg/header" 25 26 "github.com/kyma-incubator/compass/components/director/pkg/correlation" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 const expectedRequestID = "123" 31 32 func TestAttachHeadersToContext(t *testing.T) { 33 // given 34 handler := header.AttachHeadersToContext() 35 36 t.Run("request headers are persisted in context", func(t *testing.T) { 37 nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 38 headersFromContext, ok := r.Context().Value(header.ContextKey).(http.Header) 39 assert.True(t, ok) 40 41 actual := headersFromContext.Get(correlation.RequestIDHeaderKey) 42 assert.Equal(t, actual, expectedRequestID) 43 44 headerFromRequest := r.Header.Get(correlation.RequestIDHeaderKey) 45 assert.Equal(t, headerFromRequest, expectedRequestID) 46 }) 47 48 req := httptest.NewRequest("GET", "/", nil) 49 req.Header.Set(correlation.RequestIDHeaderKey, expectedRequestID) 50 51 handler(nextHandler).ServeHTTP(httptest.NewRecorder(), req) 52 }) 53 }