-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publish system events on space prefixed path for hosted version (#485)
- Loading branch information
Showing
6 changed files
with
107 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// +build !hosted | ||
|
||
package router | ||
|
||
func extractPath(host, path string) string { | ||
return path | ||
} | ||
|
||
func systemPathFromSpace(space string) string { | ||
return basePath | ||
} | ||
|
||
func systemPathFromPath(path string) string { | ||
return basePath | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// +build hosted | ||
|
||
package router | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// extractPath extracts path from hosted EG host name (<space>.eventgateway([a-z-]*)?.io|slsgateway.com) | ||
func extractPath(host, path string) string { | ||
subdomain := strings.Split(host, ".")[0] | ||
return basePath + subdomain + path | ||
} | ||
|
||
func systemPathFromSpace(space string) string { | ||
return basePath + space + "/" | ||
} | ||
|
||
// systemPathFromPath constructs path from path on which event was emitted. Helpful for "event.received" system event. | ||
func systemPathFromPath(path string) string { | ||
return basePath + strings.Split(path, "/")[1] + "/" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// +build hosted | ||
|
||
package router_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/serverless/event-gateway/event" | ||
"github.com/serverless/event-gateway/plugin" | ||
"github.com/serverless/event-gateway/router" | ||
"github.com/serverless/event-gateway/router/mock" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestHostedRouterServeHTTP(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
target := mock.NewMockTargeter(ctrl) | ||
|
||
t.Run("emit system event 'event received' on path prefixed with space", func(t *testing.T) { | ||
target.EXPECT().CORS(gomock.Any(), gomock.Any()).Return(nil) | ||
target.EXPECT().SyncSubscriber(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) | ||
target.EXPECT().AsyncSubscribers(gomock.Any(), gomock.Any(), event.TypeName("http.request")).Return([]router.AsyncSubscriber{}) | ||
|
||
target.EXPECT().AsyncSubscribers(http.MethodPost, "/custom/", event.SystemEventReceivedType).Return([]router.AsyncSubscriber{}) | ||
|
||
router := setupTestRouter(target) | ||
req, _ := http.NewRequest(http.MethodGet, "https://custom.slsgateway.com/foo/bar", nil) | ||
recorder := httptest.NewRecorder() | ||
router.ServeHTTP(recorder, req) | ||
}) | ||
|
||
t.Run("extract path from hosted domain", func(t *testing.T) { | ||
target.EXPECT().CORS(gomock.Any(), gomock.Any()).Return(nil) | ||
target.EXPECT().AsyncSubscribers(gomock.Any(), gomock.Any(), event.SystemEventReceivedType).Return([]router.AsyncSubscriber{}) | ||
|
||
target.EXPECT().SyncSubscriber(http.MethodGet, "/custom/test", event.TypeName("http.request")).Return(nil) | ||
target.EXPECT().AsyncSubscribers(http.MethodGet, "/custom/test", event.TypeName("http.request")).Return([]router.AsyncSubscriber{}) | ||
|
||
router := setupTestRouter(target) | ||
req, _ := http.NewRequest(http.MethodGet, "https://custom.slsgateway.com/test", nil) | ||
recorder := httptest.NewRecorder() | ||
router.ServeHTTP(recorder, req) | ||
}) | ||
} | ||
|
||
func setupTestRouter(target router.Targeter) *router.Router { | ||
log := zap.NewNop() | ||
plugins := plugin.NewManager([]string{}, log) | ||
router := router.New(10, 10, target, plugins, log) | ||
router.StartWorkers() | ||
return router | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters