Skip to content

Commit

Permalink
Remove CORS configuration from subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Winnicki committed Jun 8, 2018
1 parent 8cb5ea9 commit 1ccc6c9
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 176 deletions.
10 changes: 1 addition & 9 deletions httpapi/httpapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,17 +514,9 @@ func TestUpdateSubscription(t *testing.T) {
FunctionID: "func",
Method: "GET",
Path: "/",
CORS: &subscription.CORS{
Origins: []string{"*"},
Methods: []string{"HEAD", "GET", "POST"},
Headers: []string{"Origin", "Accept", "Content-Type"},
AllowCredentials: false,
},
}
updatedValue := []byte(`{"space":"default","subscriptionId":"testid","type":"sync",` +
`"eventType":"http.request","functionId":"func","method":"GET","path":"/",` +
`"cors":{"origins":["*"],"methods":["HEAD","GET","POST"],` +
`"headers":["Origin","Accept","Content-Type"],"allowCredentials":false}}`)
`"eventType":"http.request","functionId":"func","method":"GET","path":"/"}`)

t.Run("subscription updated", func(t *testing.T) {
subscriptions.EXPECT().UpdateSubscription(subscription.ID("testid"), updateSub).Return(updateSub, nil)
Expand Down
2 changes: 1 addition & 1 deletion internal/cache/subscription_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *subscriptionCache) Modified(k string, v []byte) {
root = pathtree.NewNode()
c.endpoints[s.Method] = root
}
err := root.AddRoute(s.Path, s.Space, s.FunctionID, s.CORS)
err := root.AddRoute(s.Path, s.Space, s.FunctionID)
if err != nil {
c.log.Error("Could not add path to the tree.", zap.Error(err), zap.String("path", s.Path), zap.String("method", s.Method))
}
Expand Down
23 changes: 2 additions & 21 deletions internal/cache/subscription_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/serverless/event-gateway/function"
"github.com/serverless/event-gateway/libkv"
"github.com/serverless/event-gateway/subscription"
"github.com/stretchr/testify/assert"

"go.uber.org/zap"
Expand Down Expand Up @@ -59,29 +58,11 @@ func TestSubscriptionCacheModifiedSyncSubscription(t *testing.T) {
"path": "/b",
"method": "GET"}`))

space, id, _, _ := scache.endpoints["GET"].Resolve("/a")
space, id, _ := scache.endpoints["GET"].Resolve("/a")
assert.Equal(t, function.ID("testfunc1"), *id)
assert.Equal(t, "default", space)
}

func TestSubscriptionCacheModifiedCORSConfiguration(t *testing.T) {
scache := newSubscriptionCache(zap.NewNop())

scache.Modified("testsub1", []byte(`{
"subscriptionId":"testsub1",
"type": "sync",
"eventType": "http.request",
"functionId": "testfunc1",
"path": "/a",
"method": "GET",
"cors": {
"origins": ["http://example.com"]
}}`))

_, _, _, corsConfig := scache.endpoints["GET"].Resolve("/a")
assert.Equal(t, &subscription.CORS{Origins: []string{"http://example.com"}}, corsConfig)
}

func TestSubscriptionCacheModifiedEventsWrongPayload(t *testing.T) {
scache := newSubscriptionCache(zap.NewNop())

Expand Down Expand Up @@ -139,7 +120,7 @@ func TestSubscriptionCacheModifiedHTTPSubscriptionDeleted(t *testing.T) {
"path": "/",
"method": "GET"}`))

space, id, _, _ := scache.endpoints["GET"].Resolve("/")
space, id, _ := scache.endpoints["GET"].Resolve("/")
assert.Nil(t, id)
assert.Equal(t, "", space)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/cache/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (tc *Target) SyncSubscriber(method, path string, eventType eventpkg.TypeNam
return nil
}

