Skip to content

Commit

Permalink
enhancement: add asset server tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fschade committed Feb 26, 2024
1 parent 0576e48 commit 07a1098
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
125 changes: 125 additions & 0 deletions services/web/pkg/assets/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package assets_test

import (
"fmt"
"io"
"io/fs"
"net/http"
"net/http/httptest"
"testing"
"testing/fstest"

"github.com/onsi/gomega"

"github.com/owncloud/ocis/v2/services/web/pkg/assets"
)

func TestFileServer(t *testing.T) {
g := gomega.NewWithT(t)
recorderStatus := func(s int) string {
return fmt.Sprintf("%03d %s", s, http.StatusText(s))
}

{
s := assets.FileServer(fstest.MapFS{})
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/foo", nil)
//defer req.Body.Close()
s.ServeHTTP(w, req)
res := w.Result()
defer res.Body.Close()

g.Expect(res.Status).To(gomega.Equal(recorderStatus(http.StatusNotFound)))
}

for _, tt := range []struct {
name string
url string
fs fstest.MapFS
expected string
}{
{
name: "not found fallback",
url: "/index.txt",
fs: fstest.MapFS{
"index.html": &fstest.MapFile{
Data: []byte("index file content"),
},
},
expected: `<html><head><base href="/"/></head><body>index file content</body></html>`,
},
{
name: "directory fallback",
url: "/some-folder",
fs: fstest.MapFS{
"some-folder": &fstest.MapFile{
Mode: fs.ModeDir,
},
"index.html": &fstest.MapFile{
Data: []byte("index file content"),
},
},
expected: `<html><head><base href="/"/></head><body>index file content</body></html>`,
},
{
name: "index.html",
url: "/index.html",
fs: fstest.MapFS{
"index.html": &fstest.MapFile{
Data: []byte("index file content"),
},
},
expected: `<html><head><base href="/"/></head><body>index file content</body></html>`,
},
{
name: "oidc-callback.html",
url: "/oidc-callback.html",
fs: fstest.MapFS{
"index.html": &fstest.MapFile{
Data: []byte("oidc-callback file content"),
},
},
expected: `<html><head><base href="/"/></head><body>oidc-callback file content</body></html>`,
},
{
name: "oidc-silent-redirect.html",
url: "/oidc-silent-redirect.html",
fs: fstest.MapFS{
"index.html": &fstest.MapFile{
Data: []byte("oidc-silent-redirect file content"),
},
},
expected: `<html><head><base href="/"/></head><body>oidc-silent-redirect file content</body></html>`,
},
{
name: "some-file.txt",
url: "/some-file.txt",
fs: fstest.MapFS{
"some-file.txt": &fstest.MapFile{
Data: []byte("some file content"),
},
},
expected: "some file content",
},
} {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

w := httptest.NewRecorder()
req := httptest.NewRequest("GET", tt.url, nil)
assets.FileServer(tt.fs).ServeHTTP(w, req)
res := w.Result()
defer res.Body.Close()

g.Expect(res.Status).To(gomega.Equal(recorderStatus(http.StatusOK)))

data, err := io.ReadAll(res.Body)
g.Expect(err).ToNot(gomega.HaveOccurred())
g.Expect(string(data)).To(gomega.Equal(tt.expected))

})
}

}
2 changes: 2 additions & 0 deletions services/web/pkg/config/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func ParseConfig(cfg *config.Config) error {
}

// web apps are a special case, as they are not part of the main config, but are loaded from a separate config file
// fixme: loading from a file named web.apps.yml indicates the system is only capable of loading static web apps,
// instead i prefer to have something like extensions.yml to be able to also inject proxy routes etc.
_, err = ociscfg.BindSourcesToStructs(cfg.Service.Name+".apps", &cfg.Apps)
if err != nil {
return err
Expand Down

0 comments on commit 07a1098

Please sign in to comment.