-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from bannzai/support_introspection_header
Add CLI argument for introspection-header
- Loading branch information
Showing
4 changed files
with
98 additions
and
2 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,3 @@ | ||
package multichecker | ||
|
||
type ExportedIntrospectionHeader = introspectionHeader |
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,34 @@ | ||
package multichecker | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// introspectionHeader confirmed to `Value` interface in `flag` package. | ||
type introspectionHeader http.Header | ||
|
||
func (ih introspectionHeader) String() string { | ||
var s string | ||
for k, v := range ih { | ||
if len(s) != 0 { | ||
s += "," | ||
} | ||
s += fmt.Sprintf("%v:%v", k, v[0]) | ||
} | ||
return s | ||
} | ||
|
||
func (ih introspectionHeader) Set(args string) error { | ||
for _, keyAndValueString := range strings.Split(args, ",") { | ||
keyAndValue := strings.Split(keyAndValueString, ":") | ||
key := keyAndValue[0] | ||
value := keyAndValue[1] | ||
|
||
// Supports only one value. | ||
ih[key] = []string{value} | ||
} | ||
return nil | ||
|
||
} |
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,54 @@ | ||
package multichecker_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gqlgo/gqlanalysis/multichecker" | ||
) | ||
|
||
func TestIntrospectionHeader_Set(t *testing.T) { | ||
cases := []struct { | ||
args string | ||
want map[string]string | ||
}{ | ||
{"key1:value1", map[string]string{"key1": "value1"}}, | ||
{"key1:value1,key2:value2", map[string]string{"key1": "value1", "key2": "value2"}}, | ||
} | ||
|
||
for _, tt := range cases { | ||
actual := make(multichecker.ExportedIntrospectionHeader) | ||
actual.Set(tt.args) | ||
|
||
if len(actual) != len(tt.want) { | ||
t.Errorf("len(actual) != len(tt.want). actual: %v, want: %v", actual, tt.want) | ||
} | ||
for wantKey, wantValue := range tt.want { | ||
actualValue, ok := actual[wantKey] | ||
if !ok { | ||
t.Errorf("Does not contain key: %s", wantKey) | ||
} | ||
if wantValue != actualValue[0] { | ||
t.Errorf("wantValue is not equal actualValue. key: %v, wantKey: %v, actualValue: %v", wantKey, wantValue, actualValue) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestIntrospectionHeader_Value(t *testing.T) { | ||
cases := []struct { | ||
want string | ||
}{ | ||
{"key1:value1"}, | ||
{"key1:value1,key2:value2"}, | ||
} | ||
|
||
for _, tt := range cases { | ||
ih := make(multichecker.ExportedIntrospectionHeader) | ||
ih.Set(tt.want) | ||
actual := ih.String( | ||
) | ||
if actual != tt.want { | ||
t.Errorf("actual != tt.want. actual: %v, want: %v", actual, tt.want) | ||
} | ||
} | ||
} |