From f52aa639a7900e8a51f5689bc417e46253f0ab97 Mon Sep 17 00:00:00 2001 From: "Du, Chengbin" Date: Wed, 21 Dec 2022 15:33:19 +0800 Subject: [PATCH] Fix test cases with quote --- connector/curl_test.go | 50 +++++++++++++++++++++++++----------------- httpie/cmdline.go | 18 ++++++++------- httpie/flag.go | 3 ++- httpie/item.go | 3 +++ 4 files changed, 45 insertions(+), 29 deletions(-) diff --git a/connector/curl_test.go b/connector/curl_test.go index 065d33c..bf2f5b2 100644 --- a/connector/curl_test.go +++ b/connector/curl_test.go @@ -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 { diff --git a/httpie/cmdline.go b/httpie/cmdline.go index 62fc8e5..75af7e8 100644 --- a/httpie/cmdline.go +++ b/httpie/cmdline.go @@ -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 { @@ -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 { diff --git a/httpie/flag.go b/httpie/flag.go index 135b88e..fc7a713 100644 --- a/httpie/flag.go +++ b/httpie/flag.go @@ -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) } diff --git a/httpie/item.go b/httpie/item.go index d535690..80708fca2 100644 --- a/httpie/item.go +++ b/httpie/item.go @@ -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) }