Skip to content

Commit

Permalink
Try #560:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] committed Jun 21, 2022
2 parents 517a05e + 8d2d6fb commit 672fb81
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ XMLDict = "228000da-037f-5747-90a9-8195ccbf91a5"
[compat]
Compat = "3.29"
GitHub = "5"
HTTP = "0.9.17"
HTTP = "0.9.17, 1.0"
IniFile = "0.5"
JSON = "0.18, 0.19, 0.20, 0.21"
MbedTLS = "0.6, 0.7, 1"
Expand Down
3 changes: 1 addition & 2 deletions src/AWSExceptions.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module AWSExceptions

using HTTP
using HTTP.MessageRequest: body_was_streamed
using JSON
using XMLDict
using XMLDict: XMLDictElement
Expand Down Expand Up @@ -114,7 +113,7 @@ function AWSException(e::HTTP.StatusError, body::AbstractString)
message = get(info, "Message", message)
message = get(info, "message", message)

streamed_body = e.response.body == body_was_streamed ? body : nothing
streamed_body = e.response.body isa IO ? body : nothing

return AWSException(code, message, info, e, streamed_body)
end
Expand Down
2 changes: 1 addition & 1 deletion src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function legacy_response(

# When a user defined I/O stream is passed in use the actual `HTTP.Response` body
# instead of the `AWS.Response` body which requires the I/O stream to be seekable.
body = request.response_stream !== nothing ? response.response.body : response.body
body = response.body

# The stored service name is always lowercase and may not match the module name
# specified by the user. We'll assume that the typical casing used is titlecase.
Expand Down
6 changes: 1 addition & 5 deletions src/utilities/downloads_backend.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using HTTP.MessageRequest: body_was_streamed

"""
DownloadsBackend <: AWS.AbstractBackend
Expand Down Expand Up @@ -125,9 +123,7 @@ function _http_request(backend::DownloadsBackend, request::Request, response_str
end

function _http_response(req::Request, res::Downloads.Response; throw::Bool=true)
response = HTTP.Response(
res.status, res.headers; body=body_was_streamed, request=nothing
)
response = HTTP.Response(res.status, res.headers; body=UInt8[], request=nothing)

if throw && HTTP.iserror(response)
target = HTTP.resource(HTTP.URI(req.url))
Expand Down
12 changes: 4 additions & 8 deletions src/utilities/request.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,6 @@ end
function _http_request(http_backend::HTTPBackend, request::Request, response_stream::IO)
http_options = merge(http_backend.http_options, request.http_options)

# HTTP options such as `status_exception` need to be used when creating the stack
http_stack = HTTP.stack(;
redirect=false, retry=false, aws_authorization=false, http_options...
)

local buffer
local response

Expand All @@ -219,13 +214,14 @@ function _http_request(http_backend::HTTPBackend, request::Request, response_str
buffer = Base.BufferStream()

response = @mock HTTP.request(
http_stack,
request.request_method,
HTTP.URI(request.url),
HTTP.mkheaders(request.headers),
request.content;
require_ssl_verification=false,
redirect=false,
retry=false,
response_stream=buffer,
aws_authorization=false,
http_options...,
)

Expand All @@ -240,7 +236,7 @@ function _http_request(http_backend::HTTPBackend, request::Request, response_str
# https://github.com/JuliaWeb/HTTP.jl/issues/382
return isa(e, Sockets.DNSError) ||
isa(e, HTTP.ParseError) ||
isa(e, HTTP.IOError) ||
(isdefined(HTTP, :IOError) && isa(e, HTTP.IOError)) ||
isa(e, Base.IOError) ||
(isa(e, HTTP.StatusError) && _http_status(e) >= 500)
end
Expand Down
1 change: 1 addition & 0 deletions test/AWS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ end
use_response_type=true,
)
apply(Patches._aws_http_request_patch(Patches._response(; status=301))) do
AWS.submit_request(aws, request)
@test_throws AWSException AWS.submit_request(aws, request)
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/AWSExceptions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"status_code" => 400,
)

expected["body"] = HTTP.MessageRequest.body_was_streamed
expected["body"] = UInt8[]
expected["streamed_body"] = """
<?xml version="1.0" encoding="UTF-8"?>
<$err>
Expand All @@ -50,7 +50,7 @@

@testset "XMLRequest - Invalid XML" begin
expected = Dict(
"body" => HTTP.MessageRequest.body_was_streamed,
"body" => UInt8[],
"streamed_body" => """<?xml version="1.0" encoding="UTF-8"?>InvalidXML""",
"headers" => ["Content-Type" => "application/xml"],
"status_code" => 404,
Expand All @@ -74,7 +74,7 @@
"status_code" => 400,
)

expected["body"] = HTTP.MessageRequest.body_was_streamed
expected["body"] = UInt8[]
expected["streamed_body"] = """
{
"__type": "$(expected["code"])",
Expand Down
15 changes: 10 additions & 5 deletions test/issues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ try
@testset "lambda" begin
@service Lambda

@test_throws NoCredentials Lambda.list_functions(;
aws_config=AWSConfig(; creds=nothing)
)
@test_throws NoCredentials try
Lambda.list_functions(; aws_config=AWSConfig(; creds=nothing))
catch e
if isdefined(AWS.HTTP, :RequestError) && e isa HTTP.RequestError
rethrow(e.error)
end
rethrow(e)
end
end
end

Expand Down Expand Up @@ -171,7 +176,7 @@ try
attempt_num += 1
if attempt_num <= num_attempts_to_fail
write(response_stream, data[1:(n - 1)]) # an incomplete stream that shouldn't be retained
throw(HTTP.IOError(EOFError(), "msg"))
throw(EOFError())
else
write(response_stream, data)
return HTTP.Response(200, "{\"Location\": \"us-east-1\"}")
Expand Down Expand Up @@ -212,7 +217,7 @@ try

@testset "Fail all 4 attempts then throw" begin
err_t = if AWS.DEFAULT_BACKEND[] isa AWS.HTTPBackend
HTTP.IOError
isdefined(HTTP, :IOError) ? HTTP.IOError : HTTP.RequestError
else
Downloads.RequestError
end
Expand Down
6 changes: 4 additions & 2 deletions test/patch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ end
_http_options_patches = [
@patch function HTTP.request(args...; kwargs...)
options = Dict(kwargs)
delete!(options, :require_ssl_verification)
delete!(options, :retry)
delete!(options, :redirect)
delete!(options, :aws_authorization)
delete!(options, :response_stream)
return options
end
Expand Down Expand Up @@ -171,7 +173,7 @@ function gen_http_options_400_patches(message)
if response_stream !== nothing
write(response_stream, body)
close(response_stream) # Simulating current HTTP.jl 0.9.14 behaviour
body = HTTP.MessageRequest.body_was_streamed
body = UInt8[]
end

response = HTTP.Response(400, headers; body=body, request=request)
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ using UUIDs
using XMLDict
using StableRNGs

# backwards compat
HTTP.StatusError(status, resp) = HTTP.StatusError(status, resp.request.method, resp.request.target, resp)

Mocking.activate()

include("patch.jl")
Expand Down

0 comments on commit 672fb81

Please sign in to comment.