-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_handler_test.go
94 lines (86 loc) · 2.7 KB
/
web_handler_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
package delayed_job
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestWebHandler(t *testing.T) {
for _, test := range []struct {
method string
url string
excepted_url string
body interface{}
head1 string
head2 string
excepted_body string
excepted_error string
}{{method: "GET", url: "/aaa/bbb", head1: "head1", head2: "head2"},
{method: "GEaT", url: "/aaa/bbb", excepted_error: "unsupported http method - GEaT"},
{method: "PUT", url: "/aaa/bbb", body: "adsdfdsf", excepted_body: "adsdfdsf", head1: "head1", head2: "head2"},
{method: "PUT", url: "/aaa/bbb/{{.abc1}}/{{.abc2}}", excepted_url: "/aaa/bbb/aaa/bbb", body: "adsdfdsf", excepted_body: "adsdfdsf", head1: "head1", head2: "head2"},
{method: "PUT", url: "/aaa/bbb/{{.abc1}}/{{.abc2}}", excepted_url: "/aaa/bbb/aaa/bbb", body: "adsdfdsf{{.abc2}}", excepted_body: "adsdfdsfbbb", head1: "head1", head2: "head2"}} {
func() {
var method, url, body, head1, head2 string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
method = req.Method
url = req.URL.Path
head1 = req.Header.Get("x")
head2 = req.Header.Get("aaddd")
if "HEAD" != req.Method && "GET" != req.Method {
bs, _ := ioutil.ReadAll(req.Body)
if nil != bs {
body = string(bs)
}
}
if "PUT" == req.Method {
w.WriteHeader(http.StatusAccepted)
} else {
w.WriteHeader(http.StatusOK)
}
}))
defer srv.Close()
handler, e := newHandler(nil, map[string]interface{}{"type": "web",
"method": test.method,
"url": srv.URL + test.url,
"body": test.body,
"head.x": test.head1,
"head.aaddd": test.head2,
"arguments": map[string]interface{}{
"abc1": "aaa",
"abc2": "bbb"}})
if nil != e {
if e.Error() != test.excepted_error {
t.Error(e)
}
return
}
e = handler.Perform()
if nil != e {
if e.Error() != test.excepted_error {
t.Error(e)
}
return
}
if method != test.method {
t.Error("exepted method is ", test.method, ", but actual is", method)
}
if "" != test.excepted_url {
if url != test.excepted_url {
t.Error("exepted url is ", test.excepted_url, ", but actual is", url)
}
} else if url != test.url {
t.Error("exepted url is ", test.url, ", but actual is", url)
}
if body != test.excepted_body {
t.Error("exepted body is ", test.excepted_body, ", but actual is", body)
}
if head1 != test.head1 {
t.Error("exepted head1 is ", test.head1, ", but actual is", head1)
}
if head2 != test.head2 {
t.Error("exepted head2 is ", test.head2, ", but actual is", head2)
}
}()
}
}