|
| 1 | +// Copyright (C) 2026 l3montree GmbH |
| 2 | +// |
| 3 | +// This program is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU Affero General Public License as |
| 5 | +// published by the Free Software Foundation, either version 3 of the |
| 6 | +// License, or (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU Affero General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU Affero General Public License |
| 14 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +package dependencyfirewall |
| 17 | + |
| 18 | +import ( |
| 19 | + "net/http" |
| 20 | + "net/http/httptest" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/labstack/echo/v4" |
| 24 | +) |
| 25 | + |
| 26 | +func TestEnsureReadMethod(t *testing.T) { |
| 27 | + t.Run("allows GET", func(t *testing.T) { |
| 28 | + e := echo.New() |
| 29 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 30 | + rec := httptest.NewRecorder() |
| 31 | + c := e.NewContext(req, rec) |
| 32 | + |
| 33 | + if err := ensureReadMethod(c); err != nil { |
| 34 | + t.Fatalf("expected GET to be allowed, got error: %v", err) |
| 35 | + } |
| 36 | + }) |
| 37 | + |
| 38 | + t.Run("allows HEAD", func(t *testing.T) { |
| 39 | + e := echo.New() |
| 40 | + req := httptest.NewRequest(http.MethodHead, "/", nil) |
| 41 | + rec := httptest.NewRecorder() |
| 42 | + c := e.NewContext(req, rec) |
| 43 | + |
| 44 | + if err := ensureReadMethod(c); err != nil { |
| 45 | + t.Fatalf("expected HEAD to be allowed, got error: %v", err) |
| 46 | + } |
| 47 | + }) |
| 48 | + |
| 49 | + t.Run("rejects POST", func(t *testing.T) { |
| 50 | + e := echo.New() |
| 51 | + req := httptest.NewRequest(http.MethodPost, "/", nil) |
| 52 | + rec := httptest.NewRecorder() |
| 53 | + c := e.NewContext(req, rec) |
| 54 | + |
| 55 | + err := ensureReadMethod(c) |
| 56 | + if err == nil { |
| 57 | + t.Fatal("expected POST to be rejected") |
| 58 | + } |
| 59 | + |
| 60 | + httpErr, ok := err.(*echo.HTTPError) |
| 61 | + if !ok { |
| 62 | + t.Fatalf("expected *echo.HTTPError, got %T", err) |
| 63 | + } |
| 64 | + if httpErr.Code != http.StatusMethodNotAllowed { |
| 65 | + t.Fatalf("expected status %d, got %d", http.StatusMethodNotAllowed, httpErr.Code) |
| 66 | + } |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +func TestPassthroughUpstreamResponse(t *testing.T) { |
| 71 | + e := echo.New() |
| 72 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 73 | + rec := httptest.NewRecorder() |
| 74 | + c := e.NewContext(req, rec) |
| 75 | + |
| 76 | + d := &DependencyProxyController{} |
| 77 | + headers := http.Header{} |
| 78 | + headers.Add("X-Test", "a") |
| 79 | + headers.Add("X-Test", "b") |
| 80 | + headers.Set("Content-Type", "application/json") |
| 81 | + body := []byte(`{"ok":true}`) |
| 82 | + |
| 83 | + if err := d.passthroughUpstreamResponse(c, headers, http.StatusAccepted, body); err != nil { |
| 84 | + t.Fatalf("expected no error, got %v", err) |
| 85 | + } |
| 86 | + |
| 87 | + if rec.Code != http.StatusAccepted { |
| 88 | + t.Fatalf("expected status %d, got %d", http.StatusAccepted, rec.Code) |
| 89 | + } |
| 90 | + if got := rec.Body.String(); got != string(body) { |
| 91 | + t.Fatalf("expected body %q, got %q", string(body), got) |
| 92 | + } |
| 93 | + if got := rec.Header().Values("X-Test"); len(got) != 2 || got[0] != "a" || got[1] != "b" { |
| 94 | + t.Fatalf("expected X-Test headers [a b], got %v", got) |
| 95 | + } |
| 96 | + if got := rec.Header().Get("Content-Type"); got != "application/json" { |
| 97 | + t.Fatalf("expected Content-Type application/json, got %q", got) |
| 98 | + } |
| 99 | +} |
0 commit comments