-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI: add new -header option to be able to add headers to all cli requ…
…ests #8754
- Loading branch information
1 parent
c97e44e
commit d963cdf
Showing
3 changed files
with
100 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:improvement | ||
cli: add new http option : -header which enable sending arbitrary headers with the cli | ||
``` |
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,69 @@ | ||
package command | ||
|
||
import ( | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func getDefaultCliHeaders(t *testing.T) http.Header { | ||
bc := &BaseCommand{} | ||
cli, err := bc.Client() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return cli.Headers() | ||
} | ||
|
||
func TestClient_FlagHeader(t *testing.T) { | ||
defaultHeaders := getDefaultCliHeaders(t) | ||
|
||
cases := []struct { | ||
Input map[string]string | ||
Valid bool | ||
}{ | ||
{ | ||
map[string]string{}, | ||
true, | ||
}, | ||
{ | ||
map[string]string{"foo": "bar", "header2": "value2"}, | ||
true, | ||
}, | ||
{ | ||
map[string]string{"X-Vault-foo": "bar", "header2": "value2"}, | ||
false, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
expectedHeaders := defaultHeaders.Clone() | ||
for key, val := range tc.Input { | ||
expectedHeaders.Add(key, val) | ||
} | ||
|
||
bc := &BaseCommand{flagHeader: tc.Input} | ||
cli, err := bc.Client() | ||
|
||
if err == nil && !tc.Valid { | ||
t.Errorf("No error for input[%#v], but not valid", tc.Input) | ||
continue | ||
} | ||
|
||
if err != nil { | ||
if tc.Valid { | ||
t.Errorf("Error[%v] with input[%#v], but valid", err, tc.Input) | ||
} | ||
continue | ||
} | ||
|
||
if cli == nil { | ||
t.Error("client should not be nil") | ||
} | ||
|
||
actualHeaders := cli.Headers() | ||
if !reflect.DeepEqual(expectedHeaders, actualHeaders) { | ||
t.Errorf("expected [%#v] but got [%#v]", expectedHeaders, actualHeaders) | ||
} | ||
} | ||
} |