8000 Add option WithDoubleQuotes by aoliveti · Pull Request #9 · aoliveti/curling · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add option WithDoubleQuotes #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ The following Go code demonstrates how to create a command from an HTTP request
```go
req, err := http.NewRequest(http.MethodGet, "https://www.google.com", nil)
if err != nil {
panic(err)
panic(err)
}
req.Header.Add("If-None-Match", "foo")

cmd, err := curling.NewFromRequest(req, curling.WithCompression())
if err != nil {
panic(err)
panic(err)
}

fmt.Println(cmd)
Expand Down Expand Up @@ -56,6 +56,7 @@ When creating a new command, you can provide these options:
| WithMultiLine() | Generates a multiline snippet for unix-like shell |
| WithWindowsMultiLine() | Generates a multiline snippet for Windows shell |
| WithPowerShellMultiLine() | Generates a multiline snippet for PowerShell |
| WithDoubleQuotes() | Uses double quotes to escape characters |

## License

Expand Down
36 changes: 21 additions & 15 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ type Command struct {
tokens []string
lineContinuation string

location bool
compressed bool
insecure bool
useLongForm bool
useMultiLine bool
silent bool
location bool
compressed bool
insecure bool
useLongForm bool
useMultiLine bool
silent bool
useDoubleQuotes bool
}

func NewFromRequest(r *http.Request, opts ...Option) (*Command, error) {
Expand Down Expand Up @@ -54,6 +55,16 @@ func (c *Command) optionForm(short, long string) string {
return short
}

func (c *Command) escape(s string) string {
if c.useDoubleQuotes {
v := strings.ReplaceAll(s, "\"", "\\\"")
return fmt.Sprintf("\"%s\"", v)
}

v := strings.ReplaceAll(s, "'", "'\\''")
return fmt.Sprintf("'%s'", v)
}

func (c *Command) build(r *http.Request, opts ...Option) error {
for _, opt := range opts {
opt(c)
Expand Down Expand Up @@ -105,8 +116,8 @@ func (c *Command) buildCommand(r *http.Request) {
c.appendToken(
command,
c.optionForm("-X", "--request"),
escape(method),
escape(r.URL.String()),
c.escape(method),
c.escape(r.URL.String()),
)
}

Expand All @@ -124,7 +135,7 @@ func (c *Command) buildHeaders(r *http.Request) {
for _, header := range headers {
c.appendToken(
c.optionForm("-H", "--header"),
escape(header),
c.escape(header),
)
}
}
Expand All @@ -141,13 +152,8 @@ func (c *Command) buildData(r *http.Request) error {
r.Body = io.NopCloser(bytes.NewBuffer(b.Bytes()))

option := c.optionForm("-d", "--data")
c.appendToken(option, escape(b.String()))
c.appendToken(option, c.escape(b.String()))
}

return nil
}

func escape(s string) string {
v := strings.ReplaceAll(s, "'", "'\\''")
return fmt.Sprintf("'%s'", v)
}
16 changes: 16 additions & 0 deletions command_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@ func Test_NewFromRequest_options(t *testing.T) {
},
wantErr: false,
},
{
name: "double quotes option",
args: args{
r: &http.Request{
URL: testUrl,
},
opts: []Option{WithDoubleQuotes()},
},
want: &Command{
tokens: []string{
"curl -X \"GET\" \"https://localhost/test\"",
},
useDoubleQuotes: true,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
8000
119 changes: 79 additions & 40 deletions command_test.go
95FD
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,6 @@ import (
"testing"
)

func Test_escape(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want string
}{
{
name: "without single quotes",
args: args{
s: "",
},
want: "''",
},
{
name: "with one single quotes",
args: args{
s: "'",
},
want: "''\\'''",
},
{
name: "with two single quotes",
args: args{
s: "'v'",
},
want: "''\\''v'\\'''",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := escape(tt.args.s); got != tt.want {
t.Errorf("escape() = %v, want %v", got, tt.want)
}
})
}
}

func TestCommand_String(t *testing.T) {
type fields struct {
tokens []string
Expand Down Expand Up @@ -169,6 +129,85 @@ func TestCommand_optionForm(t *testing.T) {
}
}

func TestCommand_escape(t *testing.T) {
type fields struct {
tokens []string
useDoubleQuotes bool
}
type args struct {
s string
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "without single quotes",
args: args{
s: "",
},
want: "''",
},
{
name: "with one single quotes",
args: args{
s: "'",
},
want: "''\\'''",
},
{
name: "with two single quotes",
args: args{
s: "'v'",
},
want: "''\\''v'\\'''",
},
{
name: "without double quotes",
fields: fields{
useDoubleQuotes: true,
},
args: args{
s: "",
},
want: "\"\"",
},
{
name: "with one double quotes",
fields: fields{
useDoubleQuotes: true,
},
args: args{
s: "\"",
},
want: "\"\\\"\"",
},
{
name: "with two double quotes",
fields: fields{
useDoubleQuotes: true,
},
args: args{
s: "\"v\"",
},
want: "\"\\\"v\\\"\"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Command{
tokens: tt.fields.tokens,
useDoubleQuotes: tt.fields.useDoubleQuotes,
}
if got := c.escape(tt.args.s); got != tt.want {
t.Errorf("escape() = %v, want %v", got, tt.want)
}
})
}
}

func Test_NewFromRequest(t *testing.T) {
type args struct {
r *http.Request
Expand Down
6 changes: 6 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@ func WithPowerShellMultiLine() Option {
curling.lineContinuation = lineContinuationPowerShell
}
}

func WithDoubleQuotes() Option {
return func(curling *Command) {
curling.useDoubleQuotes = true
}
}
0