-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestor_test.go
50 lines (46 loc) · 1.14 KB
/
requestor_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
package requestor
import (
"log"
"net/http"
"reflect"
"testing"
)
func TestRequestor_IsSuccess(t *testing.T) {
requestor := &Requestor{
RequestURI: "http://httpbin.org/anything",
Headers: http.Header {
"content-type" : {"application/json"},
"x-trace-id": {"123jadfn3829afl3"},
},
Method: "POST",
RequestBody: []byte("{\"abc\":\"hello world\"}"),
}
type ResponseStruct struct {
Origin string `json:"origin"`
Headers map[string]string `json:"headers"`
Method string `json:"method"`
Body string `json:"body"`
Data string `json:"data"`
Json interface{} `json:"json"`
File interface{} `json:"file"`
Form interface{} `json:"form"`
}
resp := ResponseStruct{}
if requestor.IsSuccess() {
requestor.DumpResponse()
log.Println(requestor.GetStatusCode())
log.Println(requestor.GetResponseHeader())
log.Println(requestor.GetBody())
err := requestor.UnmarshalBody(&resp)
if err != nil {
panic(err)
}
v := reflect.ValueOf(resp)
t := v.Type()
for i:=0; i < t.NumField(); i ++ {
log.Println(t.Field(i).Name, "=", v.Field(i))
}
} else {
log.Println("请求失败", requestor.GetStatusCode())
}
}