-
-
Notifications
You must be signed in to change notification settings - Fork 946
/
errorx.go
65 lines (57 loc) · 1.91 KB
/
errorx.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
package testhelpers
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"github.com/ory/x/logrusx"
"github.com/ory/herodot"
"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/selfservice/errorx"
"github.com/ory/kratos/session"
"github.com/ory/kratos/x"
)
func NewErrorTestServer(t *testing.T, reg interface {
errorx.PersistenceProvider
config.Provider
}) *httptest.Server {
logger := logrusx.New("", "", logrusx.ForceLevel(logrus.TraceLevel))
writer := herodot.NewJSONWriter(logger)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
e, err := reg.SelfServiceErrorPersister().Read(r.Context(), x.ParseUUID(r.URL.Query().Get("error")))
require.NoError(t, err)
t.Logf("Found error in NewErrorTestServer: %s", e.Errors)
writer.Write(w, r, e.Errors)
}))
t.Cleanup(ts.Close)
reg.Config(context.Background()).MustSet(config.ViperKeySelfServiceErrorUI, ts.URL)
return ts
}
func NewRedirTS(t *testing.T, body string, conf *config.Config) *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if len(body) == 0 {
w.WriteHeader(http.StatusNoContent)
return
}
_, _ = w.Write([]byte(body))
}))
t.Cleanup(ts.Close)
conf.MustSet(config.ViperKeySelfServiceBrowserDefaultReturnTo, ts.URL)
return ts
}
func NewRedirSessionEchoTS(t *testing.T, reg interface {
x.WriterProvider
session.ManagementProvider
config.Provider
}) *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess, err := reg.SessionManager().FetchFromRequest(r.Context(), r)
require.NoError(t, err, "Headers: %+v", r.Header)
reg.Writer().Write(w, r, sess)
}))
t.Cleanup(ts.Close)
reg.Config(context.Background()).MustSet(config.ViperKeySelfServiceBrowserDefaultReturnTo, ts.URL+"/return-ts")
return ts
}