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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
require 'mechanize/test_case'
class TestMechanizeHttpWwwAuthenticateParser < Mechanize::TestCase
def setup
super
@parser = Mechanize::HTTP::WWWAuthenticateParser.new
end
def test_auth_param
@parser.scanner = StringScanner.new 'realm=here'
param = @parser.auth_param
assert_equal %w[realm here], param
end
def test_auth_param_bad_no_value
@parser.scanner = StringScanner.new 'realm='
assert_nil @parser.auth_param
end
def test_auth_param_bad_token
@parser.scanner = StringScanner.new 'realm'
assert_nil @parser.auth_param
end
def test_auth_param_bad_value
@parser.scanner = StringScanner.new 'realm="this '
assert_nil @parser.auth_param
end
def test_auth_param_quoted
@parser.scanner = StringScanner.new 'realm="this site"'
param = @parser.auth_param
assert_equal ['realm', 'this site'], param
end
def test_parse
expected = [
challenge('Basic', { 'realm' => 'foo', 'qop' => 'auth,auth-int' }, 'Basic realm=foo, qop="auth,auth-int"'),
]
assert_equal expected, @parser.parse('Basic realm=foo, qop="auth,auth-int"')
end
def test_parse_without_comma_delimiter
expected = [
challenge('Basic', { 'realm' => 'foo', 'qop' => 'auth,auth-int' }, 'Basic realm=foo qop="auth,auth-int"'),
]
assert_equal expected, @parser.parse('Basic realm=foo qop="auth,auth-int"')
end
def test_parse_multiple
expected = [
challenge('Basic', { 'realm' => 'foo' }, 'Basic realm=foo'),
challenge('Digest', { 'realm' => 'bar' }, 'Digest realm=bar'),
]
assert_equal expected, @parser.parse('Basic realm=foo, Digest realm=bar')
end
def test_parse_multiple_without_comma_delimiter
expected = [
challenge('Basic', { 'realm' => 'foo' }, 'Basic realm=foo'),
challenge('Digest', { 'realm' => 'bar' }, 'Digest realm=bar'),
]
assert_equal expected, @parser.parse('Basic realm=foo Digest realm=bar')
end
def test_parse_multiple_blank
expected = [
challenge('Basic', { 'realm' => 'foo' }, 'Basic realm=foo'),
challenge('Digest', { 'realm' => 'bar' }, 'Digest realm=bar'),
]
assert_equal expected, @parser.parse('Basic realm=foo,, Digest realm=bar')
end
def test_parse_ntlm_init
expected = [
challenge('NTLM', nil, 'NTLM'),
]
assert_equal expected, @parser.parse('NTLM')
end
def test_parse_ntlm_type_2_3
expected = [
challenge('NTLM', 'foo=', 'NTLM foo='),
]
assert_equal expected, @parser.parse('NTLM foo=')
end
def test_parse_realm_uppercase
expected = [
challenge('Basic', { 'realm' => 'foo' }, 'Basic ReAlM=foo'),
]
assert_equal expected, @parser.parse('Basic ReAlM=foo')
end
def test_parse_realm_value_case
expected = [
challenge('Basic', { 'realm' => 'Foo' }, 'Basic realm=Foo'),
]
assert_equal expected, @parser.parse('Basic realm=Foo')
end
def test_parse_scheme_uppercase
expected = [
challenge('Basic', { 'realm' => 'foo' }, 'BaSiC realm=foo'),
]
assert_equal expected, @parser.parse('BaSiC realm=foo')
end
def test_parse_bad_whitespace_around_auth_param
expected = [
challenge('Basic', { 'realm' => 'foo' }, 'Basic realm = "foo"'),
]
assert_equal expected, @parser.parse('Basic realm = "foo"')
end
def test_parse_bad_single_quote
expected = [
challenge('Basic', { 'realm' => "'foo" }, "Basic realm='foo"),
]
assert_equal expected, @parser.parse("Basic realm='foo bar', qop='baz'")
end
def test_quoted_string
@parser.scanner = StringScanner.new '"text"'
string = @parser.quoted_string
assert_equal 'text', string
end
def test_quoted_string_bad
@parser.scanner = StringScanner.new '"text'
assert_nil @parser.quoted_string
end
def test_quoted_string_quote
@parser.scanner = StringScanner.new '"escaped \\" here"'
string = @parser.quoted_string
assert_equal 'escaped \\" here', string
end
def test_quoted_string_quote_end
@parser.scanner = StringScanner.new '"end \""'
string = @parser.quoted_string
assert_equal 'end \"', string
end
def test_token
@parser.scanner = StringScanner.new 'text'
string = @parser.token
assert_equal 'text', string
end
def test_token_space
@parser.scanner = StringScanner.new 't ext'
string = @parser.token
assert_equal 't', string
end
def test_token_special
@parser.scanner = StringScanner.new "t\text"
string = @parser.token
assert_equal 't', string
end
def challenge scheme, params, raw
Mechanize::HTTP::AuthChallenge.new scheme, params, raw
end
end
|