space, functionID, params, cors := root.Resolve(path)
space, functionID, params := root.Resolve(path)
if functionID == nil {
return nil
}
Expand All @@ -49,7 +49,6 @@ func (tc *Target) SyncSubscriber(method, path string, eventType eventpkg.TypeNam
Space: space,
FunctionID: *functionID,
Params: params,
CORS: cors,
}
}

Expand Down
23 changes: 8 additions & 15 deletions internal/pathtree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/serverless/event-gateway/function"
"github.com/serverless/event-gateway/subscription"
)

// Node is a data structure, inspired by prefix tree, used for routing HTTP requests in the Event Gateway. It's used for creating tree structure
Expand All @@ -16,7 +15,6 @@ type Node struct {
children map[string]*Node
space string
functionID *function.ID
cors *subscription.CORS
parameter string
isStatic bool
isParameter bool
Expand All @@ -35,11 +33,10 @@ type Params map[string]string

// AddRoute adds route to the tree. This function will panic in case of adding conflicting parameterized paths.
// nolint: gocyclo
func (n *Node) AddRoute(route string, space string, functionID function.ID, corsConfig *subscription.CORS) error {
func (n *Node) AddRoute(route string, space string, functionID function.ID) error {
if route == "/" {
n.space = space
n.functionID = &functionID
n.cors = corsConfig
return nil
}

Expand All @@ -62,7 +59,6 @@ func (n *Node) AddRoute(route string, space string, functionID function.ID, cors
if i == len(segments)-1 {
currentNode.children[segment].space = space
currentNode.children[segment].functionID = &functionID
currentNode.children[segment].cors = corsConfig
return nil
}
currentNode = currentNode.children[segment]
Expand Down Expand Up @@ -96,7 +92,6 @@ func (n *Node) AddRoute(route string, space string, functionID function.ID, cors
}
currentNode.children[segment].space = space
currentNode.children[segment].functionID = &functionID
currentNode.children[segment].cors = corsConfig
return nil
}
currentNode = currentNode.children[segment]
Expand All @@ -110,7 +105,6 @@ func (n *Node) DeleteRoute(route string) error {
if route == "/" {
n.space = ""
n.functionID = nil
n.cors = nil
return nil
}

Expand All @@ -129,7 +123,6 @@ func (n *Node) DeleteRoute(route string) error {
} else {
currentNode.children[segment].space = ""
currentNode.children[segment].functionID = nil
currentNode.children[segment].cors = nil
}

return nil
Expand All @@ -143,12 +136,12 @@ func (n *Node) DeleteRoute(route string) error {

// Resolve takes request URL path and traverse the tree trying find corresponding route.
// nolint: gocyclo
func (n *Node) Resolve(path string) (string, *function.ID, Params, *subscription.CORS) {
func (n *Node) Resolve(path string) (string, *function.ID, Params) {
if path == "/" {
if n.functionID != nil {
return n.space, n.functionID, nil, n.cors
return n.space, n.functionID, nil
}
return "", nil, nil, nil
return "", nil, nil
}

segments := toSegments(path)
Expand All @@ -162,7 +155,7 @@ func (n *Node) Resolve(path string) (string, *function.ID, Params, *subscription
// look for param
child, exists = first(currentNode.children)
if !exists || !(child.isParameter || child.isWildcard) {
return "", nil, nil, nil
return "", nil, nil
}
}
currentNode = child
Expand All @@ -174,15 +167,15 @@ func (n *Node) Resolve(path string) (string, *function.ID, Params, *subscription
if currentNode.isWildcard {
// add missing parts
params[currentNode.parameter] = strings.Join(segments[i:], "/")
return currentNode.space, currentNode.functionID, params, currentNode.cors
return currentNode.space, currentNode.functionID, params
}

if i == len(segments)-1 {
return currentNode.space, currentNode.functionID, params, currentNode.cors
return currentNode.space, currentNode.functionID, params
}
}

return "", nil, nil, nil
return "", nil, nil
}

func toSegments(route string) []string {
Expand Down
Loading

0 comments on commit 1ccc6c9

Please sign in to comment.