Skip to content

Commit

Permalink
* Also return content with status 404 if the file is not found.
Browse files Browse the repository at this point in the history
* Search for index.html, if path is a directory.
  • Loading branch information
Sebastian Mancke committed Nov 23, 2016
1 parent 997a9db commit d4d0c21
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
18 changes: 15 additions & 3 deletions composition/file_content_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/tarent/lib-compose/logging"
"os"
"path/filepath"
"strings"
"time"
)
Expand All @@ -25,8 +26,19 @@ func (loader *FileContentLoader) Load(fd *FetchDefinition) (Content, error) {
if fd.RespProc != nil {
return nil, ResponseProcessorsNotApplicable
}
filename := strings.TrimPrefix(fd.URL, FileURLPrefix)
f, err := os.Open(filename)

path := strings.TrimPrefix(fd.URL, FileURLPrefix)
stat, err := os.Stat(path)
if err == nil && stat.IsDir() {
path = filepath.Join(path, "index.html")
} else if os.IsNotExist(err) {
c := NewMemoryContent()
c.url = fd.URL
c.httpStatusCode = 404
return c, err
}

f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("error opening file %v: %v", fd.URL, err)
}
Expand All @@ -35,7 +47,7 @@ func (loader *FileContentLoader) Load(fd *FetchDefinition) (Content, error) {
c.url = fd.URL
c.httpStatusCode = 200

if strings.HasSuffix(filename, ".html") {
if strings.HasSuffix(path, ".html") {
parsingStart := time.Now()
err := loader.parser.Parse(c, f)
logging.Logger.
Expand Down
39 changes: 37 additions & 2 deletions composition/file_content_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,29 @@ func Test_FileContentLoader_LoadHTML(t *testing.T) {

loader := NewFileContentLoader()
c, err := loader.Load(NewFetchDefinition(FileURLPrefix + fileName))
a.Equal(FileURLPrefix+fileName, c.URL())
assertContentLoaded(t, c, err, "some head content")
}

func Test_FileContentLoader_LoadIndexForDirectory(t *testing.T) {
a := assert.New(t)

dir := os.TempDir()
fileName := filepath.Join(dir, "index.html")

err := ioutil.WriteFile(fileName, []byte("<html><head>some head content</head></html>"), 0660)
a.NoError(err)

loader := NewFileContentLoader()
c, err := loader.Load(NewFetchDefinition(FileURLPrefix + dir))
assertContentLoaded(t, c, err, "some head content")
}

func assertContentLoaded(t *testing.T, c Content, err error, s string) {
a := assert.New(t)
a.NoError(err)
a.NotNil(c)
a.Nil(c.Reader())
a.Equal(FileURLPrefix+fileName, c.URL())
eqFragment(t, "some head content", c.Head())
a.Equal(0, len(c.Body()))
}
Expand All @@ -53,9 +72,25 @@ func Test_FileContentLoader_LoadStream(t *testing.T) {
func Test_FileContentLoader_LoadError(t *testing.T) {
a := assert.New(t)

// create a file without read permissions
f, err := ioutil.TempFile("", "lib-compose-test")
a.NoError(err)
a.NoError(f.Chmod(os.FileMode(0)))
f.Close()

loader := NewFileContentLoader()
_, err := loader.Load(NewFetchDefinition("/tmp/some/non/existing/path"))
_, err = loader.Load(NewFetchDefinition(f.Name()))
a.Error(err)
}

func Test_FileContentLoader_Return404IfFileNotFound(t *testing.T) {
a := assert.New(t)

loader := NewFileContentLoader()
c, err := loader.Load(NewFetchDefinition("/tmp/some/non/existing/path"))
a.NotNil(c)
a.Error(err)
a.Equal(404, c.HttpStatusCode())
}

func Test_FileContentLoader_RequestProcessor(t *testing.T) {
Expand Down

0 comments on commit d4d0c21

Please sign in to comment.