wiremock_mapper 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c55d34c227d457aecbfb8fc48fe1540c4fff8e6a
4
- data.tar.gz: 7ea5eb65ccde458834d9773833a415d89f4f8985
3
+ metadata.gz: 44fa1527716e579fdceae8884c5afbe07db66862
4
+ data.tar.gz: ca83101dfb8018ed99bcc69193e21f9f5c283d45
5
5
  SHA512:
6
- metadata.gz: b465342380e86cf5bcbbe1bfbe33de1d550cb05541c645fd894770b2f65800df035e8d95c914d618987fc8753596e96ece081af0709da76a6755ef3ce8b1641a
7
- data.tar.gz: 37e5c4cf467e3017efbc48fefe3ed00b1a24b6c538b70463172202100093f65464ed51e2f8fb926446c2ce69bc8388d11b406cb1047d3fc47beaf6fa4975b94b
6
+ metadata.gz: f7962ea5867013599c6883d8f6eacb06b30dac8718168803099a1026b28f29b500dd7894204b0445f68dabfb556156b33be657f1f366f190cfe4aceebd2cc7f3
7
+ data.tar.gz: 58ce785da47a5997b4d13d24d1186321e3792701124e7cd7f7d6e0c8ea5b8badb3c07978e22b64e4cf84acfa05ebedff34a3f4497ddcbc715cb4bbdf2f7ea5c0
data/README.md CHANGED
@@ -11,12 +11,18 @@
11
11
  ####Usage Example
