Skip to content

Commit

Permalink
fix to parse "=" of a header correctly (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktr0731 authored Dec 29, 2019
1 parent 33d0d09 commit ddeebcf
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (s *stringToStringSliceValue) Set(val string) error {

out := make(map[string][]string, len(ss))
for _, pair := range ss {
kv := strings.Split(pair, "=")
kv := strings.SplitN(pair, "=", 2)
if len(kv) != 2 {
return fmt.Errorf("%s must be formatted as key=value", pair)
}
Expand Down
42 changes: 32 additions & 10 deletions app/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,38 @@ import (
)

func Test_stringToStringSliceValue(t *testing.T) {
m := make(map[string][]string)
v := newStringToStringValue(map[string][]string{
"ogiso": []string{"setsuna"},
}, &m)
if err := v.Set("touma=kazusa,touma=youko"); err != nil {
t.Fatalf("Set must not return an error, but got '%s'", err)
cases := []struct {
in string
expected string
hasErr bool
}{
{in: "touma=kazusa,touma=youko", expected: `["touma=kazusa,youko"]`, hasErr: false},
{in: "sawamura='spencer=eriri'", expected: `[sawamura='spencer=eriri']`, hasErr: false},
{in: "sawamura=spencer=eriri", expected: `[sawamura=spencer=eriri]`, hasErr: false},
{in: "megumi=kato", expected: `[megumi=kato]`, hasErr: false},
{in: "yuki=asuna,alice", expected: `["yuki=asuna,alice"]`, hasErr: false},
{in: "alice", hasErr: true},
}
const expected = `["touma=kazusa,youko"]`
actual := v.String()
if expected != actual {
t.Errorf("expected '%s', but got '%s'", expected, actual)
for _, c := range cases {
c := c
t.Run(c.in, func(t *testing.T) {
m := make(map[string][]string)
v := newStringToStringValue(map[string][]string{
"ogiso": []string{"setsuna"},
}, &m)
err := v.Set(c.in)
if c.hasErr {
if err == nil {
t.Fatalf("Set must return an error, but got nil")
}
return
} else if err != nil {
t.Fatalf("Set must not return an error, but got '%s'", err)
}
actual := v.String()
if c.expected != actual {
t.Errorf("expected '%s', but got '%s'", c.expected, actual)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

[meta]
autoupdate = false
configversion = "0.8.4"
configversion = "0.8.5"
updatelevel = "patch"

[repl]
Expand Down
9 changes: 3 additions & 6 deletions e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,6 @@ func TestE2E_CLI(t *testing.T) {
args: "--call Unary --file testdata/unary_call.in testdata/test.proto",
expectedCode: 1,
},
"cannot launch CLI mode because --header has an invalid form": {
commonFlags: "--header foo=bar=baz --package api --service Example",
args: "--call Unary --file testdata/unary_call.in testdata/test.proto",
expectedCode: 1,
},
"call unary RPC with an input file by CLI mode": {
commonFlags: "--package api --service Example",
args: "--call Unary --file testdata/unary_call.in testdata/test.proto",
Expand Down Expand Up @@ -188,14 +183,16 @@ func TestE2E_CLI(t *testing.T) {
},
},
"call unary RPC with an input file and custom headers by CLI mode": {
commonFlags: "--header ogiso=setsuna --header touma=kazusa,youko --package api --service Example",
commonFlags: "--header ogiso=setsuna --header touma=kazusa,youko --header sound=of=destiny --package api --service Example",
args: "--call UnaryHeader --file testdata/unary_header.in testdata/test.proto",
assertTest: func(t *testing.T, output string) {
expectedStrings := []string{
"key = ogiso",
"val = setsuna",
"key = touma",
"val = kazusa, youko",
"key = sound",
"val = of=destiny",
}
for _, s := range expectedStrings {
if !strings.Contains(output, s) {
Expand Down
8 changes: 3 additions & 5 deletions e2e/old_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ func TestE2E_OldCLI(t *testing.T) {
args: "--header foo --package api --service Example --call Unary --file testdata/unary_call.in testdata/test.proto",
expectedCode: 1,
},
"cannot launch CLI mode because --header has an invalid form": {
args: "--header foo=bar=baz --package api --service Example --call Unary --file testdata/unary_call.in testdata/test.proto",
expectedCode: 1,
},
"call unary RPC with an input file by CLI mode": {
args: "--package api --service Example --call Unary --file testdata/unary_call.in testdata/test.proto",
expectedOut: `{ "message": "hello, oumae" }`,
Expand Down Expand Up @@ -162,13 +158,15 @@ func TestE2E_OldCLI(t *testing.T) {
},
},
"call unary RPC with an input file and custom headers by CLI mode": {
args: "--header ogiso=setsuna --header touma=kazusa,youko --package api --service Example --call UnaryHeader --file testdata/unary_header.in testdata/test.proto",
args: "--header ogiso=setsuna --header touma=kazusa,youko --header sound=of=destiny --package api --service Example --call UnaryHeader --file testdata/unary_header.in testdata/test.proto",
assertTest: func(t *testing.T, output string) {
expectedStrings := []string{
"key = ogiso",
"val = setsuna",
"key = touma",
"val = kazusa, youko",
"key = sound",
"val = of=destiny",
}
for _, s := range expectedStrings {
if !strings.Contains(output, s) {
Expand Down
2 changes: 1 addition & 1 deletion meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import version "github.com/hashicorp/go-version"
const AppName = "evans"

var (
Version = version.Must(version.NewSemver("0.8.4"))
Version = version.Must(version.NewSemver("0.8.5"))
)

0 comments on commit ddeebcf

Please sign in to comment.