-
Notifications
You must be signed in to change notification settings - Fork 46
feat: support custom headers #670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
f5174c7
feat: support custom headers
alexanderzobnin 6f9b420
add docs to readme
alexanderzobnin 59399fb
avoid empty header
alexanderzobnin 540a9ea
validate malformed custom headers and improve docs
alexanderzobnin 80b85f9
use sentinel errors for custom header validation
alexanderzobnin 0eba131
Merge branch 'main' into alexz/custom-headers
alexanderzobnin d9497fb
add integration test verifying custom headers are sent in requests
alexanderzobnin 3d4df97
lint
alexanderzobnin 181be80
chore: bump toolchain to 1.26.2
alexanderzobnin a1f2afe
fix viper binding for StringArray flags from YAML config
alexanderzobnin ea77157
lint
alexanderzobnin 1890217
fix linter violations in viper binding
alexanderzobnin d05304d
fix linter
alexanderzobnin 257d29f
refactor TestCustomHeadersSentInRequest to use a channel for capturin…
alexanderzobnin 80efc8a
use strings.Cut
alexanderzobnin 81a0323
Enhance viperValueToStrings to handle typed slices for strings and in…
alexanderzobnin f488c7a
softer validation rules
alexanderzobnin ed898fd
handle arrays the same way as slices
alexanderzobnin a085d3d
return ErrInvalidHeaderFormat instead
alexanderzobnin 00d4e94
fix linters
alexanderzobnin 853ffed
use viperInstance.GetStringSlice()
alexanderzobnin b814e5b
allow viper in linter
alexanderzobnin ed4e7cf
Merge branch 'main' into alexz/custom-headers
alexanderzobnin 89919a0
add env var style test case
alexanderzobnin 2e365cd
fix expected format
alexanderzobnin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,2 @@ | ||
| [tools] | ||
| go = "1.26.2" |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,105 @@ | ||
| /* | ||
| Copyright © 2023 OpenFGA | ||
|
|
||
| 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 cmdutils | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestBindViperToFlags(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| const flagName = "header" | ||
|
|
||
| testcases := []struct { | ||
| name string | ||
| value any | ||
| expected []string | ||
| }{ | ||
| { | ||
| name: "slice value produces one flag value per element", | ||
| value: []any{ | ||
| "X-Custom-Header: value1", | ||
| "X-Request-ID: abc123", | ||
| }, | ||
| expected: []string{"X-Custom-Header: value1", "X-Request-ID: abc123"}, | ||
| }, | ||
|
senojj marked this conversation as resolved.
|
||
| { | ||
| name: "single element slice", | ||
| value: []any{"X-Custom-Header: value1"}, | ||
| expected: []string{"X-Custom-Header: value1"}, | ||
| }, | ||
| { | ||
| name: "empty slice leaves flag untouched", | ||
| value: []any{}, | ||
| expected: []string{}, | ||
| }, | ||
| { | ||
| name: "typed string slice produces one flag value per element", | ||
| value: []string{"X-Custom-Header: value1", "X-Request-ID: abc123"}, | ||
| expected: []string{"X-Custom-Header: value1", "X-Request-ID: abc123"}, | ||
| }, | ||
| { | ||
| name: "typed int slice produces one flag value per element", | ||
| value: []int{1, 2, 3}, | ||
| expected: []string{"1", "2", "3"}, | ||
| }, | ||
| { | ||
| name: "scalar string produces single flag value", | ||
| value: "https://api.fga.example", | ||
| expected: []string{"https://api.fga.example"}, | ||
| }, | ||
| { | ||
| name: "space separated scalar string (env var style) splits into multiple flag values", | ||
| value: "X-Custom-Header:value1 X-Request-ID:abc123", | ||
| expected: []string{"X-Custom-Header:value1", "X-Request-ID:abc123"}, | ||
| }, | ||
| { | ||
| name: "boolean value is stringified", | ||
| value: true, | ||
| expected: []string{"true"}, | ||
| }, | ||
| { | ||
| name: "integer value is stringified", | ||
| value: 42, | ||
| expected: []string{"42"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range testcases { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cmd := &cobra.Command{Use: "root"} | ||
| cmd.Flags().StringArray(flagName, nil, "") | ||
|
|
||
| viperInstance := viper.New() | ||
| viperInstance.Set(flagName, test.value) | ||
|
|
||
| BindViperToFlags(cmd, viperInstance) | ||
|
|
||
| got, err := cmd.Flags().GetStringArray(flagName) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, test.expected, got) | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.