12
12
  ```ruby
13
13
  WireMockMapper::Configuration.set_wiremock_url('http://my_wiremock.com')
14
- WireMockMapper::Configuration.add_request_header('Some-Header').equal_to('some_value')
14
+
15
+ WireMockMapper::Configuration.create_global_mapping do |request, respond|
16
+ request.with_header('Some-Header').equal_to('some_value')
17
+ .with_cookie('Some-Cookie').equal_to('some_cookie_value')
18
+ respond.with_status(200)
19
+ end
15
20
 
16
21
  WireMockMapper.create_mapping do |request, respond|
17
22
  request.receives_post
18
23
  .with_url_path.equal_to('path/to/stub')
19
24
  .with_header('Some-Other-Header').equal_to('some_other_value')
25
+ .with_cookie('Some-Other-Cookie').equal_to('some_other_cookie_value')
20
26
  .with_body.equal_to(foo: bar)
21
27
  respond.with_body('good job!')
22
28
  end
data/lib/configuration.rb CHANGED
@@ -1,11 +1,20 @@
1
+ require_relative 'request_builder'
2
+ require_relative 'response_builder'
3
+
1
4
  module WireMockMapper
2
5
  class Configuration
3
- @request_headers = {}
4
6
  @wiremock_url = ''
5
7
 
8
+ @request_builder = RequestBuilder.new
9
+ @response_builder = ResponseBuilder.new
10
+
6
11
  class << self
7
- attr_reader :request_headers, :wiremock_url
12
+ attr_reader :request_builder, :response_builder, :wiremock_url
8
13
 
14
+ # Add mappings to include for all future mappings
15
+ def create_global_mapping
16
+ yield @request_builder, @response_builder
17
+ end
9
18
 
10
19
  # Add a request header for all future requests
11
20
  # @param key [String] header key
@@ -3,9 +3,8 @@ require_relative 'url_match_builder'
3
3
 
4
4
  module WireMockMapper
5
5
  class RequestBuilder
6
- def initialize(configuration = nil)
6
+ def initialize
7
7
  @options = {}
8
- @options[:headers] = configuration.request_headers if configuration
9
8
  end
10
9
 
11
10
  HTTP_VERBS = %w(ANY DELETE GET HEAD OPTIONS POST PUT TRACE).freeze
@@ -4,26 +4,31 @@ require_relative 'request_builder'
4
4
  require_relative 'response_builder'
5
5
 
6
6
  module WireMockMapper
7
- VERSION = '0.3.0'.freeze
7
+ VERSION = '0.4.0'.freeze
8
8
 
9
9
  def self.create_mapping(url = Configuration.wiremock_url)
10
- request_builder = RequestBuilder.new
11
- response_builder = ResponseBuilder.new
10
+ request_builder = deep_clone(Configuration.request_builder)
11
+ response_builder = deep_clone(Configuration.response_builder)
12
12
 
13
13
  yield request_builder, response_builder
14
14
 
15
- send(url, request: request_builder, response: response_builder)
15
+ send_to_wiremock(url, request: request_builder, response: response_builder)
16
16
  end
17
17
 
18
18
  WIREMOCK_NEW_MAPPING_PATH = '__admin/mappings/new'.freeze
19
19
  private_constant :WIREMOCK_NEW_MAPPING_PATH
20
20
 
21
- def self.send(url, body)
21
+ def self.deep_clone(object)
22
+ Marshal.load(Marshal.dump(object))
23
+ end
24
+ private_class_method :deep_clone
25
+
26
+ def self.send_to_wiremock(url, body)
22
27
  uri = URI(URI.join(url, WIREMOCK_NEW_MAPPING_PATH))
23
28
  http = Net::HTTP.new(uri.host, uri.port)
24
29
  request = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
25
30
  request.body = body.to_json
26
31
  http.request(request)
27
32
  end
28
- private_class_method :send
33
+ private_class_method :send_to_wiremock
29
34
  end
@@ -1,14 +1,23 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe WireMockMapper::Configuration do
4
- context 'request_header' do
5
- it 'returns a MatchBuilder' do
6
- WireMockMapper::Configuration.add_request_header('some_header')
7
- expect(WireMockMapper::Configuration.request_headers['some_header']).to be_a(WireMockMapper::MatchBuilder)
4
+ describe 'create_global_mapping' do
5
+ it 'configures the request builder attribute' do
6
+ expect(WireMockMapper::Configuration.request_builder.to_hash).to eq({})
7
+
8
+ WireMockMapper::Configuration.create_global_mapping do |request, _response|
9
+ request.with_header('foo').equal_to('bar')
10
+ end
11
+
12
+ header_key_value = WireMockMapper::Configuration.request_builder.to_hash[:headers]['foo']
13
+ expect(header_key_value).to be_a(WireMockMapper::MatchBuilder)
14
+
15
+ # BOOOOOO!
16
+ WireMockMapper::Configuration.instance_variable_set(:@request_builder, WireMockMapper::RequestBuilder.new)
8
17
  end
9
18
  end
10
19
 
11
- context 'wiremock_url' do
20
+ describe 'wiremock_url' do
12
21
  it 'sets the wiremock url' do
13
22
  WireMockMapper::Configuration.set_wiremock_url('http://whereever.com')
14
23
  expect(WireMockMapper::Configuration.wiremock_url).to eq('http://whereever.com')
@@ -22,5 +22,32 @@ describe WireMockMapper do
22
22
  respond.with_body('some response body')
23
23
  end
24
24
  end
25
+
26
+ # rubocop:disable Metrics/LineLength
27
+ it 'posts the correct json with configured global mappings to the wiremock url' do
28
+ url = 'http://nowhere.com'
29
+ expected_request_body = { request: { 'method' => 'POST',
30
+ 'urlPath' => '/some/path',
31
+ 'headers' => { 'some_global_header' => { 'equalTo' => 'some global header value' },
32
+ 'some_header' => { 'equalTo' => 'some header value' } },
33
+ 'bodyPatterns' => [
34
+ { 'matches' => 'some request body' }
35
+ ] },
36
+ response: { 'body' => 'some response body' } }
37
+ stub_request(:post, "#{url}/__admin/mappings/new").with(body: expected_request_body)
38
+
39
+ request_builder = WireMockMapper::RequestBuilder.new.with_header('some_global_header').equal_to('some global header value')
40
+ expect(WireMockMapper::Configuration).to receive(:request_builder).and_return(request_builder)
41
+
42
+ WireMockMapper.create_mapping(url) do |request, respond|
43
+ request.receives_post
44
+ .with_url_path.equal_to('/some/path')
45
+ .with_header('some_header').equal_to('some header value')
46
+ .with_body.matching('some request body')
47
+
48
+ respond.with_body('some response body')
49
+ end
50
+ end
51
+ # rubocop:enable Metrics/LineLength
25
52
  end
26
53
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wiremock_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Isaac Datlof
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-13 00:00:00.000000000 Z
11
+ date: 2016-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: codeclimate-test-reporter