From c3bb29cfde1df28c831a8e5d51cf7dbe7d839b20 Mon Sep 17 00:00:00 2001 From: Leon van Kammen Date: Fri, 29 Dec 2023 08:35:00 +0000 Subject: [PATCH] index.html now gets resolved for any subfolder too (+tests) --- application/lambda/lambda.go | 5 +++++ application/lambda/lambda_test.go | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/application/lambda/lambda.go b/application/lambda/lambda.go index 9e98f44..0f5359f 100644 --- a/application/lambda/lambda.go +++ b/application/lambda/lambda.go @@ -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 diff --git a/application/lambda/lambda_test.go b/application/lambda/lambda_test.go index 211ccd8..7f3d071 100644 --- a/application/lambda/lambda_test.go +++ b/application/lambda/lambda_test.go @@ -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) @@ -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) {