File: referrer_policy_spec.rb

package info (click to toggle)
ruby-secure-headers 7.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 508 kB
  • sloc: ruby: 3,353; makefile: 5
file content (91 lines) | stat: -rw-r--r-- 2,717 bytes parent folder | download | duplicates (4)
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
# frozen_string_literal: true
require "spec_helper"

module SecureHeaders
  describe ReferrerPolicy do
    specify { expect(ReferrerPolicy.make_header).to eq([ReferrerPolicy::HEADER_NAME, "origin-when-cross-origin"]) }
    specify { expect(ReferrerPolicy.make_header("no-referrer")).to eq([ReferrerPolicy::HEADER_NAME, "no-referrer"]) }
    specify { expect(ReferrerPolicy.make_header(%w(origin-when-cross-origin strict-origin-when-cross-origin))).to eq([ReferrerPolicy::HEADER_NAME, "origin-when-cross-origin, strict-origin-when-cross-origin"]) }

    context "valid configuration values" do
      it "accepts 'no-referrer'" do
        expect do
          ReferrerPolicy.validate_config!("no-referrer")
        end.not_to raise_error
      end

      it "accepts 'no-referrer-when-downgrade'" do
        expect do
          ReferrerPolicy.validate_config!("no-referrer-when-downgrade")
        end.not_to raise_error
      end

      it "accepts 'same-origin'" do
        expect do
          ReferrerPolicy.validate_config!("same-origin")
        end.not_to raise_error
      end

      it "accepts 'strict-origin'" do
        expect do
          ReferrerPolicy.validate_config!("strict-origin")
        end.not_to raise_error
      end

      it "accepts 'strict-origin-when-cross-origin'" do
        expect do
          ReferrerPolicy.validate_config!("strict-origin-when-cross-origin")
        end.not_to raise_error
      end

      it "accepts 'origin'" do
        expect do
          ReferrerPolicy.validate_config!("origin")
        end.not_to raise_error
      end

      it "accepts 'origin-when-cross-origin'" do
        expect do
          ReferrerPolicy.validate_config!("origin-when-cross-origin")
        end.not_to raise_error
      end

      it "accepts 'unsafe-url'" do
        expect do
          ReferrerPolicy.validate_config!("unsafe-url")
        end.not_to raise_error
      end

      it "accepts nil" do
        expect do
          ReferrerPolicy.validate_config!(nil)
        end.not_to raise_error
      end

      it "accepts array of policy values" do
        expect do
          ReferrerPolicy.validate_config!(
            %w(
              origin-when-cross-origin
              strict-origin-when-cross-origin
            )
          )
        end.not_to raise_error
      end
    end

    context "invalid configuration values" do
      it "doesn't accept invalid values" do
        expect do
          ReferrerPolicy.validate_config!("open")
        end.to raise_error(ReferrerPolicyConfigError)
      end

      it "doesn't accept invalid types" do
        expect do
          ReferrerPolicy.validate_config!({})
        end.to raise_error(TypeError)
      end
    end
  end
end