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

filesystem: Improve test coverage #50

Merged
merged 1 commit into from
Jan 3, 2025
Merged
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
14 changes: 14 additions & 0 deletions pkg/fileserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,17 @@ func TestLoggingWrapper(t *testing.T) {
assert.Contains(output, "status=200")
assert.Contains(output, "path=\"/test.html\"")
}

func TestUseSSL(t *testing.T) {
fs := NewFileserver("../filesystem/testdata", true)

assert := assert.New(t)

assert.Empty(fs.SSL, "New server should not have ssl config yet")

fs.UseSSL("test.crt", "test.key")

assert.True(fs.SSL.Enabled, "SSL should be enabled")
assert.Equal("test.crt", fs.SSL.Certificate, "SSL certificate should match")
assert.Equal("test.key", fs.SSL.Key, "SSL key should match")
}
5 changes: 1 addition & 4 deletions pkg/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ func (ifs IndexlessFilesystem) Open(path string) (http.File, error) {
index := path + "/index.html"
_, err := ifs.fs.Open(index)
if err != nil {
closeErr := f.Close()
if closeErr != nil {
return nil, closeErr
}
f.Close()
return nil, err
}
}
Expand Down
16 changes: 11 additions & 5 deletions pkg/filesystem/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ func TestIndexlessFilesystem(t *testing.T) {
}
}
})
t.Run("InvalidPath", func(t *testing.T) {
assert := assert.New(t)

f, err := fs.Open("not-a-valid-path")
assert.Nil(f)
assert.Error(err)
})

testMatrix := map[string]string{
"File": "/test.html",
Expand All @@ -68,11 +75,10 @@ func TestIndexlessFilesystem(t *testing.T) {
for name, path := range testMatrix {
t.Run(name, func(t *testing.T) {
f, err := fs.Open(path)
if assert.Nil(t, err) {
err = f.Close()
if err != nil {
t.Fatalf("Unexpected error closing file: %v", err)
}
if assert.NoError(t, err) {
t.Cleanup(func() {
f.Close()
})
}
})
}
Expand Down
Loading