| 1 | package http2curl |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "io/ioutil" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "net/url" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | ) |
| 14 | |
| 15 | func ExampleGetCurlCommand() { |
| 16 | form := url.Values{} |
| 17 | form.Add("age", "10") |
| 18 | form.Add("name", "Hudson") |
| 19 | body := form.Encode() |
| 20 | |
| 21 | req, _ := http.NewRequest(http.MethodPost, "http://foo.com/cats", ioutil.NopCloser(bytes.NewBufferString(body))) |
| 22 | req.Header.Set("API_KEY", "123") |
| 23 | |
| 24 | command, _ := GetCurlCommand(req) |
| 25 | fmt.Println(command) |
| 26 | |
| 27 | // Output: |
| 28 | // curl -X 'POST' -d 'age=10&name=Hudson' -H 'Api_key: 123' 'http://foo.com/cats' |
| 29 | } |
| 30 | |
| 31 | func ExampleGetCurlCommand_json() { |
| 32 | req, _ := http.NewRequest("PUT", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString(`{"hello":"world","answer":42}`)) |
| 33 | req.Header.Set("Content-Type", "application/json") |
| 34 | |
| 35 | command, _ := GetCurlCommand(req) |
| 36 | fmt.Println(command) |
| 37 | |
| 38 | // Output: |
| 39 | // curl -X 'PUT' -d '{"hello":"world","answer":42}' -H 'Content-Type: application/json' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu' |
| 40 | } |
| 41 | |
| 42 | func ExampleGetCurlCommand_slice() { |
| 43 | // See https://github.com/moul/http2curl/issues/12 |
| 44 | req, _ := http.NewRequest("PUT", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString(`{"hello":"world","answer":42}`)) |
| 45 | req.Header.Set("Content-Type", "application/json") |
| 46 | |
| 47 | command, _ := GetCurlCommand(req) |
| 48 | fmt.Println(strings.Join(*command, " \\\n ")) |
| 49 | |
| 50 | // Output: |
| 51 | // curl \ |
| 52 | // -X \ |
| 53 | // 'PUT' \ |
| 54 | // -d \ |
| 55 | // '{"hello":"world","answer":42}' \ |
| 56 | // -H \ |
| 57 | // 'Content-Type: application/json' \ |
| 58 | // 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu' |
| 59 | } |
| 60 | |
| 61 | func ExampleGetCurlCommand_noBody() { |
| 62 | req, _ := http.NewRequest("PUT", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", nil) |
| 63 | req.Header.Set("Content-Type", "application/json") |
| 64 | |
| 65 | command, _ := GetCurlCommand(req) |
| 66 | fmt.Println(command) |
| 67 | |
| 68 | // Output: |
| 69 | // curl -X 'PUT' -H 'Content-Type: application/json' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu' |
| 70 | } |
| 71 | |
| 72 | func ExampleGetCurlCommand_emptyStringBody() { |
| 73 | req, _ := http.NewRequest("PUT", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString("")) |
| 74 | req.Header.Set("Content-Type", "application/json") |
| 75 | |
| 76 | command, _ := GetCurlCommand(req) |
| 77 | fmt.Println(command) |
| 78 | |
| 79 | // Output: |
| 80 | // curl -X 'PUT' -H 'Content-Type: application/json' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu' |
| 81 | } |
| 82 | |
| 83 | func ExampleGetCurlCommand_newlineInBody() { |
| 84 | req, _ := http.NewRequest("POST", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString("hello\nworld")) |
| 85 | req.Header.Set("Content-Type", "application/json") |
| 86 | |
| 87 | command, _ := GetCurlCommand(req) |
| 88 | fmt.Println(command) |
| 89 | |
| 90 | // Output: |
| 91 | // curl -X 'POST' -d 'hello |
| 92 | // world' -H 'Content-Type: application/json' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu' |
| 93 | } |
| 94 | |
| 95 | func ExampleGetCurlCommand_specialCharsInBody() { |
| 96 | req, _ := http.NewRequest("POST", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString(`Hello $123 o'neill -"-`)) |
| 97 | req.Header.Set("Content-Type", "application/json") |
| 98 | |
| 99 | command, _ := GetCurlCommand(req) |
| 100 | fmt.Println(command) |
| 101 | |
| 102 | // Output: |
| 103 | // curl -X 'POST' -d 'Hello $123 o'\''neill -"-' -H 'Content-Type: application/json' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu' |
| 104 | } |
| 105 | |
| 106 | func ExampleGetCurlCommand_other() { |
| 107 | uri := "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu" |
| 108 | payload := new(bytes.Buffer) |
| 109 | payload.Write([]byte(`{"hello":"world","answer":42}`)) |
| 110 | req, err := http.NewRequest("PUT", uri, payload) |
| 111 | if err != nil { |
| 112 | panic(err) |
| 113 | } |
| 114 | req.Header.Set("X-Auth-Token", "private-token") |
| 115 | req.Header.Set("Content-Type", "application/json") |
| 116 | |
| 117 | command, err := GetCurlCommand(req) |
| 118 | if err != nil { |
| 119 | panic(err) |
| 120 | } |
| 121 | fmt.Println(command) |
| 122 | // Output: curl -X 'PUT' -d '{"hello":"world","answer":42}' -H 'Content-Type: application/json' -H 'X-Auth-Token: private-token' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu' |
| 123 | } |
| 124 | |
| 125 | func ExampleGetCurlCommand_https() { |
| 126 | uri := "https://www.example.com/abc/def.ghi?jlk=mno&pqr=stu" |
| 127 | payload := new(bytes.Buffer) |
| 128 | payload.Write([]byte(`{"hello":"world","answer":42}`)) |
| 129 | req, err := http.NewRequest("PUT", uri, payload) |
| 130 | if err != nil { |
| 131 | panic(err) |
| 132 | } |
| 133 | req.Header.Set("X-Auth-Token", "private-token") |
| 134 | req.Header.Set("Content-Type", "application/json") |
| 135 | |
| 136 | command, err := GetCurlCommand(req) |
| 137 | if err != nil { |
| 138 | panic(err) |
| 139 | } |
| 140 | fmt.Println(command) |
| 141 | // Output: curl -k -X 'PUT' -d '{"hello":"world","answer":42}' -H 'Content-Type: application/json' -H 'X-Auth-Token: private-token' 'https://www.example.com/abc/def.ghi?jlk=mno&pqr=stu' |
| 142 | } |
| 143 | |
| 144 | // Benchmark test for GetCurlCommand |
| 145 | func BenchmarkGetCurlCommand(b *testing.B) { |
| 146 | form := url.Values{} |
| 147 | |
| 148 | for i := 0; i <= b.N; i++ { |
| 149 | form.Add("number", strconv.Itoa(i)) |
| 150 | body := form.Encode() |
| 151 | req, _ := http.NewRequest(http.MethodPost, "http://foo.com", ioutil.NopCloser(bytes.NewBufferString(body))) |
| 152 | _, err := GetCurlCommand(req) |
| 153 | if err != nil { |
| 154 | panic(err) |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | func TestGetCurlCommand_serverSide(t *testing.T) { |
| 160 | svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 161 | c, err := GetCurlCommand(r) |
| 162 | if err != nil { |
| 163 | t.Error(err) |
| 164 | } |
| 165 | fmt.Fprint(w, c.String()) |
| 166 | })) |
| 167 | defer svr.Close() |
| 168 | |
| 169 | resp, err := http.Get(svr.URL) |
| 170 | if err != nil { |
| 171 | t.Error(err) |
| 172 | } |
| 173 | defer resp.Body.Close() |
| 174 | data, err := ioutil.ReadAll(resp.Body) |
| 175 | if err != nil { |
| 176 | t.Error(err) |
| 177 | } |
| 178 | |
| 179 | exp := fmt.Sprintf("curl -X 'GET' -H 'Accept-Encoding: gzip' -H 'User-Agent: Go-http-client/1.1' '%s/'", svr.URL) |
| 180 | if out := string(data); out != exp { |
| 181 | t.Errorf("act: %s, exp: %s", out, exp) |
| 182 | } |
| 183 | } |