-
Notifications
You must be signed in to change notification settings - Fork 19
/
gencurl_test.go
117 lines (101 loc) · 3.03 KB
/
gencurl_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package gencurl
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"
)
func TestFromRequest(t *testing.T) {
urlStr := "http://example.com"
data := []byte(`{"key":"value"}`)
body := bytes.NewBuffer(data)
method := "POST"
req, err := http.NewRequest(method, urlStr, body)
if err != nil {
t.Fatal(err)
}
headerContentType := "Content-Type"
contentType := "application/json"
headerXCustom := "X-Custom"
xCustom1 := `{"json":"data"}`
xCustom2 := "more data"
req.Header.Add(headerContentType, contentType)
req.Header.Add(headerXCustom, xCustom1)
req.Header.Add(headerXCustom, xCustom2)
curl := FromRequest(req)
t.Log("Generated Curl: " + curl)
// be sure to capture your generated curl from your request before
// executing the request if you want to capture the post body as the
// execution of the request will drain the reader for the post body
/*
c := http.Client{}
resp, err := c.Do(req)
if err != nil {
t.Fatalf("unable to process http request - %s", err)
}
defer resp.Body.Close()
*/
if want := fmt.Sprintf("-X %s", method); !strings.Contains(curl, want) {
t.Errorf("missing ", want)
}
if want := fmt.Sprintf("--header '%s: %s'", headerContentType, contentType); !strings.Contains(curl, want) {
t.Errorf("missing ", want)
}
if want := fmt.Sprintf("--header '%s: %s'", headerXCustom, xCustom1); !strings.Contains(curl, want) {
t.Errorf("missing ", want)
}
if want := fmt.Sprintf("--header '%s: %s'", headerXCustom, xCustom2); !strings.Contains(curl, want) {
t.Errorf("missing ", want)
}
if want := fmt.Sprintf("-d '%s'", string(data)); !strings.Contains(curl, want) {
t.Errorf("missing ", want)
}
// Check the body was not emptied
bytes, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Errorf("expected no errors reading body, got %s", err)
}
if len(bytes) == 0 {
t.Errorf("expected body to not be drained.")
}
}
func TestFromParams(t *testing.T) {
urlStr := "http://www.example.com"
data := url.Values{"key": {"value"}}
_, err := http.PostForm(urlStr, data)
if err != nil {
t.Fatal(err)
}
curl := FromParams("POST", urlStr, data.Encode(), http.Header{})
t.Log(curl)
if want := fmt.Sprintf("-X POST"); !strings.Contains(curl, want) {
t.Errorf("missing %s", want)
}
}
func TestFromParamsWithNoDataNoHeaders(t *testing.T) {
urlStr := "http://www.example.com"
curl := FromParams("GET", urlStr, "", nil)
t.Log(curl)
if want := fmt.Sprintf("-X GET"); !strings.Contains(curl, want) {
t.Errorf("missing %s", want)
}
}
func TestFromParamsWithHeaders(t *testing.T) {
urlStr := "http://www.example.com"
data := url.Values{"key": {"value"}}
_, err := http.PostForm(urlStr, data)
if err != nil {
t.Fatal(err)
}
curl := FromParams("POST", urlStr, data.Encode(), http.Header{"Content-Type": []string{"application/json"}})
t.Log(curl)
if want := fmt.Sprintf("-X POST"); !strings.Contains(curl, want) {
t.Errorf("missing %s", want)
}
if want := fmt.Sprintf("--header 'Content-Type: application/json'"); !strings.Contains(curl, want) {
t.Errorf("missing %s", want)
}
}