Skip to content

Commit

Permalink
Fix test cases with quote
Browse files Browse the repository at this point in the history
  • Loading branch information
dcb9 committed Dec 21, 2022
1 parent fc54a35 commit f52aa63
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
50 changes: 30 additions & 20 deletions connector/curl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,69 +9,79 @@ func TestCurl2Httpie(t *testing.T) {
}{
{
[]string{"curl", "--request", "POST", "--header", "Content-Type: application/json", "--data", `{"foo":"bar"}`, "https://httpbin.org/anything"},
`echo '{"foo":"bar"}' | http --json POST 'https://httpbin.org/anything'`,
`echo '{"foo":"bar"}' | https --json POST httpbin.org/anything`,
},
{
[]string{"curl", "-i", "-u", "username", "-d", `{"scopes":["public_repo"]}`, `https://api.github.com/authorizations`},
`echo '{"scopes":["public_repo"]}' | http --auth 'username' --form POST 'https://api.github.com/authorizations'`,
`echo '{"scopes":["public_repo"]}' | https --auth username --form POST api.github.com/authorizations`,
},
{
[]string{"curl", "z.cn"},
"http GET 'z.cn'",
"http GET z.cn",
}, {
[]string{"curl", "--url", "local.dev"},
"http GET 'local.dev'",
"http GET local.dev",
}, {
[]string{"curl", "--user", "user:password", "local.dev"},
`http --auth 'user:password' GET 'local.dev'`,
`http --auth user:password GET local.dev`,
}, {
[]string{"curl", "local.dev", "--user", "user:password"},
`http --auth 'user:password' GET 'local.dev'`,
`http --auth user:password GET local.dev`,
}, {
[]string{"curl", "-u", "user:password", "local.dev"},
`http --auth 'user:password' GET 'local.dev'`,
`http --auth user:password GET local.dev`,
}, {
[]string{"curl", "-u", "user:password", "-X", "POST", "local.dev"},
`http --auth 'user:password' --form POST 'local.dev'`,
`http --auth user:password --form POST local.dev`,
}, {
[]string{"curl", "-u", "user:password", "-X", "POST", "-H", "Foo: bar", "--data", "foo=bar", "local.dev"},
`http --auth 'user:password' --form POST 'local.dev' 'Foo:bar' 'foo=bar'`,
`http --auth user:password --form POST local.dev Foo:bar foo=bar`,
}, {
[]string{"curl", "-u", "user:password", "-X", "POST", "-H", "Content-Type: application/json", "--data", "foo=bar", "local.dev"},
`http --auth 'user:password' --json POST 'local.dev' 'foo=bar'`,
`http --auth user:password --json POST local.dev foo=bar`,
}, {
[]string{"curl", "-u", "user:password", "--request", "GET", "local.dev", "-H", "accept: application/json", "-H", "Authorization: jwtToken"},
`http --auth 'user:password' --json GET 'local.dev' 'Authorization:jwtToken'`,
`http --auth user:password --json GET local.dev Authorization:jwtToken`,
}, {
[]string{"curl", "-u", "user:", "local.dev"},
`http --auth 'user:' GET 'local.dev'`,
`http --auth user: GET local.dev`,
}, {
[]string{"curl", "local.dev", "-u", "user:", "-d", "foo=Bar"},
`http --auth 'user:' --form POST 'local.dev' 'foo=Bar'`,
`http --auth user: --form POST local.dev foo=Bar`,
}, {
[]string{"curl", "local.dev", "--user-agent", "httpie", "-u", "user:", "-d", "foo=Bar"},
`http --auth 'user:' --form POST 'local.dev' 'User-Agent:httpie' 'foo=Bar'`,
`http --auth user: --form POST local.dev User-Agent:httpie foo=Bar`,
}, {
[]string{"curl", "local.dev", "--referer", "z.cn", "--user-agent", "httpie", "-u", "user:", "-d", "foo=Bar"},
`http --auth 'user:' --form POST 'local.dev' 'Referer:z.cn' 'User-Agent:httpie' 'foo=Bar'`,
`http --auth user: --form POST local.dev Referer:z.cn User-Agent:httpie foo=Bar`,
}, {
[]string{"curl", "local.dev", "-v", "--referer", "z.cn", "--user-agent", "httpie", "-u", "user:", "-d", "foo=Bar"},
`http --verbose --auth 'user:' --form POST 'local.dev' 'Referer:z.cn' 'User-Agent:httpie' 'foo=Bar'`,
`http --verbose --auth user: --form POST local.dev Referer:z.cn User-Agent:httpie foo=Bar`,
}, {
[]string{"curl", "local.dev", "--verbose", "--referer", "z.cn", "--user-agent", "httpie", "-u", "user:", "-d", "foo=Bar"},
`http --verbose --auth 'user:' --form POST 'local.dev' 'Referer:z.cn' 'User-Agent:httpie' 'foo=Bar'`,
`http --verbose --auth user: --form POST local.dev Referer:z.cn User-Agent:httpie foo=Bar`,
}, {
[]string{"curl", "local.dev", "--verbose", "--cookie", "NAME=VAL", "--referer", "z.cn", "--user-agent", "httpie", "-u", "user:", "-d", "foo=Bar"},
`http --verbose --auth 'user:' --form POST 'local.dev' 'Cookie:NAME=VAL' 'Referer:z.cn' 'User-Agent:httpie' 'foo=Bar'`,
`http --verbose --auth user: --form POST local.dev Cookie:NAME=VAL Referer:z.cn User-Agent:httpie foo=Bar`,
}, {
[]string{"curl", "-H", "Host: foo.bar.com", "-H", "Accept: */*", "-H", "User-Agent: debug-MyAppName/ CFNetwork/893.14 Darwin/17.4.0", "-H", "Accept-Language: en-us", "--data", "client_id=foobarfoobarfoobar&client_secret=bazquzbazquz&grant_type=password&password=SomePasswordHere&scope=user&username=first.last%2B1%40domain.com", "--compressed", "https://stage.buildsafely.com/api/oauth/token"},
`http --form POST 'https://stage.buildsafely.com/api/oauth/token' 'Host:foo.bar.com' 'Accept:*/*' 'User-Agent:debug-MyAppName/ CFNetwork/893.14 Darwin/17.4.0' 'Accept-Language:en-us' 'client_id=foobarfoobarfoobar&client_secret=bazquzbazquz&grant_type=password&password=SomePasswordHere&scope=user&username=first.last%2B1%40domain.com'`,
`https --form POST stage.buildsafely.com/api/oauth/token Host:foo.bar.com 'Accept:*/*' 'User-Agent:debug-MyAppName/ CFNetwork/893.14 Darwin/17.4.0' Accept-Language:en-us client_id=foobarfoobarfoobar client_secret=bazquzbazquz grant_type=password password=SomePasswordHere scope=user username=first.last%2B1%40domain.com`,
}, {
[]string{"curl", `https://xxx/v1/tokens`, "-H", `User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0`, "-H", `Accept: application/vnd.api+json`, "-H", `Accept-Language: en-US,en;q=0.5`, "--compressed", "-H", `Content-Type: application/vnd.api+json`, "-H", `Origin: https://xxx`, "-H", `Connection: keep-alive`, "-H", `Referer: https://xxx`, "-H", `Pragma: no-cache`, "-H", `Cache-Control: no-cache`, "--data", `{"auth":{"email":"xxx","password":"xxx"}}`},
`echo '{"auth":{"email":"xxx","password":"xxx"}}' | http --json POST 'https://xxx/v1/tokens' 'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0' 'Accept-Language:en-US,en;q=0.5' 'Origin:https://xxx' 'Connection:keep-alive' 'Referer:https://xxx' 'Pragma:no-cache' 'Cache-Control:no-cache'`,
`echo '{"auth":{"email":"xxx","password":"xxx"}}' | https --json POST xxx/v1/tokens 'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0' Accept-Language:en-US,en;q=0.5 Origin:https://xxx Connection:keep-alive Referer:https://xxx Pragma:no-cache Cache-Control:no-cache`,
},
}

