(t *testing.T)
| 140 | } |
| 141 | |
| 142 | func TestSubscriptionsHandlerForTopic(t *testing.T) { |
| 143 | t.Parallel() |
| 144 | |
| 145 | hub := createDummy(t) |
| 146 | tss := &TopicSelectorStore{} |
| 147 | ctx := t.Context() |
| 148 | logger := slog.Default() |
| 149 | |
| 150 | s1 := NewLocalSubscriber("", logger, tss) |
| 151 | s1.SetTopics([]string{"https://example.com/foo"}, nil) |
| 152 | require.NoError(t, hub.transport.AddSubscriber(ctx, s1)) |
| 153 | |
| 154 | s2 := NewLocalSubscriber("", logger, tss) |
| 155 | s2.SetTopics([]string{"https://example.com/bar"}, nil) |
| 156 | require.NoError(t, hub.transport.AddSubscriber(ctx, s2)) |
| 157 | |
| 158 | escapedBarTopic := url.QueryEscape("https://example.com/bar") |
| 159 | |
| 160 | router := mux.NewRouter() |
| 161 | router.UseEncodedPath() |
| 162 | router.SkipClean(true) |
| 163 | router.HandleFunc(subscriptionsForTopicURL, hub.SubscriptionsHandler) |
| 164 | |
| 165 | req := httptest.NewRequest(http.MethodGet, defaultHubURL+subscriptionsPath+"/"+s2.EscapedTopics[0], nil) |
| 166 | req.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: createDummyAuthorizedJWT(roleSubscriber, []string{"/.well-known/mercure/subscriptions/" + s2.EscapedTopics[0]})}) |
| 167 | |
| 168 | w := httptest.NewRecorder() |
| 169 | hub.SubscriptionsHandler(w, req) |
| 170 | res := w.Result() |
| 171 | assert.Equal(t, http.StatusOK, res.StatusCode) |
| 172 | require.NoError(t, res.Body.Close()) |
| 173 | |
| 174 | var subscriptions subscriptionCollection |
| 175 | require.NoError(t, json.Unmarshal(w.Body.Bytes(), &subscriptions)) |
| 176 | |
| 177 | assert.Equal(t, "https://mercure.rocks/", subscriptions.Context) |
| 178 | assert.Equal(t, defaultHubURL+subscriptionsPath+"/"+escapedBarTopic, subscriptions.ID) |
| 179 | assert.Equal(t, "Subscriptions", subscriptions.Type) |
| 180 | |
| 181 | lastEventID, subscribers, _ := hub.transport.(TransportSubscribers).GetSubscribers(t.Context()) |
| 182 | |
| 183 | assert.Equal(t, lastEventID, subscriptions.LastEventID) |
| 184 | require.NotEmpty(t, subscribers) |
| 185 | |
| 186 | for _, s := range subscribers { |
| 187 | for _, sub := range s.getSubscriptions("https://example.com/bar", "", true) { |
| 188 | require.NotContains(t, "foo", sub.Topic) |
| 189 | assert.Contains(t, subscriptions.Subscriptions, sub) |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | func TestSubscriptionHandler(t *testing.T) { |
| 195 | t.Parallel() |
nothing calls this directly
no test coverage detected