mechanize.cr/spec/www_authenticate_parser_spec.cr

251 lines
7.6 KiB
Crystal
Raw Normal View History

2022-01-01 11:25:15 +01:00
require "./spec_helper"
describe "Mechanize HTTP Authentication test" do
it "auth_param" do
parser = Mechanize::HTTP::WWWAuthenticateParser.new
parser.scanner = StringScanner.new("realm=here")
parser.auth_param.should eq ["realm", "here"]
end
2022-01-02 06:02:19 +01:00
it "auth_param bad no value" do
2022-01-01 11:25:15 +01:00
parser = Mechanize::HTTP::WWWAuthenticateParser.new
parser.scanner = StringScanner.new("realm=")
parser.auth_param.should eq nil
end
it "auth_param bad token" do
parser = Mechanize::HTTP::WWWAuthenticateParser.new
parser.scanner = StringScanner.new("realm")
parser.auth_param.should eq nil
end
it "auth_param bad value" do
parser = Mechanize::HTTP::WWWAuthenticateParser.new
parser.scanner = StringScanner.new("realm=\"this ")
parser.auth_param.should eq nil
end
it "auth_param with quote" do
parser = Mechanize::HTTP::WWWAuthenticateParser.new
parser.scanner = StringScanner.new("realm=\"this site\"")
parser.auth_param.should eq ["realm", "this site"]
end
2022-01-01 13:20:07 +01:00
it "test parse" do
2022-01-02 06:02:19 +01:00
expect = [challenge("Basic", {"realm" => "foo", "qop" => "auth,auth-int"}, "Basic realm=foo, qop=\"auth,auth-int\"")]
2022-01-01 13:20:07 +01:00
parser = Mechanize::HTTP::WWWAuthenticateParser.new
result = parser.parse("Basic realm=foo, qop=\"auth,auth-int\"")
result[0].scheme.should eq expect[0].scheme
result[0].params.should eq expect[0].params
2022-01-02 06:02:19 +01:00
result[0].raw.should eq expect[0].raw
2022-01-01 13:20:07 +01:00
end
it "test_parse_without_comma_delimiter" do
2022-01-02 06:02:19 +01:00
expect = [challenge("Basic", {"realm" => "foo", "qop" => "auth,auth-int"}, "Basic realm=foo qop=\"auth,auth-int\"")]
2022-01-01 13:20:07 +01:00
parser = Mechanize::HTTP::WWWAuthenticateParser.new
result = parser.parse("Basic realm=foo qop=\"auth,auth-int\"")
result[0].scheme.should eq expect[0].scheme
result[0].params.should eq expect[0].params
end
it "test_parse_multiple" do
expect = [
2022-01-02 06:02:19 +01:00
challenge("Basic", {"realm" => "foo"}, "Basic realm=foo"),
challenge("Digest", {"realm" => "bar"}, "Digest realm=bar"),
2022-01-01 13:20:07 +01:00
]
parser = Mechanize::HTTP::WWWAuthenticateParser.new
result = parser.parse("Basic realm=foo, Digest realm=bar")
result[0].scheme.should eq expect[0].scheme
result[0].params.should eq expect[0].params
2022-01-02 06:02:19 +01:00
result[0].raw.should eq expect[0].raw
2022-01-01 13:20:07 +01:00
result[1].scheme.should eq expect[1].scheme
result[1].params.should eq expect[1].params
2022-01-02 06:02:19 +01:00
result[1].raw.should eq expect[1].raw
end
it "test_parse_multiple_without_comma_delimiter" do
expect = [
2022-01-02 14:14:58 +01:00
challenge("Basic", {"realm" => "foo"}, "Basic realm=foo"),
challenge("Digest", {"realm" => "bar"}, "Digest realm=bar"),
2022-01-02 06:02:19 +01:00
]
parser = Mechanize::HTTP::WWWAuthenticateParser.new
result = parser.parse("Basic realm=foo, Digest realm=bar")
result[0].scheme.should eq expect[0].scheme
result[0].params.should eq expect[0].params
result[0].raw.should eq expect[0].raw
result[1].scheme.should eq expect[1].scheme
result[1].params.should eq expect[1].params
result[1].raw.should eq expect[1].raw
end
it "test_parse_multiple_blank" do
expect = [
2022-01-02 14:14:58 +01:00
challenge("Basic", {"realm" => "foo"}, "Basic realm=foo"),
challenge("Digest", {"realm" => "bar"}, "Digest realm=bar"),
2022-01-02 06:02:19 +01:00
]
parser = Mechanize::HTTP::WWWAuthenticateParser.new
result = parser.parse("Basic realm=foo, Digest realm=bar")
result[0].scheme.should eq expect[0].scheme
result[0].params.should eq expect[0].params
result[0].raw.should eq expect[0].raw
result[1].scheme.should eq expect[1].scheme
result[1].params.should eq expect[1].params
result[1].raw.should eq expect[1].raw
end
it "test_parse_ntlm_init" do
expect = [
challenge("NTLM", nil, "NTLM"),
]
parser = Mechanize::HTTP::WWWAuthenticateParser.new
result = parser.parse("NTLM")
result[0].scheme.should eq expect[0].scheme
result[0].params.should eq expect[0].params
result[0].raw.should eq expect[0].raw
end
2022-01-02 14:14:58 +01:00
it "test_parse_ntlm_type_2_3" do
2022-01-02 06:02:19 +01:00
expect = [
challenge("NTLM", "foo=", "NTLM foo="),
]
parser = Mechanize::HTTP::WWWAuthenticateParser.new
result = parser.parse("NTLM foo=")
result[0].scheme.should eq expect[0].scheme
result[0].params.should eq expect[0].params
result[0].raw.should eq expect[0].raw
end
it "test_parse_realm_uppercase" do
expect = [
2022-01-02 14:14:58 +01:00
challenge("Basic", {"realm" => "foo"}, "Basic ReAlM=foo"),
2022-01-02 06:02:19 +01:00
]
parser = Mechanize::HTTP::WWWAuthenticateParser.new
result = parser.parse("Basic ReAlM=foo")
result[0].scheme.should eq expect[0].scheme
result[0].params.should eq expect[0].params
result[0].raw.should eq expect[0].raw
end
it "test_parse_realm_value_case" do
expect = [
2022-01-02 14:14:58 +01:00
challenge("Basic", {"realm" => "Foo"}, "Basic realm=Foo"),
2022-01-02 06:02:19 +01:00
]
parser = Mechanize::HTTP::WWWAuthenticateParser.new
result = parser.parse("Basic realm=Foo")
result[0].scheme.should eq expect[0].scheme
result[0].params.should eq expect[0].params
result[0].raw.should eq expect[0].raw
end
it "test_parse_scheme_uppercase" do
expect = [
2022-01-02 14:14:58 +01:00
challenge("Basic", {"realm" => "foo"}, "BaSiC realm=foo"),
2022-01-02 06:02:19 +01:00
]
parser = Mechanize::HTTP::WWWAuthenticateParser.new
result = parser.parse("BaSiC realm=foo")
result[0].scheme.should eq expect[0].scheme
result[0].params.should eq expect[0].params
result[0].raw.should eq expect[0].raw
end
2022-01-02 14:14:58 +01:00
it "test_parse_bad_whitespace_around_auth_param" do
2022-01-02 06:02:19 +01:00
expect = [
2022-01-02 14:14:58 +01:00
challenge("Basic", {"realm" => "foo"}, "Basic realm = \"foo\""),
2022-01-02 06:02:19 +01:00
]
parser = Mechanize::HTTP::WWWAuthenticateParser.new
result = parser.parse("Basic realm = \"foo\"")
result[0].scheme.should eq expect[0].scheme
result[0].params.should eq expect[0].params
result[0].raw.should eq expect[0].raw
end
it "test_parse_bad_single_quote" do
expect = [
2022-01-02 14:14:58 +01:00
challenge("Basic", {"realm" => "'foo"}, "Basic realm='foo"),
2022-01-02 06:02:19 +01:00
]
parser = Mechanize::HTTP::WWWAuthenticateParser.new
result = parser.parse("Basic realm='foo bar', qop='baz'")
result[0].scheme.should eq expect[0].scheme
result[0].params.should eq expect[0].params
result[0].raw.should eq expect[0].raw
end
it "test_quoted_string" do
parser = Mechanize::HTTP::WWWAuthenticateParser.new
parser.scanner = StringScanner.new "\"text\""
string = parser.quoted_string
string.should eq "text"
end
it "test_quoted_string_bad" do
parser = Mechanize::HTTP::WWWAuthenticateParser.new
parser.scanner = StringScanner.new "\"text"
string = parser.quoted_string
string.should eq nil
end
it "test_quoted_string_quote" do
parser = Mechanize::HTTP::WWWAuthenticateParser.new
parser.scanner = StringScanner.new "\"escaped \\\" here\""
2022-01-02 14:14:58 +01:00
string = parser.quoted_string
2022-01-02 06:02:19 +01:00
string.should eq "escaped \\\" here"
end
it "test_quoted_string_quote_end" do
parser = Mechanize::HTTP::WWWAuthenticateParser.new
parser.scanner = StringScanner.new "\"end \\\"\""
string = parser.quoted_string
string.should eq "end \\\""
end
it "test_token" do
parser = Mechanize::HTTP::WWWAuthenticateParser.new
parser.scanner = StringScanner.new "text"
string = parser.token
string.should eq "text"
end
it "test_token_space" do
parser = Mechanize::HTTP::WWWAuthenticateParser.new
parser.scanner = StringScanner.new "t ext"
string = parser.token
string.should eq "t"
end
it "test_token_special" do
parser = Mechanize::HTTP::WWWAuthenticateParser.new
parser.scanner = StringScanner.new "t\text"
string = parser.token
string.should eq "t"
2022-01-01 13:20:07 +01:00
end
end
2022-01-02 06:02:19 +01:00
private def challenge(scheme, params, raw)
Mechanize::HTTP::AuthChallenge.new(scheme, params, raw)
2022-01-01 11:25:15 +01:00
end