-
-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathresourcemetadata_test.go
More file actions
142 lines (108 loc) 路 4.49 KB
/
Copy pathresourcemetadata_test.go
File metadata and controls
142 lines (108 loc) 路 4.49 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package mercure
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestProtectedResourceMetadata(t *testing.T) {
hub := createDummy(t,
WithResourceIdentifier("https://example.com/.well-known/mercure"),
// createDummy declares testIssuer as a plain trusted issuer (not
// advertised); add an authorization server, the only issuer listed in
// the metadata.
WithIssuers([]Issuer{{
Identifier: "https://as.example.com",
AuthorizationServer: true,
Subscriber: Static{Key: []byte("subscriber"), Algorithm: "HS256"},
}}),
)
req := httptest.NewRequest(http.MethodGet, protectedResourceMetadataPath, nil)
w := httptest.NewRecorder()
hub.ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
require.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
var metadata protectedResourceMetadata
require.NoError(t, json.NewDecoder(resp.Body).Decode(&metadata))
assert.Equal(t, "https://example.com/.well-known/mercure", metadata.Resource)
assert.Equal(t, []string{"header"}, metadata.BearerMethodsSupported)
assert.Equal(t, defaultCookieName, metadata.MercureCookie)
assert.False(t, metadata.MercureSubscriptions)
assert.Equal(t, []string{"https://as.example.com"}, metadata.AuthorizationServers)
assert.Equal(t, []string{authorizationDetailTypeMercure}, metadata.AuthorizationDetailsTypesSupported)
}
func TestProtectedResourceMetadataAdvertisesCustomCookieName(t *testing.T) {
hub := createDummy(t,
WithResourceIdentifier("https://example.com/.well-known/mercure"),
WithCookieName("__Secure-custom"),
)
req := httptest.NewRequest(http.MethodGet, protectedResourceMetadataPath, nil)
w := httptest.NewRecorder()
hub.ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
var metadata protectedResourceMetadata
require.NoError(t, json.NewDecoder(resp.Body).Decode(&metadata))
assert.Equal(t, "__Secure-custom", metadata.MercureCookie)
}
func TestProtectedResourceMetadataAdvertisesSubscriptions(t *testing.T) {
hub := createDummy(t,
WithResourceIdentifier("https://example.com/.well-known/mercure"),
WithSubscriptions(),
)
req := httptest.NewRequest(http.MethodGet, protectedResourceMetadataPath, nil)
w := httptest.NewRecorder()
hub.ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
var metadata protectedResourceMetadata
require.NoError(t, json.NewDecoder(resp.Body).Decode(&metadata))
assert.True(t, metadata.MercureSubscriptions)
}
func TestProtectedResourceMetadataUsesResourceIdentifier(t *testing.T) {
hub := createDummy(t, WithResourceIdentifier("https://example.com/.well-known/mercure"))
req := httptest.NewRequest(http.MethodGet, protectedResourceMetadataPath, nil)
w := httptest.NewRecorder()
hub.ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
var metadata protectedResourceMetadata
require.NoError(t, json.NewDecoder(resp.Body).Decode(&metadata))
assert.Equal(t, "https://example.com/.well-known/mercure", metadata.Resource)
assert.Empty(t, metadata.AuthorizationServers)
}
// With no static resource identifier configured, the metadata resource is
// derived from the request origin, so a hub reachable through several public
// URLs presents each caller the identity of the host it contacted.
func TestProtectedResourceMetadataDerivedFromRequest(t *testing.T) {
tms, err := NewTopicMatcherStore(0)
require.NoError(t, err)
hub, err := NewHub(t.Context(), testIssuerOption(), WithTopicMatcherStore(tms))
require.NoError(t, err)
for _, host := range []string{"https://a.example.com", "https://b.example.com"} {
req := httptest.NewRequest(http.MethodGet, host+protectedResourceMetadataPath, nil)
w := httptest.NewRecorder()
hub.ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
var metadata protectedResourceMetadata
require.NoError(t, json.NewDecoder(resp.Body).Decode(&metadata))
assert.Equal(t, host+"/.well-known/mercure", metadata.Resource)
}
}
func TestProtectedResourceMetadataNotRegisteredWhenAnonymousWithoutKeys(t *testing.T) {
tms, err := NewTopicMatcherStore(0)
require.NoError(t, err)
hub, err := NewHub(t.Context(), WithAnonymous(), WithTopicMatcherStore(tms))
require.NoError(t, err)
req := httptest.NewRequest(http.MethodGet, protectedResourceMetadataPath, nil)
w := httptest.NewRecorder()
hub.ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
}