-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo-service_test.go
180 lines (151 loc) · 5.28 KB
/
echo-service_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestEchoServiceGet(t *testing.T) {
responseJson, err := doTestRequest(http.MethodGet, "https://funkyTestServer.org/foo/bar?query1=key1", nil, "")
if err != nil {
t.Fatalf("Cannot create test request: %v", err)
}
httpJso := responseJson["http"].(map[string]interface{})
requestJso := responseJson["request"].(map[string]interface{})
if !strings.EqualFold(requestJso["path"].(string), "/foo/bar") {
t.Fail()
}
if !strings.EqualFold(httpJso["method"].(string), "GET") {
t.Fail()
}
if !strings.EqualFold(responseJson["instanceName"].(string), "internalTest") {
t.Fail()
}
}
func TestEchoServicePostWithJson(t *testing.T) {
type inJson struct {
Id int `json:"id"`
Name string `json:"name"`
}
in := inJson{42, "Lutz"}
b, err := json.Marshal(in)
if err != nil {
t.Fatalf("Cannot create json: %v", err)
}
responseJson, err := doTestRequest(http.MethodPost, "https://funkyTestServer.org/foo/bar?query1=key1", bytes.NewBuffer(b), "application/json")
if err != nil {
t.Fatalf("Cannot create test request: %v", err)
}
requestJso := responseJson["request"].(map[string]interface{})
body := requestJso["body"].(map[string]interface{})
if !strings.EqualFold("Lutz", body["name"].(string)) {
t.Error("Name not expected")
}
if 42 != body["id"].(float64) {
t.Error("Id not expected")
}
if !strings.EqualFold(responseJson["instanceName"].(string), "internalTest") {
t.Fail()
}
}
func TestEchoServicePostWithText(t *testing.T) {
responseJson, err := doTestRequest(http.MethodPost, "https://funkyTestServer.org/foo/bar?query1=key1", strings.NewReader("Hello Echo"), "plain/text")
if err != nil {
t.Fatalf("Cannot create test request: %v", err)
}
requestJso := responseJson["request"].(map[string]interface{})
if !strings.EqualFold("Hello Echo", requestJso["body"].(string)) {
t.Error("Wrong body")
}
if !strings.EqualFold(responseJson["instanceName"].(string), "internalTest") {
t.Fail()
}
}
func TestJWT(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "https://funkyTestServer.org/foo/bar?query1=key1", nil)
w := httptest.NewRecorder()
r.Header.Set("Authorization", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJjdXN0b21DbGFpbSI6IkZvbyJ9.WSt10RZi2AGyQfYsR2AyeH6yUwG89hWsX-cZyVNYMTU")
handleJwtReply(t, w, r)
r = httptest.NewRequest(http.MethodGet, "https://funkyTestServer.org/foo/bar?query1=key1", nil)
w = httptest.NewRecorder()
r.Header.Set("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJjdXN0b21DbGFpbSI6IkZvbyJ9.WSt10RZi2AGyQfYsR2AyeH6yUwG89hWsX-cZyVNYMTU")
handleJwtReply(t, w, r)
}
func handleJwtReply(t *testing.T, w *httptest.ResponseRecorder, r *http.Request) {
handleRequest(w, r)
res := w.Result()
contentType := res.Header.Get("Content-Type")
if !strings.EqualFold(contentType, "application/json") {
t.Fatalf("invalid Contant-Type: %v", contentType)
}
var responseJson map[string]interface{}
err := json.NewDecoder(res.Body).Decode(&responseJson)
if err != nil {
t.Fatalf("JWT error %v", err)
}
authorization := responseJson["authorization"].(map[string]interface{})
if authorization == nil {
t.Fatalf("Authorization is nil.")
}
payload := authorization["payload"].(map[string]interface{})
header := authorization["header"].(map[string]interface{})
if payload == nil {
t.Fatalf("Payload is nil.")
}
if header == nil {
t.Fatalf("Header is nil.")
}
if !strings.EqualFold("1234567890", payload["sub"].(string)) {
t.Fatalf("Claim 'sub' has wrong content %s", payload["sub"].(string))
}
if !strings.EqualFold("Foo", payload["customClaim"].(string)) {
t.Fatalf("Claim 'customClaim' has wrong content %s", payload["customClaim"].(string))
}
if !strings.EqualFold("HS256", header["alg"].(string)) {
t.Fatalf("Claim 'alg' has wrong content %s", header["alg"].(string))
}
}
func TestTimeout(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "https://funkyTestServer.org/foo/bar?timeout=2s", nil)
w := httptest.NewRecorder()
start := time.Now()
handleRequest(w, r)
duration := time.Now().Sub(start)
if duration < 2*time.Second {
t.Fatalf("Wrong timeout (%v) while calling with Query.", duration)
}
r = httptest.NewRequest(http.MethodGet, "https://funkyTestServer.org/foo/bar", nil)
r.Header.Set("X-Timeout", "2000ms")
w = httptest.NewRecorder()
start = time.Now()
handleRequest(w, r)
duration = time.Now().Sub(start)
if duration < 2*time.Second {
t.Fatalf("Wrong timeout (%v) while calling with Query.", duration)
}
}
func doTestRequest(method, target string, body io.Reader, ct string) (map[string]interface{}, error) {
InstanceName = "internalTest"
r := httptest.NewRequest(method, target, body)
w := httptest.NewRecorder()
if ct != "" {
r.Header.Set("Content-Type", ct)
}
handleRequest(w, r)
res := w.Result()
contentType := res.Header.Get("Content-Type")
if !strings.EqualFold(contentType, "application/json") {
return nil, fmt.Errorf("invalid Contant-Type: %v", contentType)
}
var responseJson map[string]interface{}
err := json.NewDecoder(res.Body).Decode(&responseJson)
if err != nil {
return nil, err
}
return responseJson, nil
}