Skip to content

Commit

Permalink
fix resources:directory to not require a resources subdir
Browse files Browse the repository at this point in the history
  • Loading branch information
roelarents committed Nov 25, 2024
1 parent 074764d commit 46be16c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
5 changes: 2 additions & 3 deletions internal/engine/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"log"
"net/http"
"net/url"
"strings"

"github.com/go-chi/chi/v5"
)
Expand All @@ -16,8 +15,8 @@ func newResourcesEndpoint(e *Engine) {
return
}
if res.Directory != nil && *res.Directory != "" {
resourcesPath := strings.TrimSuffix(*res.Directory, "/resources")
e.Router.Handle("/resources/*", http.FileServer(http.Dir(resourcesPath)))
resourcesPath := *res.Directory
e.Router.Handle("/resources/*", http.StripPrefix("/resources", http.FileServer(http.Dir(resourcesPath))))
} else if res.URL != nil && res.URL.String() != "" {
e.Router.Get("/resources/*", proxy(e.ReverseProxy, res.URL.String()))
}
Expand Down
46 changes: 46 additions & 0 deletions internal/engine/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,52 @@ func (m *MockReverseProxy) Proxy(w http.ResponseWriter, r *http.Request, target
m.Called(w, r, target, prefer204, overwrite)
}

func TestDir(t *testing.T) {
tests := []struct {
name string
resourcesDir string
urlParam string
expectedStatus int
expectedLog string
}{
{
name: "valid url",
resourcesDir: "docs",
urlParam: "foo.txt",
expectedStatus: http.StatusOK,
expectedLog: "",
},
{
name: "invalid url",
resourcesDir: "docs",
urlParam: "non-existing-file",
expectedStatus: http.StatusNotFound,
expectedLog: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// given
engine, err := NewEngine("internal/engine/testdata/config_resources_dir.yaml", "", false, true)
assert.NoError(t, err)
r := httptest.NewRequest(http.MethodGet, "/resources/"+tt.urlParam, nil)
w := httptest.NewRecorder()
var logOutput strings.Builder
log.SetOutput(&logOutput)

// when
newResourcesEndpoint(engine)
engine.Router.ServeHTTP(w, r)

// then
assert.Equal(t, tt.expectedStatus, w.Result().StatusCode)
if tt.expectedLog != "" {
assert.Contains(t, logOutput.String(), tt.expectedLog)
}
})
}
}

func TestProxy(t *testing.T) {
tests := []struct {
name string
Expand Down
11 changes: 11 additions & 0 deletions internal/engine/testdata/config_resources_dir.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
version: 1.0.2
title: Minimal OGC API
abstract: This is a minimal OGC API, offering only OGC API Common
baseUrl: http://localhost:8080
serviceIdentifier: Min
license:
name: MIT
url: https://www.tldrlegal.com/license/mit-license
resources:
directory: internal/engine/testdata/test-resources-dir
1 change: 1 addition & 0 deletions internal/engine/testdata/test-resources-dir/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar

0 comments on commit 46be16c

Please sign in to comment.