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

Commit 08b8053

Browse files
authored
Fix path creation for hosted version (#491)
1 parent 2dfd936 commit 08b8053

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

router/path.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ func systemPathFromSpace(space string) string {
1010
return basePath
1111
}
1212

13-
func systemPathFromPath(path string) string {
13+
func systemPathFromURL(host, path string) string {
1414
return basePath
1515
}

router/path_hosted.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,23 @@ func init() {
1414
}
1515

1616
func extractPath(host, path string) string {
17-
extracted := path
1817
if hostedDomainPattern.Copy().MatchString(host) {
1918
subdomain := strings.Split(host, ".")[0]
20-
extracted = basePath + subdomain + path
19+
return basePath + subdomain + path
2120
}
22-
return extracted
21+
return path
2322
}
2423

2524
func systemPathFromSpace(space string) string {
2625
return basePath + space + "/"
2726
}
2827

29-
// systemPathFromPath constructs path from path on which event was emitted. Helpful for "event.received" system event.
30-
func systemPathFromPath(path string) string {
31-
return basePath + strings.Split(path, "/")[1] + "/"
28+
// systemPathFromURL constructs system event path based on hostname and path
29+
// on which the event was emitted. Helpful for "event.received" system event.
30+
func systemPathFromURL(host, path string) string {
31+
if hostedDomainPattern.Copy().MatchString(host) {
32+
segment := strings.Split(path, "/")[1]
33+
return basePath + segment + "/"
34+
}
35+
return basePath
3236
}

router/router.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
8282
}
8383

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

443-
func (router *Router) emitSystemEventReceived(path string, event eventpkg.Event, header http.Header) error {
443+
func (router *Router) emitSystemEventReceived(path string, event eventpkg.Event, r *http.Request) error {
444444
system := eventpkg.New(
445445
eventpkg.SystemEventReceivedType,
446446
mimeJSON,
447-
eventpkg.SystemEventReceivedData{Path: path, Event: event, Headers: ihttp.FlattenHeader(header)},
447+
eventpkg.SystemEventReceivedData{Path: path, Event: event, Headers: ihttp.FlattenHeader(r.Header)},
448448
)
449-
router.handleAsyncSubscriptions(http.MethodPost, systemPathFromPath(path), *system, nil)
449+
router.handleAsyncSubscriptions(http.MethodPost, systemPathFromURL(r.Host, path), *system, nil)
450450
return router.plugins.React(system)
451451
}
452452

router/router_hosted_test.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestHostedRouterServeHTTP(t *testing.T) {
2020
defer ctrl.Finish()
2121
target := mock.NewMockTargeter(ctrl)
2222

23-
t.Run("emit system event 'event received' on path prefixed with space", func(t *testing.T) {
23+
t.Run("emit system event 'event.received' on path prefixed with space", func(t *testing.T) {
2424
target.EXPECT().CORS(gomock.Any(), gomock.Any()).Return(nil)
2525
target.EXPECT().SyncSubscriber(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
2626
target.EXPECT().AsyncSubscribers(gomock.Any(), gomock.Any(), event.TypeName("http.request")).Return([]router.AsyncSubscriber{})
@@ -45,6 +45,32 @@ func TestHostedRouterServeHTTP(t *testing.T) {
4545
recorder := httptest.NewRecorder()
4646
router.ServeHTTP(recorder, req)
4747
})
48+
49+
t.Run("if not hosted EG should fallback to full path", func(t *testing.T) {
50+
target.EXPECT().CORS(gomock.Any(), gomock.Any()).Return(nil)
51+
target.EXPECT().AsyncSubscribers(gomock.Any(), gomock.Any(), event.SystemEventReceivedType).Return([]router.AsyncSubscriber{})
52+
53+
target.EXPECT().SyncSubscriber(http.MethodGet, "/foo/bar", event.TypeName("http.request")).Return(nil)
54+
target.EXPECT().AsyncSubscribers(http.MethodGet, "/foo/bar", event.TypeName("http.request")).Return([]router.AsyncSubscriber{})
55+
56+
router := setupTestRouter(target)
57+
req, _ := http.NewRequest(http.MethodGet, "https://127.0.0.1/foo/bar", nil)
58+
recorder := httptest.NewRecorder()
59+
router.ServeHTTP(recorder, req)
60+
})
61+
62+
t.Run("if not hosted EG should fallback to / for 'event.received' system event", func(t *testing.T) {
63+
target.EXPECT().CORS(gomock.Any(), gomock.Any()).Return(nil)
64+
target.EXPECT().SyncSubscriber(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
65+
target.EXPECT().AsyncSubscribers(gomock.Any(), gomock.Any(), event.TypeName("http.request")).Return([]router.AsyncSubscriber{})
66+
67+
target.EXPECT().AsyncSubscribers(http.MethodPost, "/", event.SystemEventReceivedType).Return([]router.AsyncSubscriber{})
68+
69+
router := setupTestRouter(target)
70+
req, _ := http.NewRequest(http.MethodGet, "https://127.0.0.1/test", nil)
71+
recorder := httptest.NewRecorder()
72+
router.ServeHTTP(recorder, req)
73+
})
4874
}
4975

5076
func setupTestRouter(target router.Targeter) *router.Router {

0 commit comments

Comments
 (0)