From 1263271e47dbc81097bf3e1330c11cd5621ba83e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Wed, 13 May 2020 09:51:34 +0200 Subject: [PATCH] Use UUID URN by default for event IDs --- hub/publish_test.go | 4 ++-- hub/subscribe.go | 2 +- hub/subscribe_test.go | 1 - hub/subscriber.go | 6 +++++- hub/update.go | 2 +- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/hub/publish_test.go b/hub/publish_test.go index 6ef5a9c4..abe42eb7 100644 --- a/hub/publish_test.go +++ b/hub/publish_test.go @@ -221,7 +221,7 @@ func TestPublishGenerateUUID(t *testing.T) { u := <-s.Receive() require.NotNil(t, u) - _, err := uuid.FromString(u.ID) + _, err := uuid.FromString(strings.TrimPrefix(u.ID, "urn:uuid:")) assert.Nil(t, err) }() @@ -244,7 +244,7 @@ func TestPublishGenerateUUID(t *testing.T) { bodyBytes, _ := ioutil.ReadAll(resp.Body) body := string(bodyBytes) - _, err := uuid.FromString(body) + _, err := uuid.FromString(strings.TrimPrefix(body, "urn:uuid:")) assert.Nil(t, err) wg.Wait() diff --git a/hub/subscribe.go b/hub/subscribe.go index e3faff94..c31d7881 100644 --- a/hub/subscribe.go +++ b/hub/subscribe.go @@ -249,7 +249,7 @@ func (h *Hub) dispatchSubscriptionUpdate(s *Subscriber, active bool) { for k, topic := range s.Topics { connection := &subscription{ - ID: "https://mercure.rocks/subscriptions/" + s.EscapedTopics[k] + "/" + s.ID, + ID: "https://mercure.rocks/subscriptions/" + s.EscapedTopics[k] + "/" + s.EscapedID, Type: "https://mercure.rocks/Subscription", Topic: topic, Active: active, diff --git a/hub/subscribe_test.go b/hub/subscribe_test.go index edddd375..4a363f75 100644 --- a/hub/subscribe_test.go +++ b/hub/subscribe_test.go @@ -394,7 +394,6 @@ func TestSubscriptionEvents(t *testing.T) { ready := len(s.subscribers) == 2 s.RUnlock() - log.Info("Waiting for subscriber...") if ready { break } diff --git a/hub/subscriber.go b/hub/subscriber.go index 30da546a..cae93371 100644 --- a/hub/subscriber.go +++ b/hub/subscriber.go @@ -1,6 +1,8 @@ package hub import ( + "net/url" + "github.com/gofrs/uuid" log "github.com/sirupsen/logrus" "github.com/yosida95/uritemplate" @@ -14,6 +16,7 @@ type updateSource struct { // Subscriber represents a client subscribed to a list of topics. type Subscriber struct { ID string + EscapedID string Claims *claims Targets map[string]struct{} Topics []string @@ -35,9 +38,10 @@ type Subscriber struct { } func newSubscriber(lastEventID string) *Subscriber { - id := uuid.Must(uuid.NewV4()).String() + id := "urn:uuid:" + uuid.Must(uuid.NewV4()).String() s := &Subscriber{ ID: id, + EscapedID: url.QueryEscape(id), LastEventID: lastEventID, LogFields: log.Fields{ "subscriber_id": id, diff --git a/hub/update.go b/hub/update.go index 5594676f..38226882 100644 --- a/hub/update.go +++ b/hub/update.go @@ -27,7 +27,7 @@ func newUpdate(event Event, topics []string, targets map[string]struct{}) *Updat Targets: targets, } if u.ID == "" { - u.ID = uuid.Must(uuid.NewV4()).String() + u.ID = "urn:uuid:" + uuid.Must(uuid.NewV4()).String() } return u