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

Commit

Permalink
Merge pull request #127 from thoughtworks/http_method
Browse files Browse the repository at this point in the history
Rename method to http_method (Fixes #69)
  • Loading branch information
maxlinc committed Jun 24, 2014
2 parents 09fa297 + 2441b7c commit ab0e20b
Show file tree
Hide file tree
Showing 29 changed files with 67 additions and 53 deletions.
4 changes: 2 additions & 2 deletions features/configuration/strict_matchers.feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Feature: Strict Matching
"""json
{
"request": {
"method": "GET",
"http_method": "GET",
"path": "/hello/:id",
"headers": {
"Accept": "application/json"
Expand Down Expand Up @@ -94,4 +94,4 @@ Feature: Strict Matching
Exact: {"message":"Hello, world!"}
Wrong headers: {"message":"Hello, world!"}
ID placeholder: {"message":"Hello, world!"}
"""
"""
6 changes: 3 additions & 3 deletions features/generate/generation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Feature: Contract Generation
"""
{
"request": {
"method": "GET",
"http_method": "GET",
"path": "/hello",
"headers": {
"Accept": "application/json"
Expand Down Expand Up @@ -50,7 +50,7 @@ Feature: Contract Generation
"headers": {
"Accept": "application/json"
},
"method": "get",
"http_method": "get",
"path": "/hello"
},
"response": {
Expand All @@ -74,4 +74,4 @@ Feature: Contract Generation
}
}
"""
"""
2 changes: 1 addition & 1 deletion features/stub/templates.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Feature: Templating
"""json
{
"request": {
"method": "GET",
"http_method": "GET",
"path": "/hello",
"headers": {
"Accept": "application/json"
Expand Down
2 changes: 1 addition & 1 deletion features/validate/body_only.feature
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Feature: Validation
"""json
{
"request": {
"method": "GET",
"http_method": "GET",
"path": "/hello",
"headers": {
"Accept": "application/json"
Expand Down
4 changes: 2 additions & 2 deletions features/validate/meta_validation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Feature: Meta-validation
"""
{
"request": {
"method": "GET",
"http_method": "GET",
"path": "/hello_world",
"headers": {
"Accept": "application/json"
Expand Down Expand Up @@ -75,7 +75,7 @@ Feature: Meta-validation
"""
{
"request": {
"method": "GET",
"http_method": "GET",
"path": "/hello_world"
},
Expand Down
2 changes: 1 addition & 1 deletion features/validate/validation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Feature: Validation
"""
{
"request": {
"method": "GET",
"http_method": "GET",
"path": "/hello",
"headers": { "Accept": "application/json" },
"params": {}
Expand Down
1 change: 1 addition & 0 deletions lib/pacto/actors/from_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def build_request(contract, values = {})
data = contract.request.to_hash
data['uri'] = contract.request.uri
data['body'] = example.request.body
data['method'] = contract.request.http_method
Pacto::PactoRequest.new(data)
else
@fallback_actor.build_request contract, values
Expand Down
1 change: 1 addition & 0 deletions lib/pacto/actors/json_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def self.build_request(contract, _values = {})
data = contract.request.to_hash
data['uri'] = contract.request.uri
data['body'] = JSON::Generator.generate(data['schema'])
data['method'] = contract.request.http_method
Pacto::PactoRequest.new(data)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/pacto/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def generate_contract(source, request, response)
def generate_request(request, response, source)
{
:headers => @filters.filter_request_headers(request, response),
:method => request.method,
:http_method => request.method,
:params => request.uri.query_values,
:path => request.uri.path,
:schema => generate_schema(source, request.body)
Expand Down
9 changes: 9 additions & 0 deletions lib/pacto/native_contract_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def build_from_file(contract_path, host)
definition['request'].merge!('host' => host)
body_to_schema(definition, 'request', contract_path)
body_to_schema(definition, 'response', contract_path)
method_to_http_method(definition, contract_path)
request = RequestClause.new(definition['request'])
response = ResponseClause.new(definition['response'])
Contract.new(request: request, response: response, file: contract_path, name: definition['name'], examples: definition['examples'])
Expand All @@ -28,5 +29,13 @@ def body_to_schema(definition, section, file)
Pacto::UI.deprecation "Contract format deprecation: #{section}:body will be moved to #{section}:schema (#{file})"
definition[section]['schema'] = schema
end

def method_to_http_method(definition, file)
method = definition['request'].delete 'method'
return nil unless method

Pacto::UI.deprecation "Contract format deprecation: request:method will be moved to request:http_method (#{file})"
definition['request']['http_method'] = method
end
end
end
6 changes: 3 additions & 3 deletions lib/pacto/request_clause.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ class RequestClause < Hashie::Dash
include Hashie::Extensions::Coercion
# include Hashie::Extensions::IndifferentAccess # remove this if we cleanup string vs symbol
property :host # required?
property :method, required: true
property :http_method, required: true
property :schema, default: {}
property :path
property :headers
property :params, default: {}

def initialize(definition)
mash = Hashie::Mash.new definition
mash['method'] = normalize(mash['method'])
mash['http_method'] = normalize(mash['http_method'])
super mash
end

def method=(method)
def http_method=(method)
normalize(method)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/pacto/request_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Pacto
class RequestPattern
def self.for(base_request)
uri_pattern = UriPattern.for(base_request)
WebMock::RequestPattern.new(base_request.method, uri_pattern)
WebMock::RequestPattern.new(base_request.http_method, uri_pattern)
end
end
end
7 changes: 4 additions & 3 deletions lib/pacto/stubs/webmock_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Adapters
module WebMock
class PactoRequest < Pacto::PactoRequest
extend Forwardable
def_delegators :@webmock_request_signature, :headers, :body, :method, :uri, :to_s
def_delegators :@webmock_request_signature, :headers, :method, :body, :uri, :to_s

def initialize(webmock_request_signature)
@webmock_request_signature = webmock_request_signature
Expand All @@ -17,6 +17,7 @@ def path
@webmock_request_signature.uri.path
end
end

class PactoResponse < Pacto::PactoResponse
extend Forwardable
def_delegators :@webmock_response, :body, :body=, :headers=, :status=, :to_s
Expand Down Expand Up @@ -51,7 +52,7 @@ def initialize(middleware)
def stub_request!(contract)
request_clause = contract.request
uri_pattern = UriPattern.for(request_clause)
stub = WebMock.stub_request(request_clause.method, uri_pattern)
stub = WebMock.stub_request(request_clause.http_method, uri_pattern)

stub.request_pattern.with(strict_details(request_clause)) if Pacto.configuration.strict_matchers

Expand Down Expand Up @@ -99,7 +100,7 @@ def strict_details(request)
end

def webmock_params_key(request)
request.method == :get ? :query : :body
request.http_method == :get ? :query : :body
end
end
end
Expand Down
10 changes: 7 additions & 3 deletions resources/contract_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
},
"request": {
"type": "object",
"required": ["method", "path"],
"required": ["path"],
"properties": {
"method": {
"_deprecated": true,
"type": "string"
},
"http_method": {
"type": "string"
},
"path": {
Expand All @@ -27,7 +31,7 @@
"headers": {
"type": "object"
},
"params": {
"params": {
"type": "object"
},
"body": {
Expand Down Expand Up @@ -69,4 +73,4 @@
}
}
}
}
}
4 changes: 2 additions & 2 deletions samples/contracts/localhost/api/echo.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"request": {
"headers": {
},
"method": "get",
"http_method": "get",
"path": "/api/echo"
},
"response": {
Expand All @@ -23,4 +23,4 @@
}
}
}
}
}
4 changes: 2 additions & 2 deletions samples/contracts/localhost/api/ping.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"request": {
"headers": {
},
"method": "get",
"http_method": "get",
"path": "/api/ping"
},
"response": {
Expand Down Expand Up @@ -34,4 +34,4 @@
}
}
}
}
}
2 changes: 1 addition & 1 deletion spec/fabricators/contract_fabricator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
Fabricator(:request_clause, from: Pacto::RequestClause) do
initialize_with { @_klass.new to_hash } # Hash based initialization
host { 'example.com' }
method { 'GET' }
http_method { 'GET' }
path { '/abcd' }
headers do
{
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/contracts/contract.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"request": {
"method": "GET",
"http_method": "GET",
"path": "/hello_world",
"headers": {
"Accept": "application/json"
Expand Down
4 changes: 2 additions & 2 deletions spec/fixtures/contracts/contract_with_examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"request": {
"headers": {
},
"method": "get",
"http_method": "get",
"path": "/api/echo",
"schema": {
"type": "object",
Expand Down Expand Up @@ -55,4 +55,4 @@
}
}
}
}
}
2 changes: 1 addition & 1 deletion spec/fixtures/contracts/simple_contract.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"request": {
"method": "GET",
"http_method": "GET",
"path": "/hello",
"headers": {
"Accept": "application/json"
Expand Down
4 changes: 2 additions & 2 deletions spec/fixtures/contracts/strict_contract.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"request": {
"method": "GET",
"http_method": "GET",
"path": "/strict",
"headers": {
"Accept": "application/json"
Expand Down Expand Up @@ -31,4 +31,4 @@
}
}
}
}
}
2 changes: 1 addition & 1 deletion spec/fixtures/contracts/templating_contract.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"request": {
"method": "GET",
"http_method": "GET",
"path": "/echo",
"headers": {
"Accept": "application/json",
Expand Down
2 changes: 0 additions & 2 deletions spec/unit/pacto/consumer/faraday_driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class Consumer
end

context 'for a POST request' do
let(:method) { 'POST' }

it 'makes the request thru the http client' do
strategy.execute post_request
expect(WebMock).to have_requested(:post, 'http://localhost/hello_world?foo=bar')
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/pacto/contract_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Pacto
describe Contract do
let(:request_clause) do
Pacto::RequestClause.new(
method: 'GET',
http_method: 'GET',
host: 'http://example.com',
schema: {
:type => 'object',
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/pacto/generator/filters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Generator
let(:request) do
RequestClause.new(
host: record_host,
method: 'GET',
http_method: 'GET',
path: '/abcd',
headers: {
'Server' => ['example.com'],
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/pacto/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def pretty(obj)

it 'normalizes the request method' do
generated_request = subject['request']
expect(generated_request['method']).to eq(request.method.downcase.to_s)
expect(generated_request['http_method']).to eq(request.method.downcase.to_s)
end

it 'sets the response attributes' do
Expand Down
10 changes: 5 additions & 5 deletions spec/unit/pacto/request_clause_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Pacto
subject(:request) do
req_hash = {
host: host,
'method' => method,
'http_method' => method,
'path' => path,
'headers' => headers,
'params' => params
Expand All @@ -25,17 +25,17 @@ module Pacto
expect(request.host).to eq host
end

describe '#method' do
describe '#http_method' do
it 'delegates to definition' do
expect(request.method).to eq :get
expect(request.http_method).to eq :get
end

it 'downcases the method' do
expect(request.method).to eq request.method.downcase
expect(request.http_method).to eq request.http_method.downcase
end

it 'returns a symbol' do
expect(request.method).to be_kind_of Symbol
expect(request.http_method).to be_kind_of Symbol
end
end

Expand Down
Loading

0 comments on commit ab0e20b

Please sign in to comment.