|
1 | 1 | package gin_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "io/ioutil" |
4 | 5 | "net/http"
|
5 | 6 | "net/http/httptest"
|
6 | 7 | "testing"
|
7 | 8 |
|
8 | 9 | "github.com/gin-gonic/gin"
|
9 | 10 | "github.com/stretchr/testify/assert"
|
10 | 11 | "github.com/stretchr/testify/mock"
|
| 12 | + "github.com/stretchr/testify/require" |
11 | 13 |
|
12 | 14 | mmetrics "github.com/slok/go-http-metrics/internal/mocks/metrics"
|
13 | 15 | "github.com/slok/go-http-metrics/metrics"
|
14 | 16 | "github.com/slok/go-http-metrics/middleware"
|
15 | 17 | ginmiddleware "github.com/slok/go-http-metrics/middleware/gin"
|
16 | 18 | )
|
17 | 19 |
|
18 |
| -func getTestHandler(statusCode int) gin.HandlerFunc { |
19 |
| - return func(c *gin.Context) { |
20 |
| - c.String(statusCode, "Hello world") |
21 |
| - } |
22 |
| -} |
23 |
| - |
24 | 20 | func TestMiddleware(t *testing.T) {
|
25 | 21 | tests := map[string]struct {
|
26 |
| - handlerID string |
27 |
| - statusCode int |
28 |
| - req *http.Request |
29 |
| - config middleware.Config |
30 |
| - expHandlerID string |
31 |
| - expService string |
32 |
| - expMethod string |
33 |
| - expStatusCode string |
| 22 | + handlerID string |
| 23 | + config middleware.Config |
| 24 | + req func() *http.Request |
| 25 | + mock func(m *mmetrics.Recorder) |
| 26 | + handler func() gin.HandlerFunc |
| 27 | + expRespCode int |
| 28 | + expRespBody string |
34 | 29 | }{
|
35 | 30 | "A default HTTP middleware should call the recorder to measure.": {
|
36 |
| - statusCode: http.StatusAccepted, |
37 |
| - req: httptest.NewRequest(http.MethodPost, "/test", nil), |
38 |
| - expHandlerID: "/test", |
39 |
| - expMethod: http.MethodPost, |
40 |
| - expStatusCode: "202", |
| 31 | + req: func() *http.Request { |
| 32 | + return httptest.NewRequest(http.MethodPost, "/test", nil) |
| 33 | + }, |
| 34 | + mock: func(m *mmetrics.Recorder) { |
| 35 | + expHTTPReqProps := metrics.HTTPReqProperties{ |
| 36 | + ID: "/test", |
| 37 | + Service: "", |
| 38 | + Method: "POST", |
| 39 | + Code: "202", |
| 40 | + } |
| 41 | + m.On("ObserveHTTPRequestDuration", mock.Anything, expHTTPReqProps, mock.Anything).Once() |
| 42 | + m.On("ObserveHTTPResponseSize", mock.Anything, expHTTPReqProps, int64(5)).Once() |
| 43 | + |
| 44 | + expHTTPProps := metrics.HTTPProperties{ |
| 45 | + ID: "/test", |
| 46 | + Service: "", |
| 47 | + } |
| 48 | + m.On("AddInflightRequests", mock.Anything, expHTTPProps, 1).Once() |
| 49 | + m.On("AddInflightRequests", mock.Anything, expHTTPProps, -1).Once() |
| 50 | + }, |
| 51 | + handler: func() gin.HandlerFunc { |
| 52 | + return func(c *gin.Context) { |
| 53 | + c.String(202, "test1") |
| 54 | + } |
| 55 | + }, |
| 56 | + expRespCode: 202, |
| 57 | + expRespBody: "test1", |
| 58 | + }, |
| 59 | + |
| 60 | + "A default HTTP middleware using JSON should call the recorder to measure (Regression test: https://github.com/slok/go-http-metrics/issues/31).": { |
| 61 | + req: func() *http.Request { |
| 62 | + return httptest.NewRequest(http.MethodPost, "/test", nil) |
| 63 | + }, |
| 64 | + mock: func(m *mmetrics.Recorder) { |
| 65 | + expHTTPReqProps := metrics.HTTPReqProperties{ |
| 66 | + ID: "/test", |
| 67 | + Service: "", |
| 68 | + Method: "POST", |
| 69 | + Code: "202", |
| 70 | + } |
| 71 | + m.On("ObserveHTTPRequestDuration", mock.Anything, expHTTPReqProps, mock.Anything).Once() |
| 72 | + m.On("ObserveHTTPResponseSize", mock.Anything, expHTTPReqProps, int64(14)).Once() |
| 73 | + |
| 74 | + expHTTPProps := metrics.HTTPProperties{ |
| 75 | + ID: "/test", |
| 76 | + Service: "", |
| 77 | + } |
| 78 | + m.On("AddInflightRequests", mock.Anything, expHTTPProps, 1).Once() |
| 79 | + m.On("AddInflightRequests", mock.Anything, expHTTPProps, -1).Once() |
| 80 | + }, |
| 81 | + handler: func() gin.HandlerFunc { |
| 82 | + return func(c *gin.Context) { |
| 83 | + c.JSON(202, map[string]string{"test": "one"}) |
| 84 | + } |
| 85 | + }, |
| 86 | + expRespCode: 202, |
| 87 | + expRespBody: `{"test":"one"}`, |
41 | 88 | },
|
42 | 89 | }
|
43 | 90 |
|
44 | 91 | for name, test := range tests {
|
45 | 92 | t.Run(name, func(t *testing.T) {
|
46 | 93 | assert := assert.New(t)
|
| 94 | + require := require.New(t) |
47 | 95 |
|
48 | 96 | // Mocks.
|
49 | 97 | mr := &mmetrics.Recorder{}
|
50 |
| - expHTTPReqProps := metrics.HTTPReqProperties{ |
51 |
| - ID: test.expHandlerID, |
52 |
| - Service: test.expService, |
53 |
| - Method: test.expMethod, |
54 |
| - Code: test.expStatusCode, |
55 |
| - } |
56 |
| - expHTTPProps := metrics.HTTPProperties{ |
57 |
| - ID: test.expHandlerID, |
58 |
| - Service: test.expService, |
59 |
| - } |
60 |
| - mr.On("ObserveHTTPRequestDuration", mock.Anything, expHTTPReqProps, mock.Anything).Once() |
61 |
| - mr.On("ObserveHTTPResponseSize", mock.Anything, expHTTPReqProps, mock.Anything).Once() |
62 |
| - mr.On("AddInflightRequests", mock.Anything, expHTTPProps, 1).Once() |
63 |
| - mr.On("AddInflightRequests", mock.Anything, expHTTPProps, -1).Once() |
| 98 | + test.mock(mr) |
64 | 99 |
|
65 | 100 | // Create our instance with the middleware.
|
66 | 101 | mdlw := middleware.New(middleware.Config{Recorder: mr})
|
67 | 102 | engine := gin.New()
|
68 |
| - engine.POST("/test", |
69 |
| - ginmiddleware.Handler("", mdlw), |
70 |
| - getTestHandler(test.statusCode)) |
| 103 | + req := test.req() |
| 104 | + engine.Handle(req.Method, req.URL.Path, |
| 105 | + ginmiddleware.Handler(test.handlerID, mdlw), |
| 106 | + test.handler()) |
71 | 107 |
|
72 | 108 | // Make the request.
|
73 | 109 | resp := httptest.NewRecorder()
|
74 |
| - engine.ServeHTTP(resp, test.req) |
| 110 | + engine.ServeHTTP(resp, req) |
75 | 111 |
|
76 | 112 | // Check.
|
77 | 113 | mr.AssertExpectations(t)
|
78 |
| - assert.Equal(test.statusCode, resp.Result().StatusCode) |
| 114 | + assert.Equal(test.expRespCode, resp.Result().StatusCode) |
| 115 | + gotBody, err := ioutil.ReadAll(resp.Result().Body) |
| 116 | + require.NoError(err) |
| 117 | + assert.Equal(test.expRespBody, string(gotBody)) |
79 | 118 | })
|
80 | 119 | }
|
81 | 120 | }
|
0 commit comments