12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package permissionpolicy_test
- import (
- "net/http/httptest"
- "strings"
- "testing"
- "imuslab.com/zoraxy/mod/dynamicproxy/permissionpolicy"
- )
- func TestInjectPermissionPolicyHeader(t *testing.T) {
- tests := []struct {
- name string
- existingHeader string
- policy *permissionpolicy.PermissionsPolicy
- expectedHeader string
- }{
- {
- name: "No existing policy, valid PermissionsPolicy",
- existingHeader: "",
- policy: &permissionpolicy.PermissionsPolicy{
- Geolocation: []string{"self"},
- Microphone: []string{"https://example.com"},
- Camera: []string{"*"},
- },
- expectedHeader: "camera=*, geolocation=(self), microphone=(https://example.com)",
- },
- {
- name: "Existing Permissions-Policy header should not be overwritten",
- existingHeader: "geolocation=(self), microphone=()",
- policy: &permissionpolicy.PermissionsPolicy{
- Geolocation: []string{"self"},
- Microphone: []string{"https://example.com"},
- Camera: []string{"*"},
- },
- expectedHeader: "geolocation=(self), microphone=()",
- },
- {
- name: "No policy provided",
- existingHeader: "",
- policy: nil,
- expectedHeader: "",
- },
- {
- name: "Empty PermissionsPolicy",
- existingHeader: "",
- policy: &permissionpolicy.PermissionsPolicy{},
- expectedHeader: "",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- rr := httptest.NewRecorder()
- if tt.existingHeader != "" {
- rr.Header().Set("Permissions-Policy", tt.existingHeader)
- }
- permissionpolicy.InjectPermissionPolicyHeader(rr, tt.policy)
- gotHeader := rr.Header().Get("Permissions-Policy")
- if !strings.Contains(gotHeader, tt.expectedHeader) {
- t.Errorf("got header %s, want %s", gotHeader, tt.expectedHeader)
- }
- })
- }
- }
|