generated from bool64/go-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
init_test.go
83 lines (65 loc) · 2.27 KB
/
init_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
package brick_test
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/bool64/brick"
"github.com/bool64/brick/config"
"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/swaggest/assertjson"
"github.com/swaggest/rest/nethttp"
"github.com/swaggest/usecase"
"go.uber.org/zap"
)
func TestNewBaseLocator(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.String() == "/api/features" { // Laika init.
w.Header().Set("Content-Type", "application/json")
_, err := w.Write([]byte("[]"))
assert.NoError(t, err)
}
}))
defer srv.Close()
u, err := url.Parse(srv.URL)
require.NoError(t, err)
cfg := brick.BaseConfig{}
require.NoError(t, config.Load("TEST", &cfg))
log := bytes.NewBuffer(nil)
cfg.Environment = "live"
cfg.Log.Level = zap.InfoLevel
cfg.Log.Output = log
cfg.Debug.TraceSamplingProbability = 1.0
cfg.ServiceName = "test"
u.User = url.UserPassword("foo", "")
u.Path = "123123"
l, err := brick.NewBaseLocator(cfg)
require.NoError(t, err)
require.NotNil(t, l)
r := brick.NewBaseWebService(l)
serviceURLPrefix := "/" + cfg.ServiceName
uc := usecase.NewIOI(nil, nil, func(_ context.Context, _, _ interface{}) error {
panic("oops")
})
r.Route(serviceURLPrefix, func(r chi.Router) {
r.Method(http.MethodGet, "/something-public", nethttp.NewHandler(uc))
})
rw := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, serviceURLPrefix+"/something-public", nil)
require.NoError(t, err)
req.Header.Set("X-Foo", "bar")
r.ServeHTTP(rw, req)
assert.Equal(t, http.StatusInternalServerError, rw.Code)
assert.Equal(t, "application/json; charset=utf-8", rw.Header().Get("Content-Type"))
assert.Equal(t, `{"error":"request panicked"}`+"\n", rw.Body.String())
logs := "[" + strings.ReplaceAll(log.String(), "\n", ",\n") + "{}]"
assertjson.Equal(t, []byte(`[{"level":"error","@timestamp":"<ignore-diff>","message":"request panicked","panic":"oops","stack":"<ignore-diff>","client.ip":"","user_agent.original":"","url.original":"/test/something-public","http.request.method":"GET"},
{}]`), []byte(logs), logs)
l.Shutdown()
assert.NoError(t, <-l.Wait())
}