-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_test.go
193 lines (149 loc) · 6.02 KB
/
module_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
181
182
183
184
185
186
187
188
189
190
191
192
193
package fxjsonapi_test
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"github.com/ankorstore/yokai-contrib/fxjsonapi"
"github.com/ankorstore/yokai-contrib/fxjsonapi/testdata/handler"
"github.com/ankorstore/yokai-contrib/fxjsonapi/testdata/model"
"github.com/ankorstore/yokai/fxconfig"
"github.com/ankorstore/yokai/fxgenerate"
"github.com/ankorstore/yokai/fxhttpserver"
"github.com/ankorstore/yokai/fxlog"
"github.com/ankorstore/yokai/fxmetrics"
"github.com/ankorstore/yokai/fxtrace"
"github.com/ankorstore/yokai/log/logtest"
"github.com/ankorstore/yokai/trace/tracetest"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/codes"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
)
func TestFxJSONAPIModule(t *testing.T) {
t.Setenv("APP_CONFIG_PATH", "testdata/config")
var fn handler.DynamicHandlerFunc = func(p fxjsonapi.Processor, c echo.Context) error {
foo := model.Foo{}
err := p.ProcessRequest(c, &foo)
if err != nil {
return err
}
return p.ProcessResponse(c, http.StatusOK, &foo)
}
runTest := func(tb testing.TB) (*echo.Echo, logtest.TestLogBuffer, tracetest.TestTraceExporter) {
tb.Helper()
var httpServer *echo.Echo
var logBuffer logtest.TestLogBuffer
var traceExporter tracetest.TestTraceExporter
fxtest.New(
t,
fx.NopLogger,
fxconfig.FxConfigModule,
fxlog.FxLogModule,
fxtrace.FxTraceModule,
fxmetrics.FxMetricsModule,
fxgenerate.FxGenerateModule,
fxhttpserver.FxHttpServerModule,
fxjsonapi.FxJSONAPIModule,
fx.Supply(fn),
fxhttpserver.AsHandler("POST", "/test", handler.NewDynamicHandler),
fx.Populate(&httpServer, &logBuffer, &traceExporter),
).RequireStart().RequireStop()
return httpServer, logBuffer, traceExporter
}
t.Run("test failure with unsupported content type", func(t *testing.T) {
httpServer, logBuffer, traceExporter := runTest(t)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/test", nil)
httpServer.ServeHTTP(rec, req)
assert.Equal(t, http.StatusUnsupportedMediaType, rec.Code, rec.Body.String())
assert.Contains(t, rec.Body.String(), "Unsupported Media Type")
logtest.AssertHasLogRecord(t, logBuffer, map[string]interface{}{
"level": "error",
"message": "JSON API request invalid content type",
})
logtest.AssertHasLogRecord(t, logBuffer, map[string]interface{}{
"level": "error",
"code": http.StatusUnsupportedMediaType,
"error": "code=415, message=JSON API request invalid content type",
"message": "json api error handler",
})
span, err := traceExporter.Span("JSON API request processing")
assert.NoError(t, err)
assert.Equal(t, codes.Error, span.Snapshot().Status().Code)
})
t.Run("test failure with invalid json api request body", func(t *testing.T) {
httpServer, logBuffer, traceExporter := runTest(t)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewBuffer([]byte("invalid")))
req.Header.Set(echo.HeaderContentType, jsonapi.MediaType)
httpServer.ServeHTTP(rec, req)
assert.Equal(t, http.StatusBadRequest, rec.Code, rec.Body.String())
assert.Contains(t, rec.Body.String(), "Bad Request")
logtest.AssertHasLogRecord(t, logBuffer, map[string]interface{}{
"level": "error",
"message": "JSON API request processing error",
})
logtest.AssertHasLogRecord(t, logBuffer, map[string]interface{}{
"level": "error",
"code": http.StatusBadRequest,
"error": "code=400, message=invalid character 'i' looking for beginning of value",
"message": "json api error handler",
})
span, err := traceExporter.Span("JSON API request processing")
assert.NoError(t, err)
assert.Equal(t, codes.Error, span.Snapshot().Status().Code)
})
t.Run("test success with logging and tracing enabled", func(t *testing.T) {
httpServer, logBuffer, traceExporter := runTest(t)
foo := model.CreateTestFoo()
mFoo, err := fxjsonapi.Marshall(&foo, fxjsonapi.MarshallParams{})
assert.NoError(t, err)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewBuffer(mFoo))
req.Header.Set(echo.HeaderContentType, jsonapi.MediaType)
httpServer.ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
assert.Equal(t, rec.Body.String(), string(mFoo))
logtest.AssertHasLogRecord(t, logBuffer, map[string]interface{}{
"level": "debug",
"message": "JSON API request processing success",
})
logtest.AssertHasLogRecord(t, logBuffer, map[string]interface{}{
"level": "debug",
"message": "JSON API response processing success",
})
reqSpan, err := traceExporter.Span("JSON API request processing")
assert.NoError(t, err)
assert.Equal(t, codes.Ok, reqSpan.Snapshot().Status().Code)
respSpan, err := traceExporter.Span("JSON API response processing")
assert.NoError(t, err)
assert.Equal(t, codes.Ok, respSpan.Snapshot().Status().Code)
})
t.Run("test success with logging and tracing disabled", func(t *testing.T) {
t.Setenv("MODULES_JSONAPI_LOG_ENABLED", "false")
t.Setenv("MODULES_JSONAPI_TRACE_ENABLED", "false")
httpServer, logBuffer, traceExporter := runTest(t)
foo := model.CreateTestFoo()
mFoo, err := fxjsonapi.Marshall(&foo, fxjsonapi.MarshallParams{})
assert.NoError(t, err)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewBuffer(mFoo))
req.Header.Set(echo.HeaderContentType, jsonapi.MediaType)
httpServer.ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
assert.Equal(t, rec.Body.String(), string(mFoo))
logtest.AssertHasNotLogRecord(t, logBuffer, map[string]interface{}{
"level": "debug",
"message": "JSON API request processing success",
})
logtest.AssertHasNotLogRecord(t, logBuffer, map[string]interface{}{
"level": "debug",
"message": "JSON API response processing success",
})
tracetest.AssertHasNotTraceSpan(t, traceExporter, "JSON API request processing")
tracetest.AssertHasNotTraceSpan(t, traceExporter, "JSON API response processing")
})
}