Skip to content

Commit 85f13cb

Browse files
committed
Added handler and tests
Signed-off-by: Joe Elliott <[email protected]>
1 parent 376bbfc commit 85f13cb

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

Diff for: cmd/query/app/additional_headers_handler.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2020 The Jaeger Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package app
16+
17+
import (
18+
"net/http"
19+
)
20+
21+
func additionalHeadersHandler(h http.Handler, additionalHeaders http.Header) http.Handler {
22+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
23+
header := w.Header()
24+
for key, values := range additionalHeaders {
25+
header[key] = values
26+
}
27+
28+
h.ServeHTTP(w, r)
29+
})
30+
}

Diff for: cmd/query/app/additional_headers_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2020 The Jaeger Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package app
16+
17+
import (
18+
"net/http"
19+
"net/http/httptest"
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestAdditionalHeadersHandler(t *testing.T) {
26+
27+
additionalHeaders := http.Header{}
28+
additionalHeaders.Add("Access-Control-Allow-Origin", "https://mozilla.org")
29+
additionalHeaders.Add("Access-Control-Expose-Headers", "X-My-Custom-Header")
30+
additionalHeaders.Add("Access-Control-Expose-Headers", "X-Another-Custom-Header")
31+
additionalHeaders.Add("Access-Control-Request-Headers", "field1, field2")
32+
33+
emptyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
34+
w.Write([]byte{})
35+
})
36+
37+
handler := additionalHeadersHandler(emptyHandler, additionalHeaders)
38+
server := httptest.NewServer(handler)
39+
defer server.Close()
40+
41+
req, err := http.NewRequest("GET", server.URL, nil)
42+
assert.NoError(t, err)
43+
44+
resp, err := httpClient.Do(req)
45+
assert.NoError(t, err)
46+
47+
for k, v := range additionalHeaders {
48+
assert.Equal(t, v, resp.Header[k])
49+
}
50+
}

Diff for: cmd/query/app/server.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ func createHTTPServer(querySvc *querysvc.QueryService, queryOpts *QueryOptions,
8282
apiHandler.RegisterRoutes(r)
8383
RegisterStaticHandler(r, logger, queryOpts)
8484
var handler http.Handler = r
85+
handler = additionalHeadersHandler(handler, queryOpts.AdditionalHeaders)
8586
if queryOpts.BearerTokenPropagation {
86-
handler = bearerTokenPropagationHandler(logger, r)
87+
handler = bearerTokenPropagationHandler(logger, handler)
8788
}
8889
handler = handlers.CompressHandler(handler)
89-
handler = additionalHeadersHandler(handler, queryOpts.AdditionalHeaders)
9090
recoveryHandler := recoveryhandler.NewRecoveryHandler(logger, true)
9191
return &http.Server{
9292
Handler: recoveryHandler(handler),

0 commit comments

Comments
 (0)