-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtuncurl_test.go
141 lines (136 loc) · 5.37 KB
/
tuncurl_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package uncurl
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"testing"
)
func headerEq(a, b http.Header) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
bv, present := b[k]
if !present {
return false
}
if len(bv) != len(v) {
return false
}
for x := 0; x < len(v); x++ {
if bv[x] != v[x] {
return false
}
}
}
return true
}
func TestNew(t *testing.T) {
tests := []struct {
curl string
target string
header http.Header
method string
ae string
body []byte
}{
{
`curl 'https://www.wunderground.com/forecast/us/ma/waltham' -H 'authority: www.wunderground.com' -H 'upgrade-insecure-requests: 1' -H 'user-agent: Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36' -H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' -H 'sec-fetch-site: none' -H 'sec-fetch-mode: navigate' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: en-US,en;q=0.9' --compressed`,
"https://www.wunderground.com/forecast/us/ma/waltham",
http.Header{
"authority": []string{"www.wunderground.com"},
"upgrade-insecure-requests": []string{"1"},
"user-agent": []string{"Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"},
"accept": []string{"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"},
"sec-fetch-site": []string{"none"},
"sec-fetch-mode": []string{"navigate"},
"accept-language": []string{"en-US,en;q=0.9"},
},
`GET`,
"gzip, deflate, br",
nil,
},
{
`curl 'https://privnote.com/legacy/' -H 'Connection: keep-alive' -H 'Origin: https://privnote.com' -H 'X-Requested-With: XMLHttpRequest' -H 'User-Agent: Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36' -H 'Content-type: application/x-www-form-urlencoded' -H 'Accept: */*' -H 'Sec-Fetch-Site: same-origin' -H 'Sec-Fetch-Mode: cors' -H 'Referer: https://privnote.com/' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.9' --data '&data=U2FsdGVkX1%2BOxTSDTgLVqVwnRWjcvJ8AVWWZJkN456o%3D%0A&has_manual_pass=false&duration_hours=0&dont_ask=false&data_type=T¬ify_email=¬ify_ref=' --compressed`,
"https://privnote.com/legacy/",
http.Header{
"Connection": []string{"keep-alive"},
"Origin": []string{"https://privnote.com"},
"X-Requested-With": []string{"XMLHttpRequest"},
"User-Agent": []string{"Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"},
"Content-type": []string{"application/x-www-form-urlencoded"},
"Accept": []string{"*/*"},
"Sec-Fetch-Site": []string{"same-origin"},
"Sec-Fetch-Mode": []string{"cors"},
"Referer": []string{"https://privnote.com/"},
"Accept-Language": []string{"en-US,en;q=0.9"},
},
`POST`,
"gzip, deflate, br",
[]byte(`&data=U2FsdGVkX1%2BOxTSDTgLVqVwnRWjcvJ8AVWWZJkN456o%3D%0A&has_manual_pass=false&duration_hours=0&dont_ask=false&data_type=T¬ify_email=¬ify_ref=`),
},
}
for i, test := range tests {
un, err := NewString(test.curl)
if err != nil {
t.Errorf("Error uncurling %d: %s", i, err)
}
if un == nil {
t.Errorf("un is nil in test %d", i)
}
if un.String() != test.curl {
t.Errorf("curl string mismatch at test %d", i)
}
if un.Target() != test.target {
t.Errorf("Target mismatch in test %d: expected %s, got %s", i, test.target, un.Target())
}
if !headerEq(test.header, un.Header()) {
t.Errorf("Headers not equal in test %d", i)
}
if un.method != test.method {
t.Errorf("Methods not equal in test %d: expected %s, got %s", i, test.method, un.method)
}
if un.Method() != test.method {
t.Errorf("un.Method() mismatch at test %d", i)
}
if un.AcceptEncoding != test.ae {
t.Errorf("accept-encoding mismatch in test %d: expected %s, got %s", i, test.ae, un.AcceptEncoding)
}
r := un.Request()
requestTest(t, i, un, test.header, test.method, test.body, r)
r, err = un.NewRequest(un.Method(), un.Target(), bytes.NewBuffer(test.body))
if err != nil {
t.Errorf("NewRequest error in test %d: %s", i, err)
}
requestTest(t, i, un, test.header, test.method, test.body, r)
r, err = un.NewRequestWithContext(context.Background(), un.Method(), un.Target(), bytes.NewBuffer(test.body))
if err != nil {
t.Errorf("NewRequestWithContext error in test %d: %s", i, err)
}
requestTest(t, i, un, test.header, test.method, test.body, r)
}
}
func requestTest(t *testing.T, i int, un *Uncurl, th http.Header, tm string, tb []byte, r *http.Request) {
if !headerEq(th, r.Header) {
t.Errorf("r.Header mismatch in test %d", i)
}
if r.Method != tm {
t.Errorf("r.Method mismatch in test %d", i)
}
if tb != nil {
bodyTest(t, i, un, tb, r)
}
}
func bodyTest(t *testing.T, i int, un *Uncurl, tb []byte, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("error reading body in test %d: %s", i, err)
}
if !bytes.Equal(tb, b) {
t.Errorf("body mismatch at test %d", i)
}
if !bytes.Equal(tb, un.Body()) {
t.Errorf("un.Body() mismatch at test %d", i)
}
}