Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix the (non)existence of a slash when staticFolder is set #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 )
}
Comment on lines +197 to +201
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like Readdir here is overkill 😺

Could you please use something like this

if _, err := os.Stat("/path/to/whatever"); errors.Is(err, os.ErrNotExist) {
  // path/to/whatever does not exist
}

Also, for path concatenation it's better to use filepath.Join - it will take care of few corner cases

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