From 46be16c9388abf04164f985c256cbf9581293a5f Mon Sep 17 00:00:00 2001 From: Roel Arents Date: Mon, 25 Nov 2024 16:22:50 +0100 Subject: [PATCH] fix resources:directory to not require a resources subdir --- internal/engine/resources.go | 5 +- internal/engine/resources_test.go | 46 +++++++++++++++++++ .../engine/testdata/config_resources_dir.yaml | 11 +++++ .../testdata/test-resources-dir/foo.txt | 1 + 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 internal/engine/testdata/config_resources_dir.yaml create mode 100644 internal/engine/testdata/test-resources-dir/foo.txt diff --git a/internal/engine/resources.go b/internal/engine/resources.go index 55bfef4a..0da4cd54 100644 --- a/internal/engine/resources.go +++ b/internal/engine/resources.go @@ -4,7 +4,6 @@ import ( "log" "net/http" "net/url" - "strings" "github.com/go-chi/chi/v5" ) @@ -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())) } diff --git a/internal/engine/resources_test.go b/internal/engine/resources_test.go index eab5b6e2..fb243730 100644 --- a/internal/engine/resources_test.go +++ b/internal/engine/resources_test.go @@ -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 diff --git a/internal/engine/testdata/config_resources_dir.yaml b/internal/engine/testdata/config_resources_dir.yaml new file mode 100644 index 00000000..ccaa8c28 --- /dev/null +++ b/internal/engine/testdata/config_resources_dir.yaml @@ -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 diff --git a/internal/engine/testdata/test-resources-dir/foo.txt b/internal/engine/testdata/test-resources-dir/foo.txt new file mode 100644 index 00000000..ba0e162e --- /dev/null +++ b/internal/engine/testdata/test-resources-dir/foo.txt @@ -0,0 +1 @@ +bar \ No newline at end of file