-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[query] Support additional/custom HTTP headers (#2056)
* Added new FlagList type and used it in jaeger-query Signed-off-by: Joe Elliott <number101010@gmail.com> * Added tests to confirm FlagList will be treated as stringSlice by viper Signed-off-by: Joe Elliott <number101010@gmail.com> * Added additionalheaders logic. Added tests Signed-off-by: Joe Elliott <number101010@gmail.com> * Changed http handler to take a header struct Signed-off-by: Joe Elliott <number101010@gmail.com> * Updated help text on param Signed-off-by: Joe Elliott <number101010@gmail.com> * FlagList => StringSlice Signed-off-by: Joe Elliott <number101010@gmail.com> * Additional tests to further specify string slice behavior Signed-off-by: Joe Elliott <number101010@gmail.com> * Added header parsing method and tests Signed-off-by: Joe Elliott <number101010@gmail.com> * Added support for commas in params Signed-off-by: Joe Elliott <number101010@gmail.com> * Updated flags to use expected format Signed-off-by: Joe Elliott <number101010@gmail.com> * Moved stringSliceAsHeader() to flags Signed-off-by: Joe Elliott <number101010@gmail.com> * Added test for empty string slice Signed-off-by: Joe Elliott <number101010@gmail.com> * Moved additional headers to server handler Signed-off-by: Joe Elliott <number101010@gmail.com> * Added handler and tests Signed-off-by: Joe Elliott <number101010@gmail.com> * removed unnecessary whitespace Signed-off-by: Joe Elliott <number101010@gmail.com> * checked out http_handler_test to master Signed-off-by: Joe Elliott <number101010@gmail.com> * checked out http_handler to master Signed-off-by: Joe Elliott <number101010@gmail.com> * Update cmd/query/app/additional_headers_test.go Co-Authored-By: Yuri Shkuro <yurishkuro@users.noreply.github.com> Signed-off-by: Joe Elliott <number101010@gmail.com> * Update cmd/query/app/flags_test.go Co-Authored-By: Yuri Shkuro <yurishkuro@users.noreply.github.com> Signed-off-by: Joe Elliott <number101010@gmail.com> * Update cmd/query/app/flags_test.go Co-Authored-By: Yuri Shkuro <yurishkuro@users.noreply.github.com> Signed-off-by: Joe Elliott <number101010@gmail.com> * Made stringSliceAsHeader return err Signed-off-by: Joe Elliott <number101010@gmail.com> * Added test for bad header params Signed-off-by: Joe Elliott <number101010@gmail.com> Co-authored-by: Yuri Shkuro <yurishkuro@users.noreply.github.com>
- Loading branch information
1 parent
29e7131
commit df16a71
Showing
9 changed files
with
280 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) 2020 The Jaeger 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 app | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
func additionalHeadersHandler(h http.Handler, additionalHeaders http.Header) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
header := w.Header() | ||
for key, values := range additionalHeaders { | ||
header[key] = values | ||
} | ||
|
||
h.ServeHTTP(w, r) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2020 The Jaeger 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 app | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAdditionalHeadersHandler(t *testing.T) { | ||
additionalHeaders := http.Header{} | ||
additionalHeaders.Add("Access-Control-Allow-Origin", "https://mozilla.org") | ||
additionalHeaders.Add("Access-Control-Expose-Headers", "X-My-Custom-Header") | ||
additionalHeaders.Add("Access-Control-Expose-Headers", "X-Another-Custom-Header") | ||
additionalHeaders.Add("Access-Control-Request-Headers", "field1, field2") | ||
|
||
emptyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte{}) | ||
}) | ||
|
||
handler := additionalHeadersHandler(emptyHandler, additionalHeaders) | ||
server := httptest.NewServer(handler) | ||
defer server.Close() | ||
|
||
req, err := http.NewRequest("GET", server.URL, nil) | ||
assert.NoError(t, err) | ||
|
||
resp, err := server.Client().Do(req) | ||
assert.NoError(t, err) | ||
|
||
for k, v := range additionalHeaders { | ||
assert.Equal(t, v, resp.Header[k]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) 2020 The Jaeger 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 config | ||
|
||
import "strings" | ||
|
||
// StringSlice implements the pflag.Value interface and allows for parsing multiple | ||
// config values with the same name | ||
// It purposefully mimics pFlag.stringSliceValue (https://github.com/spf13/pflag/blob/master/string_slice.go) | ||
// in order to be treated like a string slice by both viper and pflag cleanly | ||
type StringSlice []string | ||
|
||
// String implements pflag.Value | ||
func (l *StringSlice) String() string { | ||
if len(*l) == 0 { | ||
return "[]" | ||
} | ||
|
||
return `["` + strings.Join(*l, `","`) + `"]` | ||
} | ||
|
||
// Set implements pflag.Value | ||
func (l *StringSlice) Set(value string) error { | ||
*l = append(*l, value) | ||
return nil | ||
} | ||
|
||
// Type implements pflag.Value | ||
func (l *StringSlice) Type() string { | ||
// this type string needs to match pflag.stringSliceValue's Type | ||
return "stringSlice" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2020 The Jaeger 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 config | ||
|
||
import ( | ||
"flag" | ||
"testing" | ||
|
||
"github.com/spf13/pflag" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStringSlice(t *testing.T) { | ||
f := &StringSlice{} | ||
|
||
assert.Equal(t, "[]", f.String()) | ||
assert.Equal(t, "stringSlice", f.Type()) | ||
|
||
f.Set("test") | ||
assert.Equal(t, `["test"]`, f.String()) | ||
|
||
f.Set("test2") | ||
assert.Equal(t, `["test","test2"]`, f.String()) | ||
|
||
f.Set("test3,test4") | ||
assert.Equal(t, `["test","test2","test3,test4"]`, f.String()) | ||
} | ||
|
||
func TestStringSliceTreatedAsStringSlice(t *testing.T) { | ||
f := &StringSlice{} | ||
|
||
// create and add flags/values to a go flag set | ||
flagset := flag.NewFlagSet("test", flag.ContinueOnError) | ||
flagset.Var(f, "test", "test") | ||
|
||
err := flagset.Set("test", "asdf") | ||
assert.NoError(t, err) | ||
err = flagset.Set("test", "blerg") | ||
assert.NoError(t, err) | ||
err = flagset.Set("test", "other,thing") | ||
assert.NoError(t, err) | ||
|
||
// add go flag set to pflag | ||
pflagset := pflag.FlagSet{} | ||
pflagset.AddGoFlagSet(flagset) | ||
actual, err := pflagset.GetStringSlice("test") | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, []string{"asdf", "blerg", "other,thing"}, actual) | ||
} |