|
| 1 | +// Zaparoo Core |
| 2 | +// Copyright (c) 2025 The Zaparoo Project Contributors. |
| 3 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | +// |
| 5 | +// This file is part of Zaparoo Core. |
| 6 | +// |
| 7 | +// Zaparoo Core is free software: you can redistribute it and/or modify |
| 8 | +// it under the terms of the GNU General Public License as published by |
| 9 | +// the Free Software Foundation, either version 3 of the License, or |
| 10 | +// (at your option) any later version. |
| 11 | +// |
| 12 | +// Zaparoo Core is distributed in the hope that it will be useful, |
| 13 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +// GNU General Public License for more details. |
| 16 | +// |
| 17 | +// You should have received a copy of the GNU General Public License |
| 18 | +// along with Zaparoo Core. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +package api |
| 21 | + |
| 22 | +import ( |
| 23 | + "net/http" |
| 24 | + "net/http/httptest" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/stretchr/testify/assert" |
| 28 | +) |
| 29 | + |
| 30 | +// TestPrivateNetworkAccessMiddleware verifies that the middleware correctly |
| 31 | +// handles Private Network Access preflight requests as specified in: |
| 32 | +// https://wicg.github.io/private-network-access/ |
| 33 | +func TestPrivateNetworkAccessMiddleware(t *testing.T) { |
| 34 | + t.Parallel() |
| 35 | + |
| 36 | + handler := privateNetworkAccessMiddleware(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 37 | + w.WriteHeader(http.StatusOK) |
| 38 | + })) |
| 39 | + |
| 40 | + tests := []struct { |
| 41 | + name string |
| 42 | + method string |
| 43 | + requestPNAHeader string |
| 44 | + expectPNAResponseHeader bool |
| 45 | + }{ |
| 46 | + { |
| 47 | + name: "OPTIONS_with_PNA_request_header", |
| 48 | + method: http.MethodOptions, |
| 49 | + requestPNAHeader: "true", |
| 50 | + expectPNAResponseHeader: true, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "OPTIONS_without_PNA_request_header", |
| 54 | + method: http.MethodOptions, |
| 55 | + requestPNAHeader: "", |
| 56 | + expectPNAResponseHeader: false, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "GET_with_PNA_request_header", |
| 60 | + method: http.MethodGet, |
| 61 | + requestPNAHeader: "true", |
| 62 | + expectPNAResponseHeader: false, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "POST_with_PNA_request_header", |
| 66 | + method: http.MethodPost, |
| 67 | + requestPNAHeader: "true", |
| 68 | + expectPNAResponseHeader: false, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "OPTIONS_with_PNA_false", |
| 72 | + method: http.MethodOptions, |
| 73 | + requestPNAHeader: "false", |
| 74 | + expectPNAResponseHeader: false, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + for _, tt := range tests { |
| 79 | + t.Run(tt.name, func(t *testing.T) { |
| 80 | + t.Parallel() |
| 81 | + |
| 82 | + req := httptest.NewRequest(tt.method, "/api", http.NoBody) |
| 83 | + if tt.requestPNAHeader != "" { |
| 84 | + req.Header.Set("Access-Control-Request-Private-Network", tt.requestPNAHeader) |
| 85 | + } |
| 86 | + |
| 87 | + rec := httptest.NewRecorder() |
| 88 | + handler.ServeHTTP(rec, req) |
| 89 | + |
| 90 | + pnaResponse := rec.Header().Get("Access-Control-Allow-Private-Network") |
| 91 | + if tt.expectPNAResponseHeader { |
| 92 | + assert.Equal(t, "true", pnaResponse, |
| 93 | + "expected Access-Control-Allow-Private-Network: true header") |
| 94 | + } else { |
| 95 | + assert.Empty(t, pnaResponse, |
| 96 | + "expected no Access-Control-Allow-Private-Network header") |
| 97 | + } |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
0 commit comments