Skip to content

Commit

Permalink
Hosted file precedence over path URI
Browse files Browse the repository at this point in the history
  • Loading branch information
t94j0 committed Mar 11, 2021
1 parent d4f6d59 commit 17cd975
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions satellite/path/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func (paths *Paths) Match(uri string) (*Path, bool) {
for _, v := range paths.list {
g := glob.MustCompile(v.Path, '/')
if g.Match(uri) {
if v.HostedFile != "" {
return v, true
}
if _, err := os.Stat(path.Join(paths.base, v.Path)); err != nil {
v.HostedFile = v.Path
} else {
Expand Down
35 changes: 35 additions & 0 deletions satellite/path/paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,41 @@ func TestNew_one(t *testing.T) {
}
}

func TestNew_different_hosted(t *testing.T) {
tmpdir, err := NewTempDir()
if err != nil {
t.Error(err)
}
defer tmpdir.Close()
tmpdir.CreatePathList(`- path: /index.html
hosted_file: abc`)
tmpdir.CreateFile("abc", Sentinal)

paths, err := NewDefaultTest(tmpdir.Path)
if err != nil {
t.Error(err)
}

req := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
didMatch, err := paths.MatchAndServe(w, req)
if err != nil {
t.Error(err)
}

if !didMatch {
t.Fail()
}

data, err := ioutil.ReadAll(w.Result().Body)
if err != nil {
t.Fail()
}
if string(data) != Sentinal {
t.Fail()
}
}

func TestNew_proxy(t *testing.T) {
tmpdir, err := NewTempDir()
if err != nil {
Expand Down

0 comments on commit 17cd975

Please sign in to comment.