diff --git a/spec/http_auth_spec.cr b/spec/http_auth_spec.cr new file mode 100644 index 0000000..68210de --- /dev/null +++ b/spec/http_auth_spec.cr @@ -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 diff --git a/src/mechanize/http/agent.cr b/src/mechanize/http/agent.cr index 74201e8..18e5b5f 100644 --- a/src/mechanize/http/agent.cr +++ b/src/mechanize/http/agent.cr @@ -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")