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