add raw and test

master
Kanezoh 2022-01-02 14:02:19 +09:00
parent 603be3ff5b
commit e514ea66df
3 changed files with 198 additions and 12 deletions

View File

@ -7,7 +7,7 @@ describe "Mechanize HTTP Authentication test" do
parser.auth_param.should eq ["realm", "here"]
end
it "auth_param no value" do
it "auth_param bad no value" do
parser = Mechanize::HTTP::WWWAuthenticateParser.new
parser.scanner = StringScanner.new("realm=")
parser.auth_param.should eq nil
@ -32,16 +32,17 @@ describe "Mechanize HTTP Authentication test" do
end
it "test parse" do
expect = [Mechanize::HTTP::AuthChallenge.new("Basic", {"realm" => "foo", "qop" => "auth,auth-int"})]
expect = [challenge("Basic", {"realm" => "foo", "qop" => "auth,auth-int"}, "Basic realm=foo, qop=\"auth,auth-int\"")]
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
result[0].raw.should eq expect[0].raw
end
it "test_parse_without_comma_delimiter" do
expect = [challenge("Basic", {"realm" => "foo", "qop" => "auth,auth-int"})]
expect = [challenge("Basic", {"realm" => "foo", "qop" => "auth,auth-int"}, "Basic realm=foo qop=\"auth,auth-int\"")]
parser = Mechanize::HTTP::WWWAuthenticateParser.new
result = parser.parse("Basic realm=foo qop=\"auth,auth-int\"")
result[0].scheme.should eq expect[0].scheme
@ -50,19 +51,202 @@ describe "Mechanize HTTP Authentication test" do
it "test_parse_multiple" do
expect = [
challenge("Basic", {"realm" => "foo"}),
challenge("Digest", {"realm" => "bar"}),
challenge("Basic", {"realm" => "foo"}, "Basic realm=foo"),
challenge("Digest", {"realm" => "bar"}, "Digest realm=bar"),
]
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_without_comma_delimiter" do
expect = [
challenge("Basic", { "realm" => "foo" }, "Basic realm=foo"),
challenge("Digest", { "realm" => "bar" }, "Digest realm=bar"),
]
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 = [
challenge("Basic", { "realm" => "foo" }, "Basic realm=foo"),
challenge("Digest", { "realm" => "bar" }, "Digest realm=bar"),
]
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
it "test_parse_ntlm_type_2_3" do
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 = [
challenge("Basic", { "realm" => "foo" }, "Basic ReAlM=foo"),
]
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 = [
challenge("Basic", { "realm" => "Foo" }, "Basic realm=Foo"),
]
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 = [
challenge("Basic", { "realm" => "foo" }, "BaSiC realm=foo"),
]
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_whitespace_around_auth_param" do
expect = [
challenge("Basic", { "realm" => "foo" }, "Basic realm = \"foo\""),
]
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 = [
challenge("Basic", { "realm" => "'foo" }, "Basic realm='foo"),
]
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\""
string = parser.quoted_string
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"
end
end
private def challenge(scheme, params)
Mechanize::HTTP::AuthChallenge.new(scheme, params)
private def challenge(scheme, params, raw)
Mechanize::HTTP::AuthChallenge.new(scheme, params, raw)
end

View File

@ -6,10 +6,12 @@ class Mechanize
class AuthChallenge
property scheme : String?
property params : String? | Hash(String, String)?
property raw : String
def initialize(scheme = nil, params = nil)
def initialize(scheme = nil, params = nil, raw = "")
@scheme = scheme
@params = params
@raw = raw
end
def [](param)

View File

@ -42,11 +42,11 @@ class Mechanize
challenge.params = scanner.scan(/.*/)
end
# challenge.raw = www_authenticate[start, @scanner.pos]
challenge.raw = www_authenticate[start, scanner.offset]
challenges << challenge
next
else
scheme = scheme.capitalize
challenge.scheme = scheme.capitalize
end
next unless space
@ -64,12 +64,12 @@ class Mechanize
challenges << challenge
if scanner.eos?
# challenge.raw = www_authenticate[start, scanner.offset]
challenge.raw = www_authenticate[start, scanner.offset]
break
end
scanner.offset = offset # rewind
# challenge.raw = www_authenticate[start, scanner.offset].sub(/(,+)? *$/, "")
challenge.raw = www_authenticate[start, scanner.offset].sub(/(,+)? *$/, "")
challenge = nil # a token should be next, new challenge
break
end