-
Notifications
You must be signed in to change notification settings - Fork 9
/
parse_test.go
88 lines (75 loc) · 2 KB
/
parse_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
package parse_curl
import (
"github.com/bmizerany/assert"
"testing"
)
type M map[string]interface{}
func TestParse(t *testing.T) {
addSample(t, "curl -XPUT http://api.sloths.com/sloth/4", M{
"method": "PUT",
"url": "http://api.sloths.com/sloth/4",
})
addSample(t, "curl http://api.sloths.com", M{
"method": "GET",
"url": "http://api.sloths.com",
})
addSample(t, "curl -H \"Accept-Encoding: gzip\" --compressed http://api.sloths.com", M{
"method": "GET",
"url": "http://api.sloths.com",
"header": M{
"Accept-Encoding": "gzip",
},
})
addSample(t, "curl -X DELETE http://api.sloths.com/sloth/4", M{
"method": "DELETE",
"url": "http://api.sloths.com/sloth/4",
})
addSample(t, "curl -d \"foo=bar\" https://api.sloths.com", M{
"method": "POST",
"url": "https://api.sloths.com",
"header": M{
"Content-Type": "application/x-www-form-urlencoded",
},
"body": "foo=bar",
})
addSample(t, "curl -H \"Accept: text/plain\" --header \"User-Agent: slothy\" https://api.sloths.com", M{
"method": "GET",
"url": "https://api.sloths.com",
"header": M{
"Accept": "text/plain",
"User-Agent": "slothy",
},
})
addSample(t, "curl --cookie 'species=sloth;type=galactic' slothy https://api.sloths.com", M{
"method": "GET",
"url": "https://api.sloths.com",
"header": M{
"Cookie": "species=sloth;type=galactic",
},
})
addSample(t, "curl --location --request GET 'http://api.sloths.com/users?token=admin'", M{
"method": "GET",
"url": "http://api.sloths.com/users?token=admin",
})
}
func addSample(t *testing.T, url string, exp M) {
request, _ := Parse(url)
check(t, exp, request)
}
func check(t *testing.T, exp M, got *Request) {
for key, value := range exp {
switch key {
case "method":
assert.Equal(t, value, got.Method)
case "url":
assert.Equal(t, value, got.Url)
case "body":
assert.Equal(t, value, got.Body)
case "header":
headers := value.(M)
for k, v := range headers {
assert.Equal(t, v, got.Header[k])
}
}
}
}