Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #168 from gobuffalo/fix-issues-with-open
Browse files Browse the repository at this point in the history
open works correctly with files w/o ext and open works with / prefix. fixes #159 fixes #161
  • Loading branch information
markbates authored Feb 27, 2019
2 parents 024a2ba + 98c7db8 commit 6f8539a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 21 deletions.
24 changes: 18 additions & 6 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ jobs:
vmImage: "vs2017-win2016"
strategy:
matrix:
go 1.9:
go_version: "1.9"
go 1.10:
go_version: "1.10"
go 1.11 (on):
Expand All @@ -19,6 +17,12 @@ jobs:
go 1.11 (off):
go_version: "1.11"
GO111MODULE: "off"
go 1.12 (on):
go_version: "1.11"
GO111MODULE: "on"
go 1.12 (off):
go_version: "1.11"
GO111MODULE: "off"
steps:
- template: azure-tests.yml

Expand All @@ -27,8 +31,6 @@ jobs:
vmImage: "macOS-10.13"
strategy:
matrix:
go 1.9:
go_version: "1.9"
go 1.10:
go_version: "1.10"
go 1.11 (on):
Expand All @@ -37,6 +39,12 @@ jobs:
go 1.11 (off):
go_version: "1.11"
GO111MODULE: "off"
go 1.12 (on):
go_version: "1.11"
GO111MODULE: "on"
go 1.12 (off):
go_version: "1.11"
GO111MODULE: "off"
steps:
- template: azure-tests.yml

Expand All @@ -45,8 +53,6 @@ jobs:
vmImage: "ubuntu-16.04"
strategy:
matrix:
go 1.9:
go_version: "1.9"
go 1.10:
go_version: "1.10"
go 1.11 (on):
Expand All @@ -55,5 +61,11 @@ jobs:
go 1.11 (off):
go_version: "1.11"
GO111MODULE: "off"
go 1.12 (on):
go_version: "1.11"
GO111MODULE: "on"
go 1.12 (off):
go_version: "1.11"
GO111MODULE: "off"
steps:
- template: azure-tests.yml
27 changes: 18 additions & 9 deletions v2/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"sort"
"strings"
Expand Down Expand Up @@ -118,7 +119,7 @@ func (b *Box) Has(name string) bool {

// HasDir returns true if the directory exists in the box
func (b *Box) HasDir(name string) bool {
oncer.Do("packr2/box/HasDir", func() {
oncer.Do("packr2/box/HasDir"+b.Name, func() {
for _, f := range b.List() {
d := filepath.Dir(f)
b.dirs.Store(d, true)
Expand All @@ -134,23 +135,31 @@ func (b *Box) HasDir(name string) bool {
// Open returns a File using the http.File interface
func (b *Box) Open(name string) (http.File, error) {
plog.Debug(b, "Open", "name", name)
if len(filepath.Ext(name)) == 0 {
if !b.HasDir(name) {
return nil, os.ErrNotExist
}
d, err := file.NewDir(name)
plog.Debug(b, "Open", "name", name, "dir", d)
return d, err
}
f, err := b.Resolve(name)
if err != nil {
if len(filepath.Ext(name)) == 0 {
return b.openWoExt(name)
}
return f, err
}
f, err = file.NewFileR(name, f)
plog.Debug(b, "Open", "name", f.Name(), "file", f.Name())
return f, err
}

func (b *Box) openWoExt(name string) (http.File, error) {
if !b.HasDir(name) {
id := path.Join(name, "index.html")
if b.Has(id) {
return b.Open(id)
}
return nil, os.ErrNotExist
}
d, err := file.NewDir(name)
plog.Debug(b, "Open", "name", name, "dir", d)
return d, err
}

// List shows "What's in the box?"
func (b *Box) List() []string {
var keys []string
Expand Down
16 changes: 10 additions & 6 deletions v2/box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,21 @@ func Test_Box_Open(t *testing.T) {
r := require.New(t)

d := resolver.NewInMemory(map[string]file.File{
"foo.txt": qfile("foo.txt", "foo!"),
"foo.txt": qfile("foo.txt", "foo!"),
"bar": qfile("bar", "bar!"),
"baz/index.html": qfile("baz", "baz!"),
})
box := New("Test_Box_Open", "./templates")

box.SetResolver("foo.txt", d)
box.DefaultResolver = d

f, err := box.Open("foo.txt")
r.NoError(err)
r.NotZero(f)
for _, x := range []string{"foo.txt", "/foo.txt", "bar", "/bar", "baz", "/baz"} {
f, err := box.Open(x)
r.NoError(err)
r.NotZero(f)
}

f, err = box.Open("idontexist.txt")
f, err := box.Open("idontexist.txt")
r.Error(err)
r.Zero(f)
}
Expand Down
21 changes: 21 additions & 0 deletions v2/http_box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@ func Test_HTTPBox_NotFound(t *testing.T) {
r.Equal(404, res.Code)
}

func Test_HTTPBox_Handles_IndexHTML_Nested(t *testing.T) {
r := require.New(t)

box := New("Test_HTTPBox_Handles_IndexHTML_Nested", "!")
box.AddString("foo/index.html", "foo")

mux := http.NewServeMux()
mux.Handle("/", http.FileServer(box))

req, err := http.NewRequest("GET", "/foo", nil)
r.NoError(err)

res := httptest.NewRecorder()

mux.ServeHTTP(res, req)

r.Equal(200, res.Code)

r.Equal("foo", strings.TrimSpace(res.Body.String()))
}

func Test_HTTPBox_Handles_IndexHTML(t *testing.T) {
r := require.New(t)

Expand Down

0 comments on commit 6f8539a

Please sign in to comment.