forked from iotaledger/wasp-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebapi_test.go
More file actions
72 lines (61 loc) · 1.99 KB
/
Copy pathwebapi_test.go
File metadata and controls
72 lines (61 loc) · 1.99 KB
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 webapi_test
import (
"context"
"io"
"net/http"
"testing"
"time"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
"github.com/iotaledger/wasp/components/webapi"
"github.com/iotaledger/wasp/packages/authentication"
)
func TestInternalServerErrors(t *testing.T) {
// start a webserver with a test log
logCore, logObserver := observer.New(zapcore.DebugLevel)
log := zap.New(logCore)
e := webapi.NewEcho(&webapi.ParametersWebAPI{
Enabled: true,
BindAddress: ":9999",
Auth: authentication.AuthConfiguration{},
Limits: webapi.ParametersWebAPILimits{
Timeout: time.Minute,
ReadTimeout: time.Minute,
WriteTimeout: time.Minute,
MaxBodyLength: "1M",
MaxTopicSubscriptionsPerClient: 0,
ConfirmedStateLagThreshold: 2,
Jsonrpc: webapi.ParametersJSONRPC{},
},
DebugRequestLoggerEnabled: true,
},
nil,
log.Sugar(),
)
// Add an endpoint that just panics with "foobar" and start the server
exceptionText := "foobar"
e.GET("/test", func(c echo.Context) error { panic(exceptionText) })
go func() {
err := e.Start(":9999")
require.ErrorIs(t, http.ErrServerClosed, err)
}()
defer e.Shutdown(context.Background())
// query the endpoint
req, err := http.NewRequest(http.MethodGet, "http://localhost:9999/test", http.NoBody)
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)
require.NoError(t, err)
resBody, err := io.ReadAll(res.Body)
require.NoError(t, err)
res.Body.Close()
// assert the exception is not present in the response (prevent leaking errors)
require.Equal(t, res.StatusCode, http.StatusInternalServerError)
require.NotContains(t, string(resBody), exceptionText)
// assert the exception is logged
logEntries := logObserver.All()
require.Len(t, logEntries, 1)
require.Contains(t, logEntries[0].Message, exceptionText)
}