Skip to content

Commit

Permalink
Merge pull request #137 from mamantoha/fix-request-initializer
Browse files Browse the repository at this point in the history
do not authenticate in Request initializer
  • Loading branch information
mamantoha authored Jun 2, 2020
2 parents 4ad2482 + 63b4e46 commit d81a3e9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
13 changes: 12 additions & 1 deletion samples/digest_auth.cr
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions spec/unit/curlify_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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="})

Expand Down
6 changes: 2 additions & 4 deletions src/crest/request.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit d81a3e9

Please sign in to comment.