Skip to content

Commit

Permalink
Update HTTPBackend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
omus committed Sep 3, 2021
1 parent 0b6ad1e commit d556cf6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/utilities/request.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function _http_request(http_backend::HTTPBackend, request::Request, response_str
http_options...,
)

return Response(r, response_stream)
return @mock Response(r, response_stream)
catch e
# Base.IOError is needed because HTTP.jl can often have errors that aren't
# caught and wrapped in an HTTP.IOError
Expand Down
22 changes: 12 additions & 10 deletions test/AWS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ struct TestBackend <: AWS.AbstractBackend
param::Int
end

function AWS._http_request(backend::TestBackend, request::AWS.Request, response_stream::IO)
function AWS._http_request(backend::TestBackend, ::AWS.Request, ::IO)
return backend.param
end

Expand All @@ -290,28 +290,30 @@ end
url="https://s3.us-east-1.amazonaws.com/sample-bucket",
backend=AWS.HTTPBackend(),
)
apply(Patches._http_options_patch) do
io = IOBuffer()

apply(Patches._http_options_patches) do
# No default options
@test isempty(AWS._http_request(request.backend, request))
@test isempty(AWS._http_request(request.backend, request, io))

# We can pass HTTP options via the backend
custom_backend = AWS.HTTPBackend(Dict(:connection_limit => 5))
@test custom_backend isa AWS.AbstractBackend
@test AWS._http_request(custom_backend, request) == Dict(:connection_limit => 5)
@test AWS._http_request(custom_backend, request, io) == Dict(:connection_limit => 5)

# We can pass options per-request
request.http_options = Dict(:pipeline_limit => 20)
@test AWS._http_request(request.backend, request) == Dict(:pipeline_limit => 20)
@test AWS._http_request(custom_backend, request) ==
@test AWS._http_request(request.backend, request, io) == Dict(:pipeline_limit => 20)
@test AWS._http_request(custom_backend, request, io) ==
Dict(:pipeline_limit => 20, :connection_limit => 5)

# per-request options override backend options:
custom_backend = AWS.HTTPBackend(Dict(:pipeline_limit => 5))
@test AWS._http_request(custom_backend, request) == Dict(:pipeline_limit => 20)
@test AWS._http_request(custom_backend, request, io) == Dict(:pipeline_limit => 20)
end

request.backend = TestBackend(2)
@test AWS._http_request(request.backend, request) == 2
@test AWS._http_request(request.backend, request, io) == 2

request = Request(;
service="s3",
Expand All @@ -320,7 +322,7 @@ end
url="https://s3.us-east-1.amazonaws.com/sample-bucket",
backend=TestBackend(4),
)
@test AWS._http_request(request.backend, request) == 4
@test AWS._http_request(request.backend, request, io) == 4

# Let's test setting the default backend
prev_backend = AWS.DEFAULT_BACKEND[]
Expand All @@ -332,7 +334,7 @@ end
request_method="GET",
url="https://s3.us-east-1.amazonaws.com/sample-bucket",
)
@test AWS._http_request(request.backend, request) == 3
@test AWS._http_request(request.backend, request, io) == 3
finally
AWS.DEFAULT_BACKEND[] = prev_backend
end
Expand Down
17 changes: 9 additions & 8 deletions test/patch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,12 @@ end
# except `require_ssl_verification` and `response_stream`. This is used to
# test which other options are being passed to `HTTP.Request` inside of
# `_http_request`.
_http_options_patch = @patch function HTTP.request(args...; kwargs...)
options = Dict(kwargs)
delete!(options, :require_ssl_verification)
delete!(options, :response_stream)
return options
end

end
_http_options_patches = [
@patch function HTTP.request(args...; kwargs...)
options = Dict(kwargs)
delete!(options, :require_ssl_verification)
delete!(options, :response_stream)
return options
end
@patch AWS.Response(options, args...) = options
]

0 comments on commit d556cf6

Please sign in to comment.