Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Commit

Permalink
Use pacto classes for stubbing
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlinc committed May 29, 2014
1 parent 31b9f31 commit 21fbd38
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 36 deletions.
1 change: 1 addition & 0 deletions lib/pacto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require 'pacto/request_pattern'
require 'pacto/core/http_middleware'
require 'pacto/consumer/faraday_driver'
require 'pacto/actors/json_generator'
require 'pacto/core/pacto_request'
require 'pacto/core/pacto_response'
require 'pacto/core/contract_registry'
Expand Down
18 changes: 18 additions & 0 deletions lib/pacto/actors/json_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Pacto
module Actors
class JSONGenerator
def self.build_request(request_clause)
data = request_clause.to_hash
data['uri'] = request_clause.uri
data['body'] = JSON::Generator.generate(data['schema'])
Pacto::PactoRequest.new(data)
end

def self.build_response(response_clause)
data = response_clause.to_hash
data['body'] = JSON::Generator.generate(data['schema'])
Pacto::PactoResponse.new(data)
end
end
end
end
3 changes: 2 additions & 1 deletion lib/pacto/core/pacto_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

module Pacto
class PactoRequest
# FIXME: Need case insensitive header lookup, but case-sensitive storage
attr_reader :headers, :body, :method, :uri

def initialize(data)
mash = Hashie::Mash.new data
@headers = mash.headers
@headers = mash.headers.to_h
@body = mash.body
@method = mash[:method]
@uri = mash.uri
Expand Down
8 changes: 5 additions & 3 deletions lib/pacto/core/pacto_response.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module Pacto
class PactoResponse
# FIXME: Need case insensitive header lookup, but case-sensitive storage
attr_accessor :body, :headers, :status

def initialize(data)
@headers = data[:headers]
@body = data[:body]
@status = data[:status]
mash = Hashie::Mash.new data
@headers = mash.headers.to_h
@body = mash.body
@status = mash.status.to_i
end
end
end
2 changes: 1 addition & 1 deletion lib/pacto/generator/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Filters

def filter_request_headers(request, response)
# FIXME: Do we need to handle all these cases in real situations, or just because of stubbing?
vary_headers = response.headers['Vary'] || []
vary_headers = response.headers['vary'] || response.headers['Vary'] || []
vary_headers = [vary_headers] if vary_headers.is_a? String
vary_headers = vary_headers.map do |h|
h.split(',').map(&:strip)
Expand Down
6 changes: 2 additions & 4 deletions lib/pacto/request_clause.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class RequestClause < Hashie::Dash
property :path
property :headers
property :params, default: {}
property :request_builder, default: Pacto::Actors::JSONGenerator

def initialize(definition)
mash = Hashie::Mash.new definition
Expand All @@ -24,10 +25,7 @@ def uri
end

def to_pacto_request
data = to_hash
data['uri'] = uri
data['body'] = '' # Should be strategy
Pacto::PactoRequest.new(data)
request_builder.build_request self
end

# FIXME: Send a PR to Hashie so it doesn't coerce values that already match the target class
Expand Down
5 changes: 3 additions & 2 deletions lib/pacto/response_clause.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ class ResponseClause < Hashie::Dash
property :status
property :headers
property :schema, default: {}
property :response_builder, default: Pacto::Actors::JSONGenerator

def body
@body ||= JSON::Generator.generate(schema)
def to_pacto_response
response_builder.build_response self
end
end
end
9 changes: 5 additions & 4 deletions lib/pacto/stubs/webmock_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ def initialize(middleware)
end
end

def stub_request!(request, response)
uri_pattern = UriPattern.for(request)
stub = WebMock.stub_request(request.method, uri_pattern)
def stub_request!(request_clause, response_clause)
uri_pattern = UriPattern.for(request_clause)
stub = WebMock.stub_request(request_clause.method, uri_pattern)

stub.request_pattern.with(strict_details(request)) if Pacto.configuration.strict_matchers
stub.request_pattern.with(strict_details(request_clause)) if Pacto.configuration.strict_matchers
response = response_clause.to_pacto_response

stub.to_return(
:status => response.status,
Expand Down
16 changes: 9 additions & 7 deletions spec/unit/pacto/response_clause_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ module Pacto
expect(response.schema).to eq(Hash.new)
end

describe 'the response body' do
let(:generated_body) { double }
describe '#to_pacto_response' do
context 'using the default builder' do
let(:generated_body) { double }

it 'is the json generated from the schema' do
JSON::Generator.should_receive(:generate).
with(definition['schema']).
and_return(generated_body)
it 'generates the body from the schema' do
JSON::Generator.should_receive(:generate).
with(definition['schema']).
and_return(generated_body)

expect(response.body).to eq(generated_body)
expect(response.to_pacto_response.body).to eq(generated_body)
end
end
end
end
Expand Down
42 changes: 28 additions & 14 deletions spec/unit/pacto/stubs/webmock_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@ module Stubs
let(:method) { :get }

let(:response) do
double(
Fabricate(
:response_clause,
:status => 200,
:headers => {},
:body => body
:schema => {
type: 'object',
required: ['message'],
properties: {
message: {
type: 'string',
default: 'foo'
}
}
}
)
end

Expand All @@ -39,10 +49,11 @@ module Stubs
subject(:adapter) { WebMockAdapter.new middleware }

before(:each) do
pacto_response = response.to_pacto_response
stubbed_request.stub(:to_return).with(
:status => response.status,
:headers => response.headers,
:body => response.body.to_json
:status => pacto_response.status,
:headers => pacto_response.headers,
:body => pacto_response.body
)
stubbed_request.stub(:request_pattern).and_return request_pattern
end
Expand Down Expand Up @@ -81,10 +92,11 @@ module Stubs
end

it 'stubs the response body with a json representation' do
pacto_response = response.to_pacto_response
stubbed_request.should_receive(:to_return).with(
:status => response.status,
:headers => response.headers,
:body => response.body.to_json
:status => pacto_response.status,
:headers => pacto_response.headers,
:body => pacto_response.body
)

request_pattern.stub(:with)
Expand All @@ -98,10 +110,11 @@ module Stubs
end

it 'stubs the response body with a json representation' do
pacto_response = response.to_pacto_response
stubbed_request.should_receive(:to_return).with(
:status => response.status,
:headers => response.headers,
:body => response.body.to_json
:status => pacto_response.status,
:headers => pacto_response.headers,
:body => pacto_response.body
)

request_pattern.stub(:with)
Expand All @@ -114,10 +127,11 @@ module Stubs
let(:body) { nil }

it 'stubs the response body with the original body' do
pacto_response = response.to_pacto_response
stubbed_request.should_receive(:to_return).with(
:status => response.status,
:headers => response.headers,
:body => response.body
:status => pacto_response.status,
:headers => pacto_response.headers,
:body => pacto_response.body
)

request_pattern.stub(:with)
Expand Down

0 comments on commit 21fbd38

Please sign in to comment.