Skip to content
Open
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
16 changes: 11 additions & 5 deletions http2curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@ func GetCurlCommand(req *http.Request) (*CurlCommand, error) {

schema := req.URL.Scheme
requestURL := req.URL.String()
if schema == "" {
schema = "http"
if req.TLS != nil {
schema = "https"
host := req.URL.Host
if len(host) == 0 {
if len(req.Host) > 0 {
host = req.Host
}
requestURL = schema + "://" + req.Host + req.URL.Path
if schema == "" {
schema = "http"
if req.TLS != nil {
schema = "https"
}
}
requestURL = schema + "://" + host + requestURL
}

if schema == "https" {
Expand Down
64 changes: 52 additions & 12 deletions http2curl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ func ExampleGetCurlCommand() {
}

func ExampleGetCurlCommand_json() {
req, _ := http.NewRequest("PUT", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString(`{"hello":"world","answer":42}`))
req, _ := http.NewRequest(
"PUT", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu",
bytes.NewBufferString(`{"hello":"world","answer":42}`),
)
req.Header.Set("Content-Type", "application/json")

command, _ := GetCurlCommand(req)
Expand All @@ -41,7 +44,10 @@ func ExampleGetCurlCommand_json() {

func ExampleGetCurlCommand_slice() {
// See https://github.com/moul/http2curl/issues/12
req, _ := http.NewRequest("PUT", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString(`{"hello":"world","answer":42}`))
req, _ := http.NewRequest(
"PUT", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu",
bytes.NewBufferString(`{"hello":"world","answer":42}`),
)
req.Header.Set("Content-Type", "application/json")

command, _ := GetCurlCommand(req)
Expand Down Expand Up @@ -82,7 +88,9 @@ func ExampleGetCurlCommand_emptyStringBody() {
}

func ExampleGetCurlCommand_newlineInBody() {
req, _ := http.NewRequest("POST", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString("hello\nworld"))
req, _ := http.NewRequest(
"POST", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString("hello\nworld"),
)
req.Header.Set("Content-Type", "application/json")

command, _ := GetCurlCommand(req)
Expand All @@ -94,7 +102,9 @@ func ExampleGetCurlCommand_newlineInBody() {
}

func ExampleGetCurlCommand_specialCharsInBody() {
req, _ := http.NewRequest("POST", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString(`Hello $123 o'neill -"-`))
req, _ := http.NewRequest(
"POST", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString(`Hello $123 o'neill -"-`),
)
req.Header.Set("Content-Type", "application/json")

command, _ := GetCurlCommand(req)
Expand Down Expand Up @@ -158,13 +168,17 @@ func BenchmarkGetCurlCommand(b *testing.B) {
}

func TestGetCurlCommand_serverSide(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := GetCurlCommand(r)
if err != nil {
t.Error(err)
}
fmt.Fprint(w, c.String())
}))
svr := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
c, err := GetCurlCommand(r)
if err != nil {
t.Error(err)
}
fmt.Fprint(w, c.String())
},
),
)
defer svr.Close()

resp, err := http.Get(svr.URL)
Expand All @@ -177,8 +191,34 @@ func TestGetCurlCommand_serverSide(t *testing.T) {
t.Error(err)
}

exp := fmt.Sprintf("curl -X 'GET' -H 'Accept-Encoding: gzip' -H 'User-Agent: Go-http-client/1.1' '%s/' --compressed", svr.URL)
exp := fmt.Sprintf(
"curl -X 'GET' -H 'Accept-Encoding: gzip' -H 'User-Agent: Go-http-client/1.1' '%s/' --compressed", svr.URL,
)
if out := string(data); out != exp {
t.Errorf("act: %s, exp: %s", out, exp)
}
}

func ExampleGetCurlCommand_noSchema() {
req, _ := http.NewRequest("PUT", "www.example.com/abc/def.ghi?jlk=mno&pqr=stu", nil)
req.Header.Set("Content-Type", "application/json")

command, _ := GetCurlCommand(req)
fmt.Println(command)

// Output:
// curl -X 'PUT' -H 'Content-Type: application/json' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu' --compressed
}

func ExampleGetCurlCommand_noHost() {
req, _ := http.NewRequest("PUT", "/abc/def.ghi?jlk=mno&pqr=stu", nil)
req.Header.Set("Content-Type", "application/json")
// it could happen, host is set in HTTP/2 pseudo-header field ":authority"
req.Host = "www.example.com"

command, _ := GetCurlCommand(req)
fmt.Println(command)

// Output:
// curl -X 'PUT' -H 'Content-Type: application/json' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu' --compressed
}