-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
72 lines (62 loc) · 1.76 KB
/
example_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
package handler_test
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/ankorstore/yokai-http-template/internal"
"github.com/ankorstore/yokai/log/logtest"
"github.com/ankorstore/yokai/trace/tracetest"
"github.com/labstack/echo/v4"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
"go.uber.org/fx"
)
func TestExampleHandler(t *testing.T) {
var httpServer *echo.Echo
var logBuffer logtest.TestLogBuffer
var traceExporter tracetest.TestTraceExporter
var metricsRegistry *prometheus.Registry
internal.RunTest(t, fx.Populate(&httpServer, &logBuffer, &traceExporter, &metricsRegistry))
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
httpServer.ServeHTTP(rec, req)
// response assertion
assert.Equal(t, http.StatusOK, rec.Code)
assert.Contains(t, rec.Body.String(), "Welcome to http-app.")
// logs assertion
logtest.AssertHasLogRecord(
t,
logBuffer,
map[string]interface{}{
"level": "info",
"service": "http-app",
"method": "GET",
"status": 200,
"uri": "/",
},
)
// trace assertion
tracetest.AssertHasTraceSpan(
t,
traceExporter,
"GET /",
semconv.HTTPMethod(http.MethodGet),
semconv.HTTPRoute("/"),
semconv.HTTPStatusCode(http.StatusOK),
)
// metrics assertion
expectedMetric := `
# HELP http_server_requests_total Number of processed HTTP requests
# TYPE http_server_requests_total counter
http_server_requests_total{method="GET",path="/",status="2xx"} 1
`
err := testutil.GatherAndCompare(
metricsRegistry,
strings.NewReader(expectedMetric),
"http_server_requests_total",
)
assert.NoError(t, err)
}