Skip to content

Commit

Permalink
Improve all stubbing tests and fix cbor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mullermp committed Oct 15, 2024
1 parent dd8408a commit c754e3f
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 62 deletions.
4 changes: 2 additions & 2 deletions gems/aws-sdk-core/lib/aws-sdk-core/stubbing.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Aws
# setup autoloading for Stubbing module
# @api private
module Stubbing
autoload :EmptyStub, 'aws-sdk-core/stubbing/empty_stub'
autoload :DataApplicator, 'aws-sdk-core/stubbing/data_applicator'
Expand All @@ -19,4 +19,4 @@ module Protocols
autoload :ApiGateway, 'aws-sdk-core/stubbing/protocols/api_gateway'
end
end
end
end
23 changes: 12 additions & 11 deletions gems/aws-sdk-core/lib/aws-sdk-core/stubbing/protocols/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Aws
module Stubbing
module Protocols
# @api private
class EC2

def stub_data(api, operation, data)
Expand All @@ -16,17 +17,17 @@ def stub_data(api, operation, data)
end

def stub_error(error_code)
http_resp = Seahorse::Client::Http::Response.new
http_resp.status_code = 400
http_resp.body = <<-XML.strip
<ErrorResponse>
<Error>
<Code>#{error_code}</Code>
<Message>stubbed-response-error-message</Message>
</Error>
</ErrorResponse>
resp = Seahorse::Client::Http::Response.new
resp.status_code = 400
resp.body = <<~XML.strip
<ErrorResponse>
<Error>
<Code>#{error_code}</Code>
<Message>stubbed-response-error-message</Message>
</Error>
</ErrorResponse>
XML
http_resp
resp
end

private
Expand All @@ -37,7 +38,7 @@ def build_body(api, operation, data)
xml.shift
xml.pop
xmlns = "http://ec2.amazonaws.com/doc/#{api.version}/".inspect
xml.unshift(" <requestId>stubbed-request-id</requestId>")
xml.unshift(' <requestId>stubbed-request-id</requestId>')
xml.unshift("<#{operation.name}Response xmlns=#{xmlns}>\n")
xml.push("</#{operation.name}Response>\n")
xml.join
Expand Down
21 changes: 11 additions & 10 deletions gems/aws-sdk-core/lib/aws-sdk-core/stubbing/protocols/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@
module Aws
module Stubbing
module Protocols
# @api private
class Json

def stub_data(api, operation, data)
resp = Seahorse::Client::Http::Response.new
resp.status_code = 200
resp.headers["Content-Type"] = content_type(api)
resp.headers["x-amzn-RequestId"] = "stubbed-request-id"
resp.headers['Content-Type'] = content_type(api)
resp.headers['x-amzn-RequestId'] = 'stubbed-request-id'
resp.body = build_body(operation, data)
resp
end

def stub_error(error_code)
http_resp = Seahorse::Client::Http::Response.new
http_resp.status_code = 400
http_resp.body = <<-JSON.strip
{
"code": #{error_code.inspect},
"message": "stubbed-response-error-message"
}
resp = Seahorse::Client::Http::Response.new
resp.status_code = 400
resp.body = <<~JSON.strip
{
"code": #{error_code.inspect},
"message": "stubbed-response-error-message"
}
JSON
http_resp
resp
end

private
Expand Down
13 changes: 7 additions & 6 deletions gems/aws-sdk-core/lib/aws-sdk-core/stubbing/protocols/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Aws
module Stubbing
module Protocols
# @api private
class Query

def stub_data(api, operation, data)
Expand All @@ -13,20 +14,20 @@ def stub_data(api, operation, data)
end

def stub_error(error_code)
http_resp = Seahorse::Client::Http::Response.new
http_resp.status_code = 400
http_resp.body = XmlError.new(error_code).to_xml
http_resp
resp = Seahorse::Client::Http::Response.new
resp.status_code = 400
resp.body = XmlError.new(error_code).to_xml
resp
end

private

def build_body(api, operation, data)
xml = []
builder = Aws::Xml::DocBuilder.new(target: xml, indent: ' ')
builder.node(operation.name + 'Response', xmlns: xmlns(api)) do
builder.node("#{operation.name}Response", xmlns: xmlns(api)) do
if (rules = operation.output)
rules.location_name = operation.name + 'Result'
rules.location_name = "#{operation.name}Result"
Xml::Builder.new(rules, target: xml, pad:' ').to_xml(data)
end
builder.node('ResponseMetadata') do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module Aws
module Stubbing
module Protocols
# @api private
class Rest

include Seahorse::Model::Shapes
Expand All @@ -22,7 +23,7 @@ def stub_data(api, operation, data)
def new_http_response
resp = Seahorse::Client::Http::Response.new
resp.status_code = 200
resp.headers["x-amzn-RequestId"] = "stubbed-request-id"
resp.headers['x-amzn-RequestId'] = 'stubbed-request-id'
resp
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Aws
module Stubbing
module Protocols
# @api private
class RestJson < Rest

def body_for(_a, _b, rules, data)
Expand All @@ -14,15 +15,15 @@ def body_for(_a, _b, rules, data)
end

def stub_error(error_code)
http_resp = Seahorse::Client::Http::Response.new
http_resp.status_code = 400
http_resp.body = <<-JSON.strip
{
"code": #{error_code.inspect},
"message": "stubbed-response-error-message"
}
resp = Seahorse::Client::Http::Response.new
resp.status_code = 400
resp.body = <<~JSON.strip
{
"code": #{error_code.inspect},
"message": "stubbed-response-error-message"
}
JSON
http_resp
resp
end

