Module: HttpHeadersValidations

Defined in:
lib/http_headers_validations.rb

Class Method Summary collapse

Class Method Details



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/http_headers_validations.rb', line 48

def self.assert_cookie_value(parsed_cookie, cookie_rules)
    if cookie_rules[parsed_cookie.name]
        valid_cookie = parsed_cookie.validate!(cookie_rules[parsed_cookie.name])
        if valid_cookie.nil?
            failed = false
            text = "Cookie '#{parsed_cookie.name}' passed verification!"
        else
            failed = true
            text = "Invalid cookie config '#{HttpHeadersUtils.bold(parsed_cookie.name)}':\n #{valid_cookie.join("\n")}"
        end
    else
        failed = true
        text = "Missing config for cookie '#{HttpHeadersUtils.bold(parsed_cookie.name)}'."
    end
    status = failed ? "[FAILED]" : "[PASSED]"

    report(text, failed, status)
    return [text, failed]
end

.assert_expected_header(expected_header, expected_value, actual_value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/http_headers_validations.rb', line 11

def self.assert_expected_header(expected_header, expected_value, actual_value)
    if (!actual_value.nil? && expected_value.is_a?(Regexp) && actual_value.match?(expected_value)) ||
        (expected_value.to_s == actual_value.to_s)
        failed = false
        text = "Expected Header '#{expected_header}' matched!"
    else
        failed = true
        text = "Expected Header '#{HttpHeadersUtils.bold(expected_header)}' failed! \nExpected Value:\n#{expected_value} \nActual Value:\n#{actual_value}."
    end
    status = failed ? "[FAILED]" : "[PASSED]"

    report(text, failed, status)

    return text if failed 
end

.assert_extra_header(actual_header, actual_value, ignored_headers, avoid_headers) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/http_headers_validations.rb', line 27

def self.assert_extra_header(actual_header, actual_value, ignored_headers, avoid_headers)

    if avoid_headers.include? actual_header.downcase
        status = "[FAILED]"
        failed = true
        text = "Extra Header '#{actual_header}' is not allowed!"
    elsif ignored_headers.include? actual_header.downcase
        status = "[PASSED]"
        failed = false
        text = "Extra Header '#{actual_header}' marked for ignore!"
    else
        status = "[WARNING]"
        failed = false
        text = "Warning: Extra Header '#{HttpHeadersUtils.bold(actual_header)}' with value '#{actual_value}' was unexpected."
    end

    report(text, failed, status)
    
    return text if failed 
end

.report(text, failed, status) ⇒ Object



5
6
7
8
9
# File 'lib/http_headers_validations.rb', line 5

def self.report(text, failed, status)
    if failed || HttpHeadersUtils.verbose
        puts "#{status} #{text}"
    end
end