-
Notifications
You must be signed in to change notification settings - Fork 6
Stubs
Stubs are classes that build a network response from provided values instead of performing a real network call. They are used in tests to control output. Stubs have a 1:1 mapping to Smithy structure shapes.
The stub classes take an empty Response object and valid stubbing data as a hash. Following protocol rules, the Response object is populated with the data as if it came from a service response.
module SampleService
# @api private
module Stubs
class CreateHighScore
def self.build(params, context:)
Params::CreateHighScoreOutput.build(params, context: context)
end
def self.validate!(output, context:)
Validators::CreateHighScoreOutput.validate!(output, context: context)
end
def self.default(visited = [])
{
high_score: HighScoreAttributes.default(visited),
location: 'location',
}
end
def self.stub(http_resp, stub:)
data = {}
http_resp.status = 201
http_resp.headers['Location'] = stub[:location] unless stub[:location].nil? || stub[:location].empty?
http_resp.headers['Content-Type'] = 'application/json'
data = Stubs::HighScoreAttributes.stub(stub[:high_score]) unless stub[:high_score].nil?
http_resp.body.write(Hearth::JSON.dump(data))
end
end
end
end
The Response object is populated with a status code, a body, and any appropriate headers. The Response object is eventually parsed by the parser middleware into an output object with a corresponding Type or as an error.
Default values are provided in a class method and are built in a nested context. When no values are provided, the defaults for all structures are used. When any value is provided, all other values are nil. Structures can be recursive, so previously visited defaults are tracked and checked.