end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@
module Aws
module Stubbing
module Protocols
# @api private
class RestXml < Rest

def body_for(api, operation, rules, data)
if eventstream?(rules)
encode_eventstream_response(rules, data, Xml::Builder)
else
xml = []
rules.location_name = operation.name + 'Result'
rules.location_name = "#{operation.name}Result"
rules['xmlNamespace'] = { 'uri' => api.metadata['xmlNamespace'] }
Xml::Builder.new(rules, target:xml).to_xml(data)
xml.join
end
end

def stub_error(error_code)
http_resp = Seahorse::Client::Http::Response.new
http_resp.status_code = 400
http_resp.body = XmlError.new(error_code).to_xml
http_resp
resp = Seahorse::Client::Http::Response.new
resp.status_code = 400
resp.body = XmlError.new(error_code).to_xml
resp
end

def xmlns(api)
Expand Down
15 changes: 8 additions & 7 deletions gems/aws-sdk-core/lib/aws-sdk-core/stubbing/protocols/rpc_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Aws
module Stubbing
module Protocols
# @api private
class RpcV2

def stub_data(_api, operation, data)
Expand All @@ -16,15 +17,15 @@ def stub_data(_api, operation, data)
end

def stub_error(error_code)
http_resp = Seahorse::Client::Http::Response.new
http_resp.status_code = 400
http_resp.body = <<~JSON.strip
resp = Seahorse::Client::Http::Response.new
resp.status_code = 400
resp.body = Cbor.encode(
{
"code": #{error_code.inspect},
"message": "stubbed-response-error-message"
'code' => error_code,
'message' => 'stubbed-response-error-message'
}
JSON
http_resp
)
resp
end

private
Expand Down
13 changes: 13 additions & 0 deletions gems/aws-sdk-core/spec/aws/stubbing/protocols/ec2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ def normalize(xml)
XML
end

it 'can stub errors' do
resp = EC2.new.stub_error('error-code')
expect(resp.status_code).to eq(400)
expect(normalize(resp.body.string)).to eq(normalize(<<-XML))
<ErrorResponse>
<Error>
<Code>error-code</Code>
<Message>stubbed-response-error-message</Message>
</Error>
</ErrorResponse>
XML
end

end
end
end
Expand Down
35 changes: 23 additions & 12 deletions gems/aws-sdk-core/spec/aws/stubbing/protocols/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Protocols
describe '#stub_data' do

def normalize(json)
JSON.pretty_generate(JSON.load(json), indent: ' ')
JSON.pretty_generate(JSON.parse(json), indent: ' ')
end

let(:api) { ApiHelper.sample_json::Client.api }
Expand All @@ -25,37 +25,37 @@ def normalize(json)

it 'populates the expected headers' do
resp = Json.new.stub_data(api, operation, {})
expect(resp.headers.to_h).to eq({
"content-type" => "application/x-amz-json-1.0",
"x-amzn-requestid" => "stubbed-request-id",
})
expect(resp.headers.to_h).to eq(
'content-type' => 'application/x-amz-json-1.0',
'x-amzn-requestid' => 'stubbed-request-id'
)
end

it 'populates the body with the stub data' do
now = Time.now
data = {
table: {
table_name: "my-table-name",
table_name: 'my-table-name',
table_size_bytes: 0,
table_status: "ACTIVE",
table_status: 'ACTIVE',
attribute_definitions: [
{
attribute_name: "Id",
attribute_type: "S"
attribute_name: 'Id',
attribute_type: 'S'
}
],
creation_date_time: now,
item_count: 0,
key_schema: [
attribute_name: "Id",
key_type: "HASH"
attribute_name: 'Id',
key_type: 'HASH'
],
provisioned_throughput: {
last_increase_date_time: now,
last_decrease_date_time: now,
number_of_decreases_today: 0,
read_capacity_units: 50,
write_capacity_units: 50,
write_capacity_units: 50
}
}
}
Expand Down Expand Up @@ -92,6 +92,17 @@ def normalize(json)
JSON
end

it 'can stub errors' do
resp = Json.new.stub_error('error-code')
expect(resp.status_code).to eq(400)
expect(normalize(resp.body.string)).to eq(normalize(<<-JSON))
{
"code": "error-code",
"message": "stubbed-response-error-message"
}
JSON
end

end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def normalize(xml)

it 'can stub errors' do
resp = Query.new.stub_error('error-code')
expect(resp.status_code).to eq(400)
expect(normalize(resp.body.string)).to eq(normalize(<<-XML))
<ErrorResponse>
<Error>
Expand Down
11 changes: 11 additions & 0 deletions gems/aws-sdk-core/spec/aws/stubbing/protocols/rest_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ def normalize(json)
JSON
end

it 'can stub errors' do
resp = RestJson.new.stub_error('error-code')
expect(resp.status_code).to eq(400)
expect(normalize(resp.body.string)).to eq(normalize(<<-JSON))
{
"code": "error-code",
"message": "stubbed-response-error-message"
}
JSON
end

end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def normalize(xml)

it 'can stub errors' do
resp = RestXml.new.stub_error('error-code')
expect(resp.status_code).to eq(400)
expect(normalize(resp.body.string)).to eq(normalize(<<-XML))
<ErrorResponse>
<Error>
Expand Down
Loading

0 comments on commit c754e3f

Please sign in to comment.