Skip to content

Commit

Permalink
Fix path creation for hosted version (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthenw committed Jul 24, 2018
1 parent 2dfd936 commit 08b8053
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
2 changes: 1 addition & 1 deletion router/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ func systemPathFromSpace(space string) string {
return basePath
}

func systemPathFromPath(path string) string {
func systemPathFromURL(host, path string) string {
return basePath
}
16 changes: 10 additions & 6 deletions router/path_hosted.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ func init() {
}

func extractPath(host, path string) string {
extracted := path
if hostedDomainPattern.Copy().MatchString(host) {
subdomain := strings.Split(host, ".")[0]
extracted = basePath + subdomain + path
return basePath + subdomain + path
}
return extracted
return 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] + "/"
// systemPathFromURL constructs system event path based on hostname and path
// on which the event was emitted. Helpful for "event.received" system event.
func systemPathFromURL(host, path string) string {
if hostedDomainPattern.Copy().MatchString(host) {
segment := strings.Split(path, "/")[1]
return basePath + segment + "/"
}
return basePath
}
8 changes: 4 additions & 4 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

router.log.Debug("Event received.", zap.String("path", path), zap.Object("event", event))
err = router.emitSystemEventReceived(path, *event, r.Header)
err = router.emitSystemEventReceived(path, *event, r)
if err != nil {
router.log.Debug("Event processing stopped because sync plugin subscription returned an error.",
zap.Object("event", event),
Expand Down Expand Up @@ -440,13 +440,13 @@ func (router *Router) processEvent(e backlogEvent) {
metricEventsProcessed.WithLabelValues(e.space, "custom").Inc()
}

func (router *Router) emitSystemEventReceived(path string, event eventpkg.Event, header http.Header) error {
func (router *Router) emitSystemEventReceived(path string, event eventpkg.Event, r *http.Request) error {
system := eventpkg.New(
eventpkg.SystemEventReceivedType,
mimeJSON,
eventpkg.SystemEventReceivedData{Path: path, Event: event, Headers: ihttp.FlattenHeader(header)},
eventpkg.SystemEventReceivedData{Path: path, Event: event, Headers: ihttp.FlattenHeader(r.Header)},
)
router.handleAsyncSubscriptions(http.MethodPost, systemPathFromPath(path), *system, nil)
router.handleAsyncSubscriptions(http.MethodPost, systemPathFromURL(r.Host, path), *system, nil)
return router.plugins.React(system)
}

Expand Down
28 changes: 27 additions & 1 deletion router/router_hosted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestHostedRouterServeHTTP(t *testing.T) {
defer ctrl.Finish()
target := mock.NewMockTargeter(ctrl)

t.Run("emit system event 'event received' on path prefixed with space", func(t *testing.T) {
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{})
Expand All @@ -45,6 +45,32 @@ func TestHostedRouterServeHTTP(t *testing.T) {
recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, req)
})

t.Run("if not hosted EG should fallback to full path", 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, "/foo/bar", event.TypeName("http.request")).Return(nil)
target.EXPECT().AsyncSubscribers(http.MethodGet, "/foo/bar", event.TypeName("http.request")).Return([]router.AsyncSubscriber{})

router := setupTestRouter(target)
req, _ := http.NewRequest(http.MethodGet, "https://127.0.0.1/foo/bar", nil)
recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, req)
})

t.Run("if not hosted EG should fallback to / for 'event.received' system event", 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, "/", event.SystemEventReceivedType).Return([]router.AsyncSubscriber{})

router := setupTestRouter(target)
req, _ := http.NewRequest(http.MethodGet, "https://127.0.0.1/test", nil)
recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, req)
})
}

func setupTestRouter(target router.Targeter) *router.Router {
Expand Down

0 comments on commit 08b8053

Please sign in to comment.