From 63b4e4697eece328537c53bd5ed2b2c9c5973f9b Mon Sep 17 00:00:00 2001 From: Anton Maminov Date: Tue, 2 Jun 2020 12:24:46 +0300 Subject: [PATCH] do not authenticate in Request initializer --- samples/digest_auth.cr | 13 ++++++++++++- spec/unit/curlify_spec.cr | 7 +++++++ src/crest/request.cr | 6 ++---- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/samples/digest_auth.cr b/samples/digest_auth.cr index 58d33c1f..be490315 100644 --- a/samples/digest_auth.cr +++ b/samples/digest_auth.cr @@ -1,6 +1,17 @@ require "../src/crest" -response = Crest.get("https://httpbin.org/digest-auth/auth/admin/passwd/MD5", auth: "digest", user: "admin", password: "passwd") +request = Crest::Request.new( + :get, + "https://httpbin.org/digest-auth/auth/admin/passwd/MD5", + auth: "digest", + user: "admin", + password: "passwd", + logging: false +) + +puts request.to_curl + +response = request.execute puts response.body puts response.status_code diff --git a/spec/unit/curlify_spec.cr b/spec/unit/curlify_spec.cr index 17959ff4..80c51b64 100644 --- a/spec/unit/curlify_spec.cr +++ b/spec/unit/curlify_spec.cr @@ -33,6 +33,13 @@ describe Crest::Curlify do (request.to_curl).should eq(result) end + it "converts a request to invalid domain with digest auth" do + request = Crest::Request.new(:get, "https://domain.invalid", auth: "digest", user: "user", password: "passwd") + + result = "curl -X GET https://domain.invalid --digest --user user:passwd" + (request.to_curl).should eq(result) + end + it "converts a request with basic auth in headers" do request = Crest::Request.new(:get, "http://httpbin.org/basic-auth/user/passwd", headers: {"Authorization" => "Basic dXNlcjpwYXNzd2Q="}) diff --git a/src/crest/request.cr b/src/crest/request.cr index ab5e78f1..fc9df3c2 100644 --- a/src/crest/request.cr +++ b/src/crest/request.cr @@ -131,8 +131,6 @@ module Crest set_proxy!(@p_addr, @p_port, @p_user, @p_pass) yield self - - authenticate! end # When block is not given. @@ -152,7 +150,6 @@ module Crest # ``` def self.{{method.id}}(url : String, **args, &block : Crest::Response ->) : Nil request = Request.new(:{{method.id}}, url, **args) - request.authenticate! response = request.execute(&block) end @@ -164,7 +161,6 @@ module Crest # ``` def self.{{method.id}}(url : String, **args) : Crest::Response request = Request.new(:{{method.id}}, url, **args) - request.authenticate! request.execute end @@ -173,6 +169,7 @@ module Crest # Execute HTTP request def execute : Crest::Response @http_client.set_proxy(@proxy) + authenticate! @logger.request(self) if @logging @http_request = new_http_request(@method, @url, @headers, @form_data) @@ -185,6 +182,7 @@ module Crest # Execute streaming HTTP request def execute(&block : Crest::Response ->) : Nil @http_client.set_proxy(@proxy) + authenticate! @logger.request(self) if @logging @http_request = new_http_request(@method, @url, @headers, @form_data)