-
Notifications
You must be signed in to change notification settings - Fork 2
CDI-9: Support Custom Headers #96
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
8 commits
Select commit
Hold shift + click to select a range
8dd09bc
CDI-9: Support Custom Headers
wesleymccollam 693f431
Merge branch 'main' into request-custom-header-support, remove unnece…
wesleymccollam 6e59c80
Merge branch 'main' into request-custom-header-support
wesleymccollam 859df4c
disallow overriding of default Authorization header, edit header cust…
wesleymccollam 54dd14c
simplify string method on customtypes slices
wesleymccollam ee383d4
update test error regex pattern
wesleymccollam 026a567
edit tests, stringslice method, default value type
wesleymccollam 1b418c2
update type constructor function
wesleymccollam 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
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,91 @@ | ||
| // Copyright © 2025 Ping Identity Corporation | ||
|
|
||
| package customtypes | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "regexp" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/pflag" | ||
| ) | ||
|
|
||
| type Header struct { | ||
| Key string | ||
| Value string | ||
| } | ||
|
|
||
| type HeaderSlice []Header | ||
|
|
||
| // Verify that the custom type satisfies the pflag.Value interface | ||
| var _ pflag.Value = (*HeaderSlice)(nil) | ||
|
|
||
| func NewHeader(header string) (Header, error) { | ||
| regexPattern := `(^[^\s]+):[\t ]{0,1}(.*)$` | ||
| headerNameRegex := regexp.MustCompile(regexPattern) | ||
| matches := headerNameRegex.FindStringSubmatch(header) | ||
| if len(matches) != 3 { | ||
| return Header{}, fmt.Errorf("failed to set Headers: Invalid header: %s. Headers must be in the proper format. Expected regex pattern: %s", header, regexPattern) | ||
| } | ||
|
|
||
| if matches[1] == "Authorization" { | ||
| return Header{}, fmt.Errorf("failed to set Headers: Invalid header: %s. Authorization header is not allowed", matches[1]) | ||
| } | ||
|
|
||
| return Header{ | ||
| Key: matches[1], | ||
| Value: matches[2], | ||
| }, nil | ||
| } | ||
|
|
||
| func (h *HeaderSlice) Set(val string) error { | ||
| if h == nil { | ||
| return fmt.Errorf("failed to set Headers value: %s. Headers is nil", val) | ||
| } | ||
|
|
||
| if val == "" || val == "[]" { | ||
| return nil | ||
| } else { | ||
| valH := strings.SplitSeq(val, ",") | ||
| for header := range valH { | ||
| headerVal, err := NewHeader(header) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| *h = append(*h, headerVal) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (h HeaderSlice) SetHttpRequestHeaders(request *http.Request) { | ||
| for _, header := range h { | ||
| request.Header.Add(header.Key, header.Value) | ||
| } | ||
| } | ||
|
|
||
| func (h HeaderSlice) Type() string { | ||
| return "[]string" | ||
| } | ||
|
|
||
| func (h HeaderSlice) String() string { | ||
| return strings.Join(h.StringSlice(), ",") | ||
| } | ||
|
|
||
| func (h HeaderSlice) StringSlice() []string { | ||
| if h == nil { | ||
| return []string{} | ||
| } | ||
|
|
||
| headers := []string{} | ||
| for _, header := range h { | ||
| headers = append(headers, fmt.Sprintf("%s:%s", header.Key, header.Value)) | ||
erikostien-pingidentity marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| slices.Sort(headers) | ||
|
|
||
| return headers | ||
erikostien-pingidentity marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
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,40 @@ | ||
| // Copyright © 2025 Ping Identity Corporation | ||
|
|
||
| package customtypes_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/pingidentity/pingcli/internal/customtypes" | ||
| "github.com/pingidentity/pingcli/internal/testing/testutils" | ||
| ) | ||
|
|
||
| // Test Headers Set function | ||
| func Test_Headers_Set(t *testing.T) { | ||
| hs := new(customtypes.HeaderSlice) | ||
|
|
||
| service := "key: value" | ||
| err := hs.Set(service) | ||
| if err != nil { | ||
| t.Errorf("Set returned error: %v", err) | ||
| } | ||
| } | ||
|
|
||
| // Test Headers Set function with invalid value | ||
| func Test_Headers_Set_InvalidValue(t *testing.T) { | ||
| hs := new(customtypes.HeaderSlice) | ||
|
|
||
| invalidValue := "invalid=value" | ||
| expectedErrorPattern := `^failed to set Headers: Invalid header: .*\. Headers must be in the proper format. Expected regex pattern: .*$` | ||
| err := hs.Set(invalidValue) | ||
| testutils.CheckExpectedError(t, err, &expectedErrorPattern) | ||
| } | ||
|
|
||
| // Test Headers Set function with nil | ||
| func Test_Headers_Set_Nil(t *testing.T) { | ||
| var hs *customtypes.HeaderSlice | ||
|
|
||
| expectedErrorPattern := `^failed to set Headers value: .* Headers is nil$` | ||
| err := hs.Set("key: value") | ||
| testutils.CheckExpectedError(t, err, &expectedErrorPattern) | ||
| } |
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.
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.