Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix protocol tests with new changes to CBOR and Rest #3123

Merged
merged 6 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"uri": "/service/RpcV2Protocol/operation/EmptyInputOutput",
"body": "v/8=",
"headers": {
"Accept": "application/cbor",
mullermp marked this conversation as resolved.
Show resolved Hide resolved
"Content-Type": "application/cbor",
"smithy-protocol": "rpc-v2-cbor"
},
Expand Down Expand Up @@ -76,6 +77,7 @@
"uri": "/service/RpcV2Protocol/operation/NoInputOutput",
"body": "",
"headers": {
"Accept": "application/cbor",
"smithy-protocol": "rpc-v2-cbor"
},
"forbidHeaders": [
Expand Down Expand Up @@ -129,6 +131,7 @@
"uri": "/service/RpcV2Protocol/operation/OptionalInputOutput",
"body": "v/8=",
"headers": {
"Accept": "application/cbor",
"Content-Type": "application/cbor",
"smithy-protocol": "rpc-v2-cbor"
},
Expand Down Expand Up @@ -217,6 +220,7 @@
"uri": "/service/RpcV2Protocol/operation/RecursiveShapes",
"body": "v2ZuZXN0ZWS/Y2Zvb2RGb28xZm5lc3RlZL9jYmFyZEJhcjFvcmVjdXJzaXZlTWVtYmVyv2Nmb29kRm9vMmZuZXN0ZWS/Y2JhcmRCYXIy//////8=",
"headers": {
"Accept": "application/cbor",
"Content-Type": "application/cbor",
"smithy-protocol": "rpc-v2-cbor"
},
Expand Down Expand Up @@ -359,6 +363,7 @@
"uri": "/service/RpcV2Protocol/operation/RpcV2CborDenseMaps",
"body": "oW5kZW5zZVN0cnVjdE1hcKJjZm9voWJoaWV0aGVyZWNiYXqhYmhpY2J5ZQ==",
"headers": {
"Accept": "application/cbor",
"Content-Type": "application/cbor",
"smithy-protocol": "rpc-v2-cbor"
},
Expand Down Expand Up @@ -394,6 +399,7 @@
"uri": "/service/RpcV2Protocol/operation/RpcV2CborDenseMaps",
"body": "om5kZW5zZU51bWJlck1hcKFheABvZGVuc2VCb29sZWFuTWFwoWF49A==",
"headers": {
"Accept": "application/cbor",
"Content-Type": "application/cbor",
"smithy-protocol": "rpc-v2-cbor"
},
Expand Down Expand Up @@ -430,6 +436,7 @@
"uri": "/service/RpcV2Protocol/operation/RpcV2CborDenseMaps",
"body": "oWtkZW5zZVNldE1hcKJheIBheYJhYWFi",
"headers": {
"Accept": "application/cbor",
"Content-Type": "application/cbor",
"smithy-protocol": "rpc-v2-cbor"
},
Expand Down Expand Up @@ -665,6 +672,7 @@
"uri": "/service/RpcV2Protocol/operation/RpcV2CborLists",
"body": "v2pzdHJpbmdMaXN0gmNmb29jYmFyaXN0cmluZ1NldIJjZm9vY2JhcmtpbnRlZ2VyTGlzdIIBAmtib29sZWFuTGlzdIL19G10aW1lc3RhbXBMaXN0gsH7QdTX+/OAAADB+0HU1/vzgAAAaGVudW1MaXN0gmNGb29hMGtpbnRFbnVtTGlzdIIBAnBuZXN0ZWRTdHJpbmdMaXN0goJjZm9vY2JhcoJjYmF6Y3F1eG1zdHJ1Y3R1cmVMaXN0gqJhYWExYWJhMqJhYWEzYWJhNGhibG9iTGlzdIJDZm9vQ2Jhcv8=",
"headers": {
"Accept": "application/cbor",
"Content-Type": "application/cbor",
"smithy-protocol": "rpc-v2-cbor"
},
Expand Down Expand Up @@ -696,6 +704,7 @@
"uri": "/service/RpcV2Protocol/operation/RpcV2CborLists",
"body": "v2pzdHJpbmdMaXN0n///",
"headers": {
"Accept": "application/cbor",
"Content-Type": "application/cbor",
"smithy-protocol": "rpc-v2-cbor"
},
Expand Down Expand Up @@ -727,6 +736,7 @@
"uri": "/service/RpcV2Protocol/operation/RpcV2CborLists",
"body": "oWpzdHJpbmdMaXN0gA==",
"headers": {
"Accept": "application/cbor",
"Content-Type": "application/cbor",
"smithy-protocol": "rpc-v2-cbor"
},
Expand Down Expand Up @@ -841,6 +851,7 @@
"uri": "/service/RpcV2Protocol/operation/SimpleScalarProperties",
"body": "v2lieXRlVmFsdWUFa2RvdWJsZVZhbHVl+z/+OVgQYk3TcWZhbHNlQm9vbGVhblZhbHVl9GpmbG9hdFZhbHVl+kD0AABsaW50ZWdlclZhbHVlGQEAaWxvbmdWYWx1ZRkmkWpzaG9ydFZhbHVlGSaqa3N0cmluZ1ZhbHVlZnNpbXBsZXB0cnVlQm9vbGVhblZhbHVl9WlibG9iVmFsdWVDZm9v/w==",
"headers": {
"Accept": "application/cbor",
"Content-Type": "application/cbor",
"smithy-protocol": "rpc-v2-cbor"
},
Expand Down Expand Up @@ -870,6 +881,7 @@
"uri": "/service/RpcV2Protocol/operation/SimpleScalarProperties",
"body": "v/8=",
"headers": {
"Accept": "application/cbor",
"Content-Type": "application/cbor",
"smithy-protocol": "rpc-v2-cbor"
},
Expand Down Expand Up @@ -900,6 +912,7 @@
"uri": "/service/RpcV2Protocol/operation/SimpleScalarProperties",
"body": "v2tkb3VibGVWYWx1Zft/+AAAAAAAAGpmbG9hdFZhbHVl+n/AAAD/",
"headers": {
"Accept": "application/cbor",
"Content-Type": "application/cbor",
"smithy-protocol": "rpc-v2-cbor"
},
Expand Down Expand Up @@ -930,6 +943,7 @@
"uri": "/service/RpcV2Protocol/operation/SimpleScalarProperties",
"body": "v2tkb3VibGVWYWx1Zft/8AAAAAAAAGpmbG9hdFZhbHVl+n+AAAD/",
"headers": {
"Accept": "application/cbor",
"Content-Type": "application/cbor",
"smithy-protocol": "rpc-v2-cbor"
},
Expand Down Expand Up @@ -960,6 +974,7 @@
"uri": "/service/RpcV2Protocol/operation/SimpleScalarProperties",
"body": "v2tkb3VibGVWYWx1Zfv/8AAAAAAAAGpmbG9hdFZhbHVl+v+AAAD/",
"headers": {
"Accept": "application/cbor",
"Content-Type": "application/cbor",
"smithy-protocol": "rpc-v2-cbor"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def set_engine(context, protocol, engine)
when /xml/, /ec2/, /query/
Aws::Xml::Parser
when /smithy-rpc-v2-cbor/
Aws::Cbor
Aws::RpcV2
else
raise "unsupported protocol: #{protocol}"
end
Expand Down Expand Up @@ -301,8 +301,8 @@ def match_req_body(suite, test_case, http_req, it)
expected_body = Aws::Json.load(expected_body)
end
when 'smithy-rpc-v2-cbor'
request_body = Aws::Cbor.decode(request_body)
expected_body = Aws::Cbor.decode(Base64.decode64(expected_body))
request_body = Aws::RpcV2.decode(request_body)
expected_body = Aws::RpcV2.decode(Base64.decode64(expected_body))
else raise "unsupported protocol: `#{protocol}`"
end
assert(it, request_body, expected_body)
Expand Down
2 changes: 2 additions & 0 deletions gems/aws-sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased Changes
------------------

* Issue - Fix RPCv2 protocol to always send an Accept header for CBOR.

3.209.1 (2024-09-25)
------------------

Expand Down
59 changes: 3 additions & 56 deletions gems/aws-sdk-core/lib/aws-sdk-core/cbor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

require_relative 'cbor/encoder'
require_relative 'cbor/decoder'

module Aws
# @api private
module Cbor
Expand Down Expand Up @@ -46,61 +49,5 @@ def initialize(add_info)
super("Unexpected additional information: #{add_info}")
end
end

class << self
# @param [Symbol,Class] engine
# Must be one of the following values:
#
# * :cbor
#
def engine=(engine)
@engine = Class === engine ? engine : load_engine(engine)
end

# @return [Class] Returns the default engine.
# One of:
#
# * {CborEngine}
#
def engine
set_default_engine unless @engine
@engine
end

def encode(data)
@engine.encode(data)
end

def decode(bytes)
bytes.force_encoding(Encoding::BINARY)
@engine.decode(bytes)
end

def set_default_engine
[:cbor].each do |name|
@engine ||= try_load_engine(name)
end

unless @engine
raise 'Unable to find a compatible cbor library.'
end
end

private

def load_engine(name)
require "aws-sdk-core/cbor/#{name}_engine"
const_name = name[0].upcase + name[1..-1] + 'Engine'
const_get(const_name)
end

def try_load_engine(name)
load_engine(name)
rescue LoadError
false
end
end

set_default_engine
end
end
67 changes: 65 additions & 2 deletions gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,69 @@
# frozen_string_literal: true

require_relative 'cbor'
require_relative 'rpc_v2/handler'
require_relative 'rpc_v2/builder'
require_relative 'rpc_v2/content_type_handler'
require_relative 'rpc_v2/error_handler'
require_relative 'rpc_v2/builder'
require_relative 'rpc_v2/handler'
require_relative 'rpc_v2/parser'

module Aws
# @api private
module RpcV2
class << self
# @param [Symbol,Class] engine
# Must be one of the following values:
#
# * :cbor
#
def engine=(engine)
@engine = Class === engine ? engine : load_engine(engine)
end

# @return [Class] Returns the default engine.
# One of:
#
# * {CborEngine}
#
def engine
set_default_engine unless @engine
@engine
end

def encode(data)
@engine.encode(data)
end

def decode(bytes)
bytes.force_encoding(Encoding::BINARY)
@engine.decode(bytes)
end

def set_default_engine
[:cbor].each do |name|
@engine ||= try_load_engine(name)
end

unless @engine
raise 'Unable to find a compatible cbor library.'
end
end

private

def load_engine(name)
require "aws-sdk-core/rpc_v2/#{name}_engine"
const_name = name[0].upcase + name[1..-1] + 'Engine'
const_get(const_name)
end

def try_load_engine(name)
load_engine(name)
rescue LoadError
false
end
end

set_default_engine
end
end
2 changes: 1 addition & 1 deletion gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def serialize(params)
# different than if the input shape is a structure with no members.
return nil if @rules.shape.struct_class == EmptyStructure

Cbor.encode(format(@rules, params))
RpcV2.encode(format(@rules, params))
end

private
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# frozen_string_literal: true

require_relative 'encoder'
require_relative 'decoder'
require_relative '../cbor'

module Aws
module Cbor
module RpcV2
# Pure Ruby implementation of CBOR encode and decode
module CborEngine
def self.encode(data)
Encoder.new.add(data).bytes
Cbor::Encoder.new.add(data).bytes
end

def self.decode(bytes)
Decoder.new(bytes.force_encoding(Encoding::BINARY)).decode
Cbor::Decoder.new(bytes.force_encoding(Encoding::BINARY)).decode
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ def call(context)
accept =
if eventstream_output?(context)
'application/vnd.amazon.eventstream'
else
'application/cbor'
end

headers = context.http_request.headers
headers['Content-Type'] ||= content_type if content_type
headers['Accept'] ||= accept if accept
headers['Accept'] ||= accept
@handler.call(context)
end

Expand Down
2 changes: 1 addition & 1 deletion gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def valid_response?(context)
end

def extract_error(body, context)
data = Cbor.decode(body)
data = RpcV2.decode(body)
code = error_code(data, context)
message = data['message']
data = parse_error_data(context, body, code)
Expand Down
2 changes: 1 addition & 1 deletion gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(rules, query_compatible: false)
def parse(cbor, target = nil)
return {} if cbor.empty?

parse_ref(@rules, Cbor.decode(cbor), target)
parse_ref(@rules, RpcV2.decode(cbor), target)
end

private
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
# frozen_string_literal: true

require_relative '../spec_helper'
require 'aws-sdk-core/cbor'

module Aws
describe Cbor do
describe RpcV2 do
[:cbor].each do |engine|
describe("ENGINE: #{engine}") do

begin
Cbor.engine = engine
RpcV2.engine = engine
rescue LoadError
next
end

describe '.encode' do
it 'encodes an object into bytes' do
expect(Cbor.encode('abc')).to eq("\x63abc")
expect(RpcV2.encode('abc')).to eq("\x63abc")
end
end

describe '.decode' do
it 'decodes bytes into an object' do
# frozen string
bytes = StringIO.new("\x63abc").read
expect(Cbor.decode(bytes)).to eq('abc')
expect(RpcV2.decode(bytes)).to eq('abc')
end
end
end
Expand Down
Loading