Skip to content

Commit

Permalink
Improvements (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
mullermp committed Aug 25, 2023
1 parent 96e4ecc commit 6d6b724
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 36 deletions.
2 changes: 1 addition & 1 deletion codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ allprojects {
version = "0.1.0"
}

extra["smithyVersion"] = "1.33.0"
extra["smithyVersion"] = "1.37.0"

// The root project doesn't produce a JAR.
tasks["jar"].enabled = false
Expand Down
10 changes: 0 additions & 10 deletions hearth/lib/hearth/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,5 @@ def [](key)
def []=(key, value)
@metadata[key] = value
end

# @api private
def interceptor_context(input, output)
Hearth::Interceptor::Context.new(
input: input,
request: request,
response: response,
output: output
)
end
end
end
13 changes: 11 additions & 2 deletions hearth/lib/hearth/interceptor_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ def add(interceptor)
# error is encountered.
# @return nil if successful, an exception otherwise
def apply(hook:, input:, context:, output:, aggregate_errors: false)
ictx = context.interceptor_context(input, output)
i_ctx = interceptor_context(input, context, output)
last_error = nil
@interceptors.each do |i|
next unless i.respond_to?(hook)

begin
i.send(hook, ictx)
i.send(hook, i_ctx)
rescue StandardError => e
context.logger.error(last_error) if last_error
last_error = e
Expand All @@ -74,6 +74,15 @@ def each(&block)

private

def interceptor_context(input, context, output)
Hearth::Interceptor::Context.new(
input: input,
request: context.request,
response: context.response,
output: output
)
end

def set_output_error(last_error, context, output)
return unless last_error && output

Expand Down
4 changes: 2 additions & 2 deletions hearth/lib/hearth/signers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ module Signers
# Base class for all Signer classes.
class Base
def sign(request:, identity:, properties:)
# Do nothing by default
raise NotImplementedError
end

def reset(request:, properties:)
# Do nothing by default
raise NotImplementedError
end
end
end
Expand Down
10 changes: 9 additions & 1 deletion hearth/lib/hearth/signers/anonymous.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
module Hearth
module Signers
# A signer that does not sign requests.
class Anonymous < Signers::Base; end
class Anonymous < Signers::Base
def sign(request:, identity:, properties:)
# Do nothing.
end

def reset(request:, properties:)
# Do nothing.
end
end
end
end
13 changes: 0 additions & 13 deletions hearth/spec/hearth/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,6 @@ module Hearth
end
end

describe '#interceptor_context' do
let(:input) { double('input') }
let(:output) { double('output') }

it 'creates an interceptor context' do
ictx = subject.interceptor_context(input, output)
expect(ictx.input).to eq(input)
expect(ictx.request).to eq(request)
expect(ictx.response).to eq(response)
expect(ictx.output).to eq(output)
end
end

describe '#[]' do
it 'returns the metadata for the given key' do
expect(subject[:foo]).to eq('bar')
Expand Down
28 changes: 21 additions & 7 deletions hearth/spec/hearth/interceptor_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,17 @@ def read_before_execution(_ctx); end
let(:logger) { double('logger') }
let(:input) { double('input') }
let(:output) { double('output', error: nil) }
let(:context) { double('context', logger: logger) }
let(:ictx) { double('interceptor_context') }
let(:request) { double('request') }
let(:response) { double('response') }
let(:context) do
double(
'context',
request: request,
response: response,
logger: logger
)
end
let(:i_ctx) { double('interceptor_context') }
let(:error) { StandardError.new }

let(:interceptors) do
Expand All @@ -81,14 +90,19 @@ def read_before_execution(_ctx); end
let(:hook) { :read_before_execution }

before(:each) do
allow(context).to receive(:interceptor_context).and_return(ictx)
allow(context).to receive(:interceptor_context).and_return(i_ctx)
end

it 'calls each interceptor hook with context' do
expect(context).to receive(:interceptor_context)
.with(input, output).and_return(ictx)
expect(interceptor1).to receive(hook).with(ictx)
expect(interceptor2).to receive(hook).with(ictx)
expect(Interceptor::Context).to receive(:new).with(
input: input,
request: request,
response: response,
output: output
).and_return(i_ctx)

expect(interceptor1).to receive(hook).with(i_ctx)
expect(interceptor2).to receive(hook).with(i_ctx)

out = interceptors.apply(
hook: hook,
Expand Down
31 changes: 31 additions & 0 deletions hearth/spec/hearth/signers/anonymous_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module Hearth
module Signers
describe Anonymous do
let(:request) { HTTP::Request.new }
let(:identity) { Identities::Anonymous.new }
let(:properties) { {} }

describe '#sign' do
it 'does not modify the request' do
expect do
subject.sign(
request: request,
identity: identity,
properties: properties
)
end.not_to(change { request })
end
end

describe '#reset' do
it 'does not modify the request' do
expect do
subject.reset(request: request, properties: properties)
end.not_to(change { request })
end
end
end
end
end
25 changes: 25 additions & 0 deletions hearth/spec/hearth/signers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Hearth
module Signers
describe Base do
let(:request) { HTTP::Request.new }
let(:identity) { Identities::Anonymous.new }
let(:properties) { {} }

it 'defines the interface' do
expect do
subject.sign(
request: request,
identity: identity,
properties: properties
)
end.to raise_error(NotImplementedError)

expect do
subject.reset(request: request, properties: properties)
end.to raise_error(NotImplementedError)
end
end
end
end

0 comments on commit 6d6b724

Please sign in to comment.