Skip to content

Commit

Permalink
Move "Not authorized" patches to Patches
Browse files Browse the repository at this point in the history
  • Loading branch information
omus committed Sep 29, 2021
1 parent 18eb605 commit 77a33e1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 38 deletions.
38 changes: 1 addition & 37 deletions test/AWS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,45 +202,9 @@ end
)

message = "User is not authorized to perform: action on resource with an explicit deny"
body = "{\"__type\":\"AccessDeniedException\",\"Message\":\"$message\"}"
headers = [
"Content-Type" => "application/x-amz-json-1.1",
"Content-Length" => string(sizeof(body)),
]

# Simulate the HTTP.request behaviour with a HTTP 400 response
patches = [
@patch function HTTP.request(
args...; status_exception=true, response_stream=nothing, kwargs...
)
request = HTTP.Request("GET", "/")

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
end

response = HTTP.Response(400, headers; body=body, request=request)
exception = HTTP.StatusError(400, response)
return !status_exception ? response : throw(exception)
end
@patch function Downloads.request(args...; output=nothing, kwargs...)
if output !== nothing
write(output, body)
end

return Downloads.Response(
"https",
"https://region.amazonaws.com/",
400,
"HTTP/1.1 400 Bad Request",
headers,
)
end
]

exception = apply(patches) do
exception = apply(Patches.gen_http_options_400_patches(message)) do
try
AWS.submit_request(aws, request)
return nothing
Expand Down
41 changes: 41 additions & 0 deletions test/patch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Patches

using AWS
using Dates
using Downloads: Downloads
using HTTP
using JSON
using GitHub
Expand Down Expand Up @@ -142,4 +143,44 @@ get_profile_settings_patch = @patch function aws_get_profile_settings(profile, i
return Dict("foo" => "bar")
end

# Simulate the HTTP.request behaviour with a HTTP 400 response
function gen_http_options_400_patches(message)
body = "{\"__type\":\"AccessDeniedException\",\"Message\":\"$message\"}"
headers = [
"Content-Type" => "application/x-amz-json-1.1",
"Content-Length" => string(sizeof(body)),
]

return [
@patch function HTTP.request(
args...; status_exception=true, response_stream=nothing, kwargs...
)
request = HTTP.Request("GET", "/")

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
end

response = HTTP.Response(400, headers; body=body, request=request)
exception = HTTP.StatusError(400, response)
return !status_exception ? response : throw(exception)
end
@patch function Downloads.request(args...; output=nothing, kwargs...)
if output !== nothing
write(output, body)
end

return Downloads.Response(
"https",
"https://region.amazonaws.com/",
400,
"HTTP/1.1 400 Bad Request",
headers,
)
end
]
end

end
1 change: 0 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ using AWS.AWSMetadata:
using Base64
using Compat: mergewith
using Dates
using Downloads: Downloads
using GitHub
using HTTP
using IniFile: Inifile
Expand Down

0 comments on commit 77a33e1

Please sign in to comment.