|
| 1 | +package httputil_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/alecthomas/assert/v2" |
| 9 | + |
| 10 | + "github.com/block/cachew/internal/httputil" |
| 11 | +) |
| 12 | + |
| 13 | +func TestPlaypenGuardMiddleware(t *testing.T) { |
| 14 | + ok := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 15 | + w.WriteHeader(http.StatusOK) |
| 16 | + }) |
| 17 | + guard := httputil.PlaypenGuardMiddleware(ok) |
| 18 | + |
| 19 | + t.Run("allows normal requests", func(t *testing.T) { |
| 20 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 21 | + rr := httptest.NewRecorder() |
| 22 | + guard.ServeHTTP(rr, req) |
| 23 | + assert.Equal(t, http.StatusOK, rr.Code) |
| 24 | + }) |
| 25 | + |
| 26 | + t.Run("blocks requests with cachew playpen baggage", func(t *testing.T) { |
| 27 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 28 | + req.Header.Set("Baggage", "cachew-playpen=jrobotham") |
| 29 | + rr := httptest.NewRecorder() |
| 30 | + guard.ServeHTTP(rr, req) |
| 31 | + assert.Equal(t, http.StatusServiceUnavailable, rr.Code) |
| 32 | + }) |
| 33 | + |
| 34 | + t.Run("allows requests with unrelated baggage", func(t *testing.T) { |
| 35 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 36 | + req.Header.Set("Baggage", "blox-playpen=jrobotham") |
| 37 | + rr := httptest.NewRecorder() |
| 38 | + guard.ServeHTTP(rr, req) |
| 39 | + assert.Equal(t, http.StatusOK, rr.Code) |
| 40 | + }) |
| 41 | +} |
0 commit comments