Skip to content

Commit

Permalink
index.html now gets resolved for any subfolder too (+tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
coderofsalvation committed Dec 29, 2023
1 parent 314f3d9 commit c3bb29c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions application/lambda/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ func (local *localLambda) writeStaticFile(path string, out io.Writer) error {
if err != nil {
return err
}
dir, err := f.Readdir(1)
if len(dir) > 0 {
f.Close()
return local.writeStaticFile( path + "/index.html", out )
}
defer f.Close()
_, err = io.Copy(out, f)
return err
Expand Down
16 changes: 16 additions & 0 deletions application/lambda/lambda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func TestStaticFile(t *testing.T) {
require.NoError(t, os.WriteFile(filepath.Join(d, "static", "foo", "foo"), []byte("foo page"), 0755))
require.NoError(t, os.Mkdir(filepath.Join(d, "static", "foo", "bar"), 0755))
require.NoError(t, os.WriteFile(filepath.Join(d, "static", "foo", "bar", "bar"), []byte("bar page"), 0755))
require.NoError(t, os.WriteFile(filepath.Join(d, "static", "foo","index.html"), []byte("sub index page"), 0755))

fn, err := DummyPublic(d, "cat", "-")
require.NoError(t, err)
Expand Down Expand Up @@ -189,6 +190,21 @@ func TestStaticFile(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "bar page", string(content))
})
t.Run("sub path with index.html served (no trailing slash)", func(t *testing.T) {
content, err := testRequest(fn, http.MethodGet, "/f/foo", nil)
require.NoError(t, err)
assert.Equal(t, "sub index page", string(content))
})
t.Run("sub path with index.html served (with trailing slash)", func(t *testing.T) {
content, err := testRequest(fn, http.MethodGet, "/f/foo/", nil)
require.NoError(t, err)
assert.Equal(t, "sub index page", string(content))
})
t.Run("sub path with index.html served (with trailing slash + index.html)", func(t *testing.T) {
content, err := testRequest(fn, http.MethodGet, "/f/foo/index.html", nil)
require.NoError(t, err)
assert.Equal(t, "sub index page", string(content))
})
}

func testRequest(fn application.Invokable, method string, path string, payload []byte) ([]byte, error) {
Expand Down

0 comments on commit c3bb29c

Please sign in to comment.