Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit d0e2e91

Browse files
authored
allow path without trailing / and normalize method. Closes #272 (#296)
1 parent 28f041e commit d0e2e91

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

subscriptions/subscriptions.go

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package subscriptions
33
import (
44
"bytes"
55
"encoding/json"
6+
"strings"
67

78
"github.com/docker/libkv/store"
89
"github.com/serverless/event-gateway/functions"
@@ -204,6 +205,11 @@ func (ps Subscriptions) deleteEndpoint(method, path string) error {
204205
}
205206

206207
func (ps Subscriptions) validateSubscription(s *Subscription) error {
208+
if s.Event == SubscriptionHTTP {
209+
s.Path = ensurePrefix(s.Path, "/")
210+
s.Method = strings.ToUpper(s.Method)
211+
}
212+
207213
validate := validator.New()
208214
validate.RegisterValidation("urlpath", urlPathValidator)
209215
validate.RegisterValidation("eventname", eventNameValidator)
@@ -220,3 +226,11 @@ func (ps Subscriptions) validateSubscription(s *Subscription) error {
220226

221227
return nil
222228
}
229+
230+
// ensurePrefix ensures s starts with prefix.
231+
func ensurePrefix(s, prefix string) string {
232+
if strings.HasPrefix(s, prefix) {
233+
return s
234+
}
235+
return prefix + s
236+
}

subscriptions/subscriptions_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ func TestCreateSubscription_HTTPOK(t *testing.T) {
3030
assert.Nil(t, err)
3131
}
3232

33+
func TestCreateSubscription_HTTPNormalizeMethodPath(t *testing.T) {
34+
ctrl := gomock.NewController(t)
35+
defer ctrl.Finish()
36+
37+
subscriptionsDB := mock.NewMockStore(ctrl)
38+
subscriptionsDB.EXPECT().Get("http-GET-%2F").Return(nil, errors.New("KV sub not found"))
39+
subscriptionsDB.EXPECT().Put("http-GET-%2F", []byte(`{"subscriptionId":"http-GET-%2F","event":"http","functionId":"func","method":"GET","path":"/"}`), nil).Return(nil)
40+
endpointsDB := mock.NewMockStore(ctrl)
41+
endpointsDB.EXPECT().Put("GET-%2F", []byte(`{"endpointId":"GET-%2F","functionId":"func","method":"GET","path":"/"}`), nil).Return(nil)
42+
functionsDB := mock.NewMockStore(ctrl)
43+
functionsDB.EXPECT().Exists("func").Return(true, nil)
44+
subs := &Subscriptions{SubscriptionsDB: subscriptionsDB, EndpointsDB: endpointsDB, FunctionsDB: functionsDB, Log: zap.NewNop()}
45+
46+
_, err := subs.CreateSubscription(&Subscription{ID: "testid", Event: "http", FunctionID: "func", Method: "get", Path: ""})
47+
48+
assert.Nil(t, err)
49+
}
50+
3351
func TestCreateSubscription_HTTPValidationError(t *testing.T) {
3452
ctrl := gomock.NewController(t)
3553
defer ctrl.Finish()

0 commit comments

Comments
 (0)