Skip to content

Commit

Permalink
enginex url tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxence-charriere committed Oct 17, 2023
1 parent cfc11af commit 59a48f2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
5 changes: 1 addition & 4 deletions pkg/app/enginex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
50 changes: 47 additions & 3 deletions pkg/app/enginex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}

0 comments on commit 59a48f2

Please sign in to comment.