-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo_test.go
179 lines (152 loc) · 5.12 KB
/
echo_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
package barton
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
func TestEchoNameChange(t *testing.T) {
buf := bytes.NewBufferString("")
zc := NewZerologConfig().SetWriter(buf).UseUTCTime()
zc.SetGlobalPolicy().SetGlobalLogger()
b := NewWebApp("BartonTest")
assert.Equal(t, "BartonTest", b.appName)
b.Name("SetANewName")
assert.Equal(t, "SetANewName", b.appName)
e, cleanup := b.NewEcho()
defer cleanup()
buf.Reset()
// Perform an HTTP call
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/metrics", nil)
req.Header.Set("User-Agent", "Golang_UT")
e.ServeHTTP(w, req)
response := w.Result()
body, _ := io.ReadAll(response.Body)
if !strings.Contains(string(body),
"SetANewName_request_duration_seconds_sum") {
t.Errorf("PrometheusMetricsNotFound:response=%s", body)
return
}
}
func TestEchoPrometheusIntegration(t *testing.T) {
buf := bytes.NewBufferString("")
zc := NewZerologConfig().SetWriter(buf).UseUTCTime()
zc.SetGlobalPolicy().SetGlobalLogger()
// Cleanup() function must be called at last step to make
// sure we can create another instance without internal error
// inside Promethues library complaining duplicated registration
// attempts. This is because Prometheus registration is done in
// global namespace.
e, cleanup := NewWebApp("BartonTest").NewEcho()
defer cleanup()
// Perform an HTTP call
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/metrics", nil)
req.Header.Set("User-Agent", "Golang_UT")
e.ServeHTTP(w, req)
response := w.Result()
body, _ := io.ReadAll(response.Body)
if !strings.Contains(string(body),
"BartonTest_request_duration_seconds_sum") {
t.Errorf("PrometheusMetricsNotFound:response=%s", body)
return
}
}
func TestEchoEnablePrometheusNoException(t *testing.T) {
testKey := []byte("test123")
c := NewHMACJWTGen(testKey).SigningMethod("HS512")
token := newToken(t, "HS512", testKey)
buf := bytes.NewBufferString("")
zc := NewZerologConfig().SetWriter(buf).UseUTCTime()
zc.SetGlobalPolicy()
zc.SetGlobalLogger()
// Cleanup() function must be called at last step to make
// sure we can create another instance without internal error
// inside Promethues library complaining duplicated registration
// attempts. This is because Prometheus registration is done in
// global namespace.
e, cleanup := NewWebApp("BartonTest").NewEcho()
defer cleanup()
e.Use(c.NewEchoAuthMiddleware())
// Prometheus path /metrics is also protected.
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/metrics", nil)
req.Header.Set("User-Agent", "Golang_UT")
e.ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
// Prometheus path /metrics is also protected. This is not a
// recommended approach but let's set it as current behavior.
// We will fix it later.
w2 := httptest.NewRecorder()
req2 := httptest.NewRequest("GET", "/metrics", nil)
req2.Header.Set("User-Agent", "Golang_UT")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
e.ServeHTTP(w2, req)
resp2 := w2.Result()
defer resp2.Body.Close()
body, _ := io.ReadAll(resp2.Body)
if !strings.Contains(string(body),
"BartonTest_request_duration_seconds_sum") {
t.Errorf("PrometheusMetricsNotFound:resp=%s", body)
return
}
}
func TestEchoEnablePrometheusBecomeJWTException(t *testing.T) {
// This is not a real test case, but a demo that shows how we
// workaround current behavior if we want a protected regular
// service except /metrics.
testKey := []byte("test123")
c := NewHMACJWTGen(testKey).SigningMethod("HS512")
buf := bytes.NewBufferString("")
zc := NewZerologConfig().SetWriter(buf).UseUTCTime()
zc.SetGlobalPolicy()
zc.SetGlobalLogger()
e, cleanup := NewWebApp("BartonTest").NewEcho()
defer cleanup()
g := e.Group("/v1", c.NewEchoAuthMiddleware())
g.GET("/protected", func(c echo.Context) error {
return c.String(http.StatusOK, "hello!")
})
// Prometheus path /metrics is not protected
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/metrics", nil)
req.Header.Set("User-Agent", "Golang_UT")
e.ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if !strings.Contains(string(body),
"BartonTest_request_duration_seconds_sum") {
t.Errorf("PrometheusMetricsNotFound:resp=%s", body)
return
}
// Normal path are protected - No JWT is denied
w = httptest.NewRecorder()
req = httptest.NewRequest("GET", "/v1/protected", nil)
e.ServeHTTP(w, req)
resp = w.Result()
defer resp.Body.Close()
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
// Normal path are protected - Access granted with JWT
token := newToken(t, "HS512", testKey)
w = httptest.NewRecorder()
req = httptest.NewRequest("GET", "/v1/protected", nil)
req.Header.Set("Authorization",
fmt.Sprintf("Bearer %s", token))
e.ServeHTTP(w, req)
resp = w.Result()
defer resp.Body.Close()
answer, err := ioutil.ReadAll(resp.Body)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "hello!", string(answer))
}