permissionpolicy_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package permissionpolicy_test
  2. import (
  3. "net/http/httptest"
  4. "strings"
  5. "testing"
  6. "imuslab.com/zoraxy/mod/dynamicproxy/permissionpolicy"
  7. )
  8. func TestInjectPermissionPolicyHeader(t *testing.T) {
  9. //Prepare the data for permission policy
  10. testPermissionPolicy := permissionpolicy.GetDefaultPermissionPolicy()
  11. testPermissionPolicy.Geolocation = []string{"self"}
  12. testPermissionPolicy.Microphone = []string{"self", "https://example.com"}
  13. testPermissionPolicy.Camera = []string{"*"}
  14. tests := []struct {
  15. name string
  16. existingHeader string
  17. policy *permissionpolicy.PermissionsPolicy
  18. expectedHeader string
  19. }{
  20. {
  21. name: "Default policy with a few limitations",
  22. existingHeader: "",
  23. policy: testPermissionPolicy,
  24. expectedHeader: `accelerometer=*, ambient-light-sensor=*, autoplay=*, battery=*, camera=*, cross-origin-isolated=*, display-capture=*, document-domain=*, encrypted-media=*, execution-while-not-rendered=*, execution-while-out-of-viewport=*, fullscreen=*, geolocation=(self), gyroscope=*, keyboard-map=*, magnetometer=*, microphone=(self "https://example.com"), midi=*, navigation-override=*, payment=*, picture-in-picture=*, publickey-credentials-get=*, screen-wake-lock=*, sync-xhr=*, usb=*, web-share=*, xr-spatial-tracking=*, clipboard-read=*, clipboard-write=*, gamepad=*, speaker-selection=*, conversion-measurement=*, focus-without-user-activation=*, hid=*, idle-detection=*, interest-cohort=*, serial=*, sync-script=*, trust-token-redemption=*, unload=*, window-placement=*, vertical-scroll=*`,
  25. },
  26. }
  27. for _, tt := range tests {
  28. t.Run(tt.name, func(t *testing.T) {
  29. rr := httptest.NewRecorder()
  30. if tt.existingHeader != "" {
  31. rr.Header().Set("Permissions-Policy", tt.existingHeader)
  32. }
  33. permissionpolicy.InjectPermissionPolicyHeader(rr, tt.policy)
  34. gotHeader := rr.Header().Get("Permissions-Policy")
  35. if !strings.Contains(gotHeader, tt.expectedHeader) {
  36. t.Errorf("got header %s, want %s", gotHeader, tt.expectedHeader)
  37. }
  38. })
  39. }
  40. }