Skip to content

Commit 027515b

Browse files
authored
Improve testing of client features (#290)
1 parent 6667c65 commit 027515b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+546
-451
lines changed

gems/smithy-client/lib/smithy-client/plugins/protocol.rb

+18-20
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,25 @@ class Protocol < Plugin
1212
docstring: 'The protocol to use for request serialization and response deserialization.'
1313
)
1414

15-
def add_handlers(handlers, config)
16-
return unless config.protocol
15+
# @api private
16+
class BuildHandler < Handler
17+
def call(context)
18+
context.config.protocol.build_request(context)
19+
@handler.call(context)
20+
end
21+
end
1722

23+
# @api private
24+
class ParseHandler < Handler
25+
def call(context)
26+
output = @handler.call(context)
27+
output.error = context.config.protocol.parse_error(context) unless output.error
28+
output.data = context.config.protocol.parse_data(context) unless output.error
29+
output
30+
end
31+
end
32+
33+
def add_handlers(handlers, _config)
1834
handlers.add(BuildHandler)
1935
handlers.add(ParseHandler, step: :parse)
2036
end
@@ -45,24 +61,6 @@ def resolve_default_protocol(client_class, options)
4561
end
4662
end
4763
end
48-
49-
# @api private
50-
class BuildHandler < Handler
51-
def call(context)
52-
context.config.protocol.build_request(context)
53-
@handler.call(context)
54-
end
55-
end
56-
57-
# @api private
58-
class ParseHandler < Handler
59-
def call(context)
60-
output = @handler.call(context)
61-
output.error = context.config.protocol.parse_error(context) unless output.error
62-
output.data = context.config.protocol.parse_data(context) unless output.error
63-
output
64-
end
65-
end
6664
end
6765
end
6866
end

gems/smithy-client/lib/smithy-client/rpc_v2_cbor/request_builder.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def event_stream?(shape)
5555
end
5656

5757
def event_stream_shape?(shape)
58-
shape.traits.include?('smithy.api#streaming') && shape.is_a?(Shapes::Union)
58+
shape.traits.include?('smithy.api#streaming') && shape.is_a?(Schema::Shapes::UnionShape)
5959
end
6060

6161
def build_url(context)
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module Smithy
22
module Client
33
class AuthOption
4-
def initialize: (scheme_id: String, ?identity_properties: Hash[Symbol, untyped], ?signer_properties: Hash[Symbol, untyped]) -> void
4+
def initialize: (scheme_id: String, ?identity_properties: Hash[String, untyped], ?signer_properties: Hash[String, untyped]) -> void
55
attr_reader scheme_id: String
6-
attr_reader identity_properties: Hash[Symbol, untyped]
7-
attr_reader signer_properties: Hash[Symbol, untyped]
6+
attr_reader identity_properties: Hash[String, untyped]
7+
attr_reader signer_properties: Hash[String, untyped]
88
end
99
end
1010
end

gems/smithy-client/spec/smithy-client/auth_option_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ module Smithy
66
module Client
77
describe AuthOption do
88
let(:scheme_id) { 'scheme_id' }
9-
let(:identity_properties) { { identity: 'property' } }
10-
let(:signer_properties) { { signer: 'property' } }
9+
let(:identity_properties) { { 'identity' => 'property' } }
10+
let(:signer_properties) { { 'signer' => 'property' } }
1111

