From 59a48f2ce3ae6ba73ffb8db966fe78c23e72b209 Mon Sep 17 00:00:00 2001 From: Maxence Charriere Date: Tue, 17 Oct 2023 15:28:52 +0800 Subject: [PATCH] enginex url tests --- pkg/app/enginex.go | 5 +---- pkg/app/enginex_test.go | 50 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/pkg/app/enginex.go b/pkg/app/enginex.go index 444046e1..8400f870 100644 --- a/pkg/app/enginex.go +++ b/pkg/app/enginex.go @@ -110,10 +110,7 @@ func (e *engineX) initBrowser() { } func (e *engineX) externalNavigation(v *url.URL) bool { - return v.Host != "" && - !strings.HasPrefix(v.Host, "127.0.0.1") && - !strings.HasPrefix(Window().URL().Host, "localhost") && - v.Host != "" && v.Host != Window().URL().Host + return v.Host != e.originPage.URL().Host } func (e *engineX) mailTo(v *url.URL) bool { diff --git a/pkg/app/enginex_test.go b/pkg/app/enginex_test.go index aa3eb4e3..feaae623 100644 --- a/pkg/app/enginex_test.go +++ b/pkg/app/enginex_test.go @@ -125,14 +125,58 @@ func TestEngineXNavigate(t *testing.T) { }) } +func TestEngineXInternalURL(t *testing.T) { + t.Run("destination is internal URL", func(t *testing.T) { + os.Setenv("GOAPP_INTERNAL_URLS", `["https://murlok.io"]`) + defer os.Unsetenv("GOAPP_INTERNAL_URLS") + + e := newTestEngine() + destination, _ := url.Parse("https://murlok.io/warrior") + require.True(t, e.internalURL(destination)) + }) + + t.Run("destination is internal URL", func(t *testing.T) { + e := newTestEngine() + destination, _ := url.Parse("https://murlok.io/warrior") + require.False(t, e.internalURL(destination)) + }) +} + +func TestEngineXMailTo(t *testing.T) { + t.Run("destination is mailto", func(t *testing.T) { + e := newTestEngine() + destination, _ := url.Parse("mailto:maxence@goapp.dev") + require.True(t, e.mailTo(destination)) + }) + + t.Run("destination is not mailto", func(t *testing.T) { + e := newTestEngine() + destination, _ := url.Parse("/hello") + require.False(t, e.mailTo(destination)) + }) +} + +func TestEngineXExternalNavigation(t *testing.T) { + t.Run("destination is external navigation", func(t *testing.T) { + e := newTestEngine() + destination, _ := url.Parse("https://murlok.io") + require.True(t, e.externalNavigation(destination)) + }) + + t.Run("destination is not external navigation", func(t *testing.T) { + e := newTestEngine() + destination, _ := url.Parse("/hello") + require.False(t, e.externalNavigation(destination)) + }) +} + func newTestEngine() *engineX { - url, _ := url.Parse("/") + origin, _ := url.Parse("/") routes := makeRouter() - return newEngineX(context.Background(), &routes, nil, - url, + origin, Body, ) }