-
-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathdebugger_test.go
More file actions
94 lines (72 loc) 路 2.79 KB
/
Copy pathdebugger_test.go
File metadata and controls
94 lines (72 loc) 路 2.79 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package mercure
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDebuggerConfigHandler(t *testing.T) {
t.Parallel()
h, err := NewHub(t.Context(), WithDebugger(), WithPlayground(), WithAnonymous(), WithSubscriptions())
require.NoError(t, err)
req := httptest.NewRequest(http.MethodGet, "https://example.com/.well-known/mercure/debug/config.json", nil)
w := httptest.NewRecorder()
h.DebuggerConfigHandler(w, req)
resp := w.Result()
t.Cleanup(func() { _ = resp.Body.Close() })
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
var cfg debuggerConfig
require.NoError(t, json.NewDecoder(resp.Body).Decode(&cfg))
assert.True(t, cfg.Playground)
assert.True(t, cfg.Anonymous)
assert.True(t, cfg.Subscriptions)
assert.Equal(t, defaultCookieName, cfg.CookieName)
}
func TestPlaygroundTokenHandlerUsesDerivedResourceIdentifier(t *testing.T) {
t.Parallel()
const resourceIdentifier = "https://hub.example.com/.well-known/mercure"
h, err := NewHub(t.Context(),
WithPlayground(),
WithResourceIdentifier(resourceIdentifier),
WithPlaygroundTokenFunc(func(rid string) (string, error) { return "token-for:" + rid, nil }),
)
require.NoError(t, err)
req := httptest.NewRequest(http.MethodGet, "https://example.com/.well-known/mercure/debug/playground-token", nil)
w := httptest.NewRecorder()
h.PlaygroundTokenHandler(w, req)
resp := w.Result()
t.Cleanup(func() { _ = resp.Body.Close() })
body, _ := io.ReadAll(resp.Body)
assert.Equal(t, "token-for:"+resourceIdentifier, string(body))
}
// TestDebugRoutes checks the router wiring: config.json rides along with the
// debugger, and playground-token exists only when the playground is on and a
// minter is configured.
func TestDebugRoutes(t *testing.T) {
t.Parallel()
tokenFunc := WithPlaygroundTokenFunc(func(string) (string, error) { return "t", nil })
for _, tc := range []struct {
name string
opts []Option
path string
wantStatus int
}{
{"config.json with debugger", []Option{WithDebugger()}, "debug/config.json", http.StatusOK},
{"config.json without debugger", nil, "debug/config.json", http.StatusNotFound},
{"playground-token with minter", []Option{WithPlayground(), tokenFunc}, "debug/playground-token", http.StatusOK},
{"playground-token without minter", []Option{WithPlayground()}, "debug/playground-token", http.StatusNotFound},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
h, err := NewHub(t.Context(), tc.opts...)
require.NoError(t, err)
req := httptest.NewRequest(http.MethodGet, "https://example.com/.well-known/mercure/"+tc.path, nil)
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
assert.Equal(t, tc.wantStatus, w.Result().StatusCode)
})
}
}