|
| 1 | +# frozen_string_literal: true |
| 2 | +require "spec_helper" |
| 3 | + |
| 4 | +module SecureHeaders |
| 5 | + describe ReportingEndpoints do |
| 6 | + describe "make_header" do |
| 7 | + it "returns nil with nil config" do |
| 8 | + expect(described_class.make_header).to be_nil |
| 9 | + end |
| 10 | + |
| 11 | + it "returns nil with opt-out config" do |
| 12 | + expect(described_class.make_header(OPT_OUT)).to be_nil |
| 13 | + end |
| 14 | + |
| 15 | + it "returns an empty string with empty config" do |
| 16 | + name, value = described_class.make_header({}) |
| 17 | + expect(name).to eq(ReportingEndpoints::HEADER_NAME) |
| 18 | + expect(value).to eq("") |
| 19 | + end |
| 20 | + |
| 21 | + it "builds a valid header with correct configuration" do |
| 22 | + name, value = described_class.make_header({endpoint: "https://report-endpoint-example.io/"}) |
| 23 | + expect(name).to eq(ReportingEndpoints::HEADER_NAME) |
| 24 | + expect(value).to eq("endpoint=\"https://report-endpoint-example.io/\"") |
| 25 | + end |
| 26 | + |
| 27 | + it "supports multiple endpoints" do |
| 28 | + name, value = described_class.make_header({ |
| 29 | + endpoint: "https://report-endpoint-example.io/", |
| 30 | + 'csp-endpoint': "https://csp-report-endpoint-example.io/" |
| 31 | + }) |
| 32 | + expect(name).to eq(ReportingEndpoints::HEADER_NAME) |
| 33 | + expect(value).to eq("endpoint=\"https://report-endpoint-example.io/\",csp-endpoint=\"https://csp-report-endpoint-example.io/\"") |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + describe "validate_config!" do |
| 38 | + it "raises an exception when configuration is not a hash" do |
| 39 | + expect do |
| 40 | + described_class.validate_config!(["invalid-configuration"]) |
| 41 | + end.to raise_error(ReportingEndpointsConfigError) |
| 42 | + end |
| 43 | + |
| 44 | + it "raises an exception when all hash elements are not a string" do |
| 45 | + expect do |
| 46 | + described_class.validate_config!({endpoint: 1234}) |
| 47 | + end.to raise_error(ReportingEndpointsConfigError) |
| 48 | + end |
| 49 | + end |
| 50 | + end |
| 51 | +end |
0 commit comments