Skip to content

Commit

Permalink
allow path without trailing / and normalize method. Closes #272 (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthenw committed Aug 17, 2017
1 parent 28f041e commit d0e2e91
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions subscriptions/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package subscriptions
import (
"bytes"
"encoding/json"
"strings"

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

func (ps Subscriptions) validateSubscription(s *Subscription) error {
if s.Event == SubscriptionHTTP {
s.Path = ensurePrefix(s.Path, "/")
s.Method = strings.ToUpper(s.Method)
}

validate := validator.New()
validate.RegisterValidation("urlpath", urlPathValidator)
validate.RegisterValidation("eventname", eventNameValidator)
Expand All @@ -220,3 +226,11 @@ func (ps Subscriptions) validateSubscription(s *Subscription) error {

return nil
}

// ensurePrefix ensures s starts with prefix.
func ensurePrefix(s, prefix string) string {
if strings.HasPrefix(s, prefix) {
return s
}
return prefix + s
}
18 changes: 18 additions & 0 deletions subscriptions/subscriptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ func TestCreateSubscription_HTTPOK(t *testing.T) {
assert.Nil(t, err)
}

func TestCreateSubscription_HTTPNormalizeMethodPath(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

subscriptionsDB := mock.NewMockStore(ctrl)
subscriptionsDB.EXPECT().Get("http-GET-%2F").Return(nil, errors.New("KV sub not found"))
subscriptionsDB.EXPECT().Put("http-GET-%2F", []byte(`{"subscriptionId":"http-GET-%2F","event":"http","functionId":"func","method":"GET","path":"/"}`), nil).Return(nil)
endpointsDB := mock.NewMockStore(ctrl)
endpointsDB.EXPECT().Put("GET-%2F", []byte(`{"endpointId":"GET-%2F","functionId":"func","method":"GET","path":"/"}`), nil).Return(nil)
functionsDB := mock.NewMockStore(ctrl)
functionsDB.EXPECT().Exists("func").Return(true, nil)
subs := &Subscriptions{SubscriptionsDB: subscriptionsDB, EndpointsDB: endpointsDB, FunctionsDB: functionsDB, Log: zap.NewNop()}

_, err := subs.CreateSubscription(&Subscription{ID: "testid", Event: "http", FunctionID: "func", Method: "get", Path: ""})

assert.Nil(t, err)
}

func TestCreateSubscription_HTTPValidationError(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down

0 comments on commit d0e2e91

Please sign in to comment.