master
Kanezoh 2021-12-01 20:29:36 +09:00
parent 972fc61ad7
commit d54a93a64a
2 changed files with 23 additions and 0 deletions

15
spec/http_auth_spec.cr Normal file
View File

@ -0,0 +1,15 @@
require "./spec_helper"
WebMock.stub(:get, "http://auth.example.com/")
.with(headers: {"Authorization" => "Basic #{Base64.strict_encode("user:pass").chomp}"})
.to_return(status: 200)
WebMock.stub(:get, "http://auth.example.com/")
.to_return(status: 401, headers: {"WWW-Authenticate" => "Basic realm=\"Access to the staging site\", charset=\"UTF-8\""})
describe "Mechanize HTTP Authentication test" do
it "auth" do
agent = Mechanize.new
agent.get("http://auth.example.com/")
end
end

View File

@ -47,6 +47,10 @@ class Mechanize
return follow_redirect(response, headers, page)
end
if response && response.status.unauthorized?
response_authenticate(response)
end
page
end
@ -220,6 +224,10 @@ class Mechanize
target_url
end
private def response_authenticate(response)
www_authenticate = response.headers["www-authenticate"]
end
# reset request cookie before setting headers.
private def reset_request_header_cookies
request_headers.delete("Cookie")