// cases = []struct {
// in []string
// want string
// }{
// {
// []string{"curl", "local.dev", "--verbose", "--cookie", "NAME=VAL", "--referer", "z.cn", "--user-agent", "httpie", "-u", "user:", "-d", "foo=Bar"},
// `http --verbose --auth user: --form POST local.dev Cookie:NAME=VAL Referer:z.cn User-Agent:httpie foo=Bar`,
// },
// }

for _, c := range cases {
gotStringer, warningMessages, err := Curl2Httpie(c.in[1:])
if len(warningMessages) > 0 {
Expand Down
18 changes: 10 additions & 8 deletions httpie/cmdline.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,21 @@ func (cl *CmdLine) AddItem(i *Item) {
cl.Items = append(cl.Items, i)
}

func addQuoteIfNeeded(s string) string {
specialCharIndex := strings.IndexFunc(s, func(r rune) bool {
func needQuote(s string) bool {
return -1 != strings.IndexFunc(s, func(r rune) bool {
switch r {
case '&', '@', '#', '[', ']', '{', '}':
case '&', '@', '#', '[', ']', '{', '}', ' ', '(', ')', '*':
return true
}
return false
})
if -1 == specialCharIndex {
return fmt.Sprintf("%s", s)
}
}

return fmt.Sprintf("'%s'", s)
func addQuoteIfNeeded(s string) string {
if needQuote(s) {
return fmt.Sprintf("'%s'", s)
}
return fmt.Sprintf("%s", s)
}

func (cl *CmdLine) String() string {
Expand All @@ -65,7 +67,7 @@ func (cl *CmdLine) String() string {

// default flag
for _, v := range cl.Flags {
s = append(s, addQuoteIfNeeded(v.String()))
s = append(s, v.String())
}

if cl.Method == nil {
Expand Down
3 changes: 2 additions & 1 deletion httpie/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func (f *Flag) String() string {
if f.Separator == "" {
f.Separator = " " // Use whitespace as default separator
}
arg = fmt.Sprintf(`%s%s`, f.Separator, f.Arg)
arg = fmt.Sprintf(`%s%s`, f.Separator, addQuoteIfNeeded(f.Arg))
}

return fmt.Sprintf("--%s%s", f.Long, arg)
}

Expand Down
3 changes: 3 additions & 0 deletions httpie/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type Item struct {
}

func (i *Item) String() string {
if needQuote(i.V) {
return fmt.Sprintf(`'%s%s%s'`, i.K, i.S, i.V)
}
return fmt.Sprintf(`%s%s%s`, i.K, i.S, i.V)
}

Expand Down

0 comments on commit f52aa63

Please sign in to comment.