-
Notifications
You must be signed in to change notification settings - Fork 0
/
echoBody_test.go
116 lines (97 loc) · 3.3 KB
/
echoBody_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
package main
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"reflect"
// "strings"
"testing"
)
// 传入空body
func TestEchoEmptyBody(t *testing.T) {
reqBody := make(map[string]interface{})
reqBodyInJson, _ := json.Marshal(reqBody)
req := httptest.NewRequest(http.MethodPost, "http:/0.0.0.0", bytes.NewBuffer(reqBodyInJson))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("host", "foo.com")
req.Header.Set("X-Echo-Ingress-Info", "false")
w := httptest.NewRecorder()
echoHandler(w, req)
res := w.Result()
defer res.Body.Close()
resBodyInJson, err := io.ReadAll(res.Body)
resBody := make(map[string]interface{})
if err != nil {
t.Errorf("expected error to be nil got %v", err)
}
err = json.Unmarshal(resBodyInJson, &resBody)
if err != nil {
t.Errorf("expected error to be nil got %v", err)
}
if !reflect.DeepEqual(resBody, reqBody) {
t.Errorf("wrong resbody: %v", string(resBodyInJson))
}
}
// 基本功能测试
func TestEchoBodyBasic(t *testing.T) {
reqBody := make(map[string]interface{})
reqBody["username"]= []string{"unamexxx"}
reqBody["password"]= []string{"pswdxxxx"}
reqBody["company"]= []string{"AliBaba", "Tencent"}
reqBodyInJson, _ := json.MarshalIndent(reqBody, "", " ")
req := httptest.NewRequest(http.MethodPost, "http:/0.0.0.0", bytes.NewBuffer(reqBodyInJson))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("host", "foo.com")
req.Header.Set("X-Echo-Ingress-Info", "false")
w := httptest.NewRecorder()
echoHandler(w, req)
res := w.Result()
defer res.Body.Close()
resBodyInJson, err := io.ReadAll(res.Body)
resBody := make(map[string]interface{})
if err != nil {
t.Errorf("expected error to be nil got %v", err)
}
err = json.Unmarshal(resBodyInJson, &resBody)
if err != nil {
t.Errorf("expected error to be nil got %v", err)
}
if !reflect.DeepEqual(string(resBodyInJson), string(reqBodyInJson)) {
t.Errorf("wrong resbody: %v", string(resBodyInJson))
}
}
// 自定义追加信息
func TestEchoBodyWithExtraSet(t *testing.T) {
reqBody := make(map[string]interface{})
reqBody["username"]= []string{"unamexxx"}
reqBody["password"]= []string{"pswdxxxx"}
reqBody["company"]= []string{"AliBaba", "Tencent"}
reqBodyInJson, _ := json.MarshalIndent(reqBody, "", " ")
req := httptest.NewRequest(http.MethodPost, "http:/0.0.0.0", bytes.NewBuffer(reqBodyInJson))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("host", "foo.com")
req.Header.Set("X-Echo-Ingress-Info", "false")
req.Header.Set("X-Echo-Set-Body", "X-not-renamed:test,X-remove:exist,X-replace:not-replaced,X-replace:not-replaced2")
reqBody["X-not-renamed"]= []string{"test"}
reqBody["X-remove"]= []string{"exist"}
reqBody["X-replace"]= []string{"not-replaced","not-replaced2"}
reqBodyInJson, _ = json.MarshalIndent(reqBody, "", " ")
w := httptest.NewRecorder()
echoHandler(w, req)
res := w.Result()
defer res.Body.Close()
resBodyInJson, err := io.ReadAll(res.Body)
resBody := make(map[string]interface{})
if err != nil {
t.Errorf("expected error to be nil got %v", err)
}
err = json.Unmarshal(resBodyInJson, &resBody)
if err != nil {
t.Errorf("expected error to be nil got %v", err)
}
if !reflect.DeepEqual(string(resBodyInJson), string(reqBodyInJson)) {
t.Errorf("wrong resbody: %v", string(resBodyInJson))
}
}