1212
subject do
1313
AuthOption.new(

gems/smithy-client/spec/smithy-client/param_converter_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Client
1010
describe ParamConverter do
1111
describe '#convert' do
1212
it 'performs a deeply nested conversion of values' do
13-
client_class = ClientHelper.sample_service
13+
client_class = ClientHelper.sample_client
1414
rules = client_class.const_get(:Schema).const_get(:SERVICE).operation(:operation).input
1515
structure = client_class.const_get(:Types).const_get(:Structure)
1616
union = client_class.const_get(:Types).const_get(:Union).const_get(:Structure)

gems/smithy-client/spec/smithy-client/param_validator_spec.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ module Smithy
66
module Client
77
describe ParamValidator do
88
let(:shapes) { SchemaHelper.sample_shapes }
9-
let(:sample_service) { ClientHelper.sample_service(shapes: shapes) }
10-
let(:service) { sample_service.const_get(:Schema).const_get(:SERVICE) }
9+
let(:sample_client) { ClientHelper.sample_client(shapes: shapes) }
10+
let(:service_shape) { sample_client.const_get(:Schema).const_get(:SERVICE) }
1111

1212
def validate(params, expected_errors = [])
13-
schema = service.operation(:operation).input
13+
schema = service_shape.operation(:operation).input
1414
if expected_errors.empty?
1515
ParamValidator.new(schema).validate!(params)
1616
else
@@ -271,7 +271,7 @@ def match_errors(error, expected_errors)
271271
end
272272

273273
it 'accepts a modeled type' do
274-
structure = sample_service.const_get(:Types).const_get(:Structure).new({})
274+
structure = sample_client.const_get(:Types).const_get(:Structure).new({})
275275
validate({ structure: structure })
276276
end
277277
end
@@ -312,12 +312,12 @@ def match_errors(error, expected_errors)
312312
end
313313

314314
it 'accepts a modeled type' do
315-
union_structure = sample_service.const_get(:Types).const_get(:Union).const_get(:Structure)
315+
union_structure = sample_client.const_get(:Types).const_get(:Union).const_get(:Structure)
316316
validate({ union: union_structure.new({ string: 'string' }) })
317317
end
318318

319319
it 'raises an error when given the wrong modeled type' do
320-
union_structure = sample_service.const_get(:Types).const_get(:Union).const_get(:Structure)
320+
union_structure = sample_client.const_get(:Types).const_get(:Union).const_get(:Structure)
321321
validate({ union: union_structure.new({ structure: 'abc' }) },
322322
'expected params[:union][:structure] to be a Hash, got class String instead.')
323323
end

gems/smithy-client/spec/smithy-client/plugins/http_api_key_auth_spec.rb

+39-7
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@ module Smithy
88
module Client
99
module Plugins
1010
describe HttpApiKeyAuth do
11-
let(:sample_service) { ClientHelper.sample_service }
11+
let(:shapes) { ClientHelper.sample_shapes }
12+
let(:sample_client) { ClientHelper.sample_client(shapes: shapes) }
1213

1314
let(:client_class) do
14-
client_class = sample_service.const_get(:Client)
15+
client_class = sample_client.const_get(:Client)
1516
client_class.clear_plugins
16-
client_class.add_plugin(sample_service::Plugins::Endpoint)
17-
client_class.add_plugin(sample_service::Plugins::Auth)
17+
client_class.add_plugin(sample_client::Plugins::Auth)
18+
client_class.add_plugin(sample_client::Plugins::Endpoint)
1819
client_class.add_plugin(AnonymousAuth)
1920
client_class.add_plugin(HttpApiKeyAuth)
21+
client_class.add_plugin(Protocol)
22+
client_class.add_plugin(SignRequests)
2023
client_class.add_plugin(StubResponses)
2124
client_class
2225
end
2326

24-
let(:client) { client_class.new }
27+
let(:client) { client_class.new(stub_responses: true) }
2528

2629
it 'adds an :http_api_key option to config' do
2730
expect(client.config).to respond_to(:http_api_key)
@@ -32,20 +35,20 @@ module Plugins
3235
end
3336

3437
it 'does not default a :http_api_key' do
38+
client = client_class.new
3539
expect(client.config.http_api_key).to be_nil
3640
end
3741

3842
it 'does not default a :http_api_key_provider' do
43+
client = client_class.new
3944
expect(client.config.http_api_key_provider).to be_nil
4045
end
4146

4247
it 'has a default :http_api_key when :stub_responses is true' do
43-
client = client_class.new(stub_responses: true)
4448
expect(client.config.http_api_key).to eq('stubbed-api-key')
4549
end
4650

4751
it 'has a default :http_api_key_provider when :stub_responses is true' do
48-
client = client_class.new(stub_responses: true)
4952
provider = client.config.http_api_key_provider
5053
expect(provider).to be_a(HttpApiKeyProvider)
5154
expect(provider.identity({}).key).to eq('stubbed-api-key')
@@ -57,6 +60,35 @@ module Plugins
5760
expect(provider).to be_a(HttpApiKeyProvider)
5861
expect(provider.identity({}).key).to eq('api-key')
5962
end
63+
64+
context 'signing' do
65+
it 'signs in the header' do
66+
shapes['smithy.ruby.tests#SampleClient']['traits']['smithy.api#httpApiKeyAuth'] = {
67+
'name' => 'x-api-key', 'in' => 'header'
68+
}
69+
70+
output = client.operation
71+
expect(output.context.request.headers['x-api-key']).to eq('stubbed-api-key')
72+
end
73+
74+
it 'signs in the header with a custom scheme' do
75+
shapes['smithy.ruby.tests#SampleClient']['traits']['smithy.api#httpApiKeyAuth'] = {
76+
'name' => 'x-api-key', 'in' => 'header', 'scheme' => 'ApiKey'
77+
}
78+
79+
output = client.operation
80+
expect(output.context.request.headers['x-api-key']).to eq('ApiKey stubbed-api-key')
81+
end
82+
83+
it 'can sign on the query string' do
84+
shapes['smithy.ruby.tests#SampleClient']['traits']['smithy.api#httpApiKeyAuth'] = {
85+
'name' => 'x-api-key', 'in' => 'query'
86+
}
87+
88+
output = client.operation
89+
expect(output.context.request.endpoint.query).to include('x-api-key=stubbed-api-key')
90+
end
91+
end
6092
end
6193
end
6294
end

gems/smithy-client/spec/smithy-client/plugins/http_basic_auth_spec.rb

+21-7
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@ module Smithy
88
module Client
99
module Plugins
1010
describe HttpBasicAuth do
11-
let(:sample_service) { ClientHelper.sample_service }
11+
let(:shapes) { ClientHelper.sample_shapes }
12+
let(:sample_client) { ClientHelper.sample_client(shapes: shapes) }
1213

1314
let(:client_class) do
14-
client_class = sample_service.const_get(:Client)
15+
client_class = sample_client.const_get(:Client)
1516
client_class.clear_plugins
16-
client_class.add_plugin(sample_service::Plugins::Endpoint)
17-
client_class.add_plugin(sample_service::Plugins::Auth)
17+
client_class.add_plugin(sample_client::Plugins::Auth)
18+
client_class.add_plugin(sample_client::Plugins::Endpoint)
1819
client_class.add_plugin(AnonymousAuth)
1920
client_class.add_plugin(HttpBasicAuth)
21+
client_class.add_plugin(Protocol)
22+
client_class.add_plugin(SignRequests)
2023
client_class.add_plugin(StubResponses)
2124
client_class
2225
end
2326

24-
let(:client) { client_class.new }
27+
let(:client) { client_class.new(stub_responses: true) }
2528

2629
it 'adds an :http_login_username option to config' do
2730
expect(client.config).to respond_to(:http_login_username)
@@ -36,22 +39,22 @@ module Plugins
3639
end
3740

3841
it 'does not default an :http_login_username or :http_login_password' do
42+
client = client_class.new
3943
expect(client.config.http_login_username).to be_nil
4044
expect(client.config.http_login_password).to be_nil
4145
end
4246

4347
it 'does not default a :http_login_provider' do
48+
client = client_class.new
4449
expect(client.config.http_login_provider).to be_nil
4550
end
4651

4752
it 'has a default :http_login_username and :http_login_password when :stub_responses is true' do
48-
client = client_class.new(stub_responses: true)
4953
expect(client.config.http_login_username).to eq('stubbed-username')
5054
expect(client.config.http_login_password).to eq('stubbed-password')
5155
end
5256

5357
it 'has a default :http_login_provider when :stub_responses is true' do
54-
client = client_class.new(stub_responses: true)
5558
provider = client.config.http_login_provider
5659
expect(provider).to be_a(HttpLoginProvider)
5760
identity = provider.identity({})
@@ -77,6 +80,17 @@ module Plugins
7780
provider = client.config.http_login_provider
7881
expect(provider).to be_nil
7982
end
83+
84+
context 'signing' do
85+
it 'signs in the header' do
86+
shapes['smithy.ruby.tests#SampleClient']['traits']['smithy.api#httpBasicAuth'] = {}
87+
88+
output = client.operation
89+
identity_string = "#{client.config.http_login_username}:#{client.config.http_login_password}"
90+
expect(output.context.request.headers['Authorization'])
91+
.to eq("Basic #{Base64.strict_encode64(identity_string)}")
92+
end
93+
end
8094
end
8195
end
8296
end

gems/smithy-client/spec/smithy-client/plugins/http_bearer_auth_spec.rb

+20-7
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@ module Smithy
88
module Client
99
module Plugins
1010
describe HttpBearerAuth do
11-
let(:sample_service) { ClientHelper.sample_service }
11+
let(:shapes) { ClientHelper.sample_shapes }
12+
let(:sample_client) { ClientHelper.sample_client(shapes: shapes) }
1213

1314
let(:client_class) do
14-
client_class = sample_service.const_get(:Client)
15+
client_class = sample_client.const_get(:Client)
1516
client_class.clear_plugins
16-
client_class.add_plugin(sample_service::Plugins::Endpoint)
17-
client_class.add_plugin(sample_service::Plugins::Auth)
17+
client_class.add_plugin(sample_client::Plugins::Auth)
18+
client_class.add_plugin(sample_client::Plugins::Endpoint)
1819
client_class.add_plugin(AnonymousAuth)
1920
client_class.add_plugin(HttpBearerAuth)
21+
client_class.add_plugin(Protocol)
22+
client_class.add_plugin(SignRequests)
2023
client_class.add_plugin(StubResponses)
2124
client_class
2225
end
2326

24-
let(:client) { client_class.new }
27+
let(:client) { client_class.new(stub_responses: true) }
2528

2629
it 'adds an :http_bearer_token option to config' do
2730
expect(client.config).to respond_to(:http_bearer_token)
@@ -32,20 +35,20 @@ module Plugins
3235
end
3336

3437
it 'does not default a :http_bearer_token' do
38+
client = client_class.new
3539
expect(client.config.http_bearer_token).to be_nil
3640
end
3741

3842
it 'does not default a :http_bearer_provider' do
43+
client = client_class.new
3944
expect(client.config.http_bearer_provider).to be_nil
4045
end
4146

4247
it 'has a default :http_bearer_token when :stub_responses is true' do
43-
client = client_class.new(stub_responses: true)
4448
expect(client.config.http_bearer_token).to eq('stubbed-bearer-token')
4549
end
4650

4751
it 'has a default :http_bearer_provider when :stub_responses is true' do
48-
client = client_class.new(stub_responses: true)
4952
provider = client.config.http_bearer_provider
5053
expect(provider).to be_a(HttpBearerProvider)
5154
expect(provider.identity({}).token).to eq('stubbed-bearer-token')
@@ -57,6 +60,16 @@ module Plugins
5760
expect(provider).to be_a(HttpBearerProvider)
5861
expect(provider.identity({}).token).to eq('bearer')
5962
end
63+
64+
context 'signing' do
65+
it 'signs in the header' do
66+
shapes['smithy.ruby.tests#SampleClient']['traits']['smithy.api#httpBearerAuth'] = {}
67+
68+
output = client.operation
69+
expect(output.context.request.headers['Authorization'])
70+
.to eq("Bearer #{client.config.http_bearer_token}")
71+
end
72+
end
6073
end
6174
end
6275
end

0 commit comments

Comments
 (0)