wiremock_mapper 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -3,7 +3,7 @@ require 'rubocop/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task default: :spec
6
+ task default: [:rubocop, :spec]
7
7
 
8
8
  RuboCop::RakeTask.new(:rubocop) do |task|
9
9
  task.patterns = ['lib/**/*.rb', 'spec/**/*.rb']
@@ -1,8 +1,10 @@
1
1
  module WireMockMapper
2
2
  module Builders
3
3
  module Helpers
4
- def self.regexp_to_string(regexp)
5
- %r{^/(.*)/$}.match(regexp.inspect)[1]
4
+ class << self
5
+ def regexp_to_string(regexp)
6
+ %r{^/(.*)/$}.match(regexp.inspect)[1]
7
+ end
6
8
  end
7
9
  end
8
10
  end
@@ -35,7 +35,7 @@ module WireMockMapper
35
35
  end
36
36
 
37
37
  # Match if attribute json is equal to the arg
38
- # @param json [String] json to compare against
38
+ # @param json [String, Hash] json to compare against
39
39
  # @param ignore_array_order [true, false] flag to ignore the order of arrays
40
40
  # @param ignore_extra_elements [true, false] flag to ignore any extra elements
41
41
  # @return [RequestBuilder] calling request builder for chaining additional attributes
@@ -2,29 +2,54 @@ require 'net/http'
2
2
  require_relative 'configuration'
3
3
 
4
4
  module WireMockMapper
5
- def self.create_mapping(url = Configuration.wiremock_url)
6
- request_builder = deep_clone(Configuration.request_builder)
7
- response_builder = deep_clone(Configuration.response_builder)
5
+ class << self
6
+ def create_mapping(url = Configuration.wiremock_url)
7
+ request_builder = deep_clone(Configuration.request_builder)
8
+ response_builder = deep_clone(Configuration.response_builder)
8
9
 
9
- yield request_builder, response_builder
10
+ yield request_builder, response_builder
10
11
 
11
- send_to_wiremock(url, request: request_builder, response: response_builder)
12
- end
12
+ response = send_to_wiremock(url, request: request_builder, response: response_builder)
13
+ JSON.parse(response.body).fetch('id')
14
+ end
13
15
 
14
- WIREMOCK_NEW_MAPPING_PATH = '__admin/mappings/new'.freeze
15
- private_constant :WIREMOCK_NEW_MAPPING_PATH
16
+ def delete_mapping(mapping_id, url = Configuration.wiremock_url)
17
+ delete_from_wiremock(url, mapping_id)
18
+ end
16
19
 
17
- def self.deep_clone(object)
18
- Marshal.load(Marshal.dump(object))
19
- end
20
- private_class_method :deep_clone
21
-
22
- def self.send_to_wiremock(url, body)
23
- uri = URI(URI.join(url, WIREMOCK_NEW_MAPPING_PATH))
24
- http = Net::HTTP.new(uri.host, uri.port)
25
- request = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
26
- request.body = body.to_json
27
- http.request(request)
20
+ def clear_mappings(url = Configuration.wiremock_url)
21
+ clear_wiremock_mappings(url)
22
+ end
23
+
24
+ private
25
+
26
+ WIREMOCK_MAPPINGS_PATH = '__admin/mappings'.freeze
27
+ WIREMOCK_CLEAR_MAPPINGS_PATH = "#{WIREMOCK_MAPPINGS_PATH}/reset".freeze
28
+
29
+ def deep_clone(object)
30
+ Marshal.load(Marshal.dump(object))
31
+ end
32
+
33
+ def clear_wiremock_mappings(url)
34
+ uri = URI([url, WIREMOCK_CLEAR_MAPPINGS_PATH].join('/'))
35
+ http = Net::HTTP.new(uri.host, uri.port)
36
+ request = Net::HTTP::Post.new(uri.path)
37
+ http.request(request)
38
+ end
39
+
40
+ def delete_from_wiremock(url, mapping_id)
41
+ uri = URI([url, WIREMOCK_MAPPINGS_PATH, mapping_id].join('/'))
42
+ http = Net::HTTP.new(uri.host, uri.port)
43
+ request = Net::HTTP::Delete.new(uri.path)
44
+ http.request(request)
45
+ end
46
+
47
+ def send_to_wiremock(url, body)
48
+ uri = URI([url, WIREMOCK_MAPPINGS_PATH].join('/'))
49
+ http = Net::HTTP.new(uri.host, uri.port)
50
+ request = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
51
+ request.body = body.to_json
52
+ http.request(request)
53
+ end
28
54
  end
29
- private_class_method :send_to_wiremock
30
55
  end
@@ -11,7 +11,9 @@ describe WireMockMapper do
11
11
  { 'matches' => 'some request body' }
12
12
  ] },
13
13
  response: { 'body' => 'some response body' } }
14
- stub_request(:post, "#{url}/__admin/mappings/new").with(body: expected_request_body)
14
+
15
+ stub = stub_request(:post, "#{url}/__admin/mappings").with(body: expected_request_body)
16
+ .to_return(body: { id: 'whatevs' }.to_json)
15
17
 
16
18
  WireMockMapper.create_mapping(url) do |request, respond|
17
19
  request.is_a_post
@@ -21,23 +23,27 @@ describe WireMockMapper do
21
23
 
22
24
  respond.with_body('some response body')
23
25
  end
26
+
27
+ expect(stub).to have_been_requested
24
28
  end
25
29
 
26
- # rubocop:disable Metrics/LineLength
27
30
  it 'posts the correct json with configured global mappings to the wiremock url' do
28
31
  url = 'http://nowhere.com'
29
32
  expected_request_body = { request: { 'method' => 'POST',
30
33
  'url' => '/some/url',
31
- 'headers' => { 'some_global_header' => { 'equalTo' => 'some global header value' },
34
+ 'headers' => { 'some_global_header' => { 'equalTo' => 'global value' },
32
35
  'some_header' => { 'equalTo' => 'some header value' } },
33
36
  'bodyPatterns' => [
34
37
  { 'matches' => 'some request body' }
35
38
  ] },
36
39
  response: { 'body' => 'some response body' } }
37
- stub_request(:post, "#{url}/__admin/mappings/new").with(body: expected_request_body)
38
40
 
39
- request_builder = WireMockMapper::Builders::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
+ stub = stub_request(:post, "#{url}/__admin/mappings").with(body: expected_request_body)
42
+ .to_return(body: { id: 'whatevs' }.to_json)
43
+
44
+ WireMockMapper::Configuration.create_global_mapping do |request|
45
+ request.with_header('some_global_header').equal_to('global value')
46
+ end
41
47
 
42
48
  WireMockMapper.create_mapping(url) do |request, respond|
43
49
  request.is_a_post
@@ -47,7 +53,29 @@ describe WireMockMapper do
47
53
 
48
54
  respond.with_body('some response body')
49
55
  end
56
+
57
+ expect(stub).to have_been_requested
58
+ end
59
+ end
60
+
61
+ describe 'delete_mapping' do
62
+ it 'issues a DELETE for the supplied mapping id' do
63
+ mapping_id = 'james-james-james'
64
+ url = 'http://nowhere.com'
65
+ stub = stub_request(:delete, "#{url}/__admin/mappings/#{mapping_id}")
66
+ WireMockMapper.delete_mapping(mapping_id, url)
67
+
68
+ expect(stub).to have_been_requested
69
+ end
70
+ end
71
+
72
+ describe 'clear_mappings' do
73
+ it 'POSTS to the wiremock __admin/mappings/reset path' do
74
+ url = 'http://nowhere.com'
75
+ stub = stub_request(:post, "#{url}/__admin/mappings/reset")
76
+ WireMockMapper.clear_mappings(url)
77
+
78
+ expect(stub).to have_been_requested
50
79
  end
51
- # rubocop:enable Metrics/LineLength
52
80
  end
53
81
  end
@@ -2,7 +2,7 @@ $LOAD_PATH.unshift(File.expand_path('../lib', __FILE__))
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'wiremock_mapper'
5
- spec.version = '1.0.1'
5
+ spec.version = '1.1.0'
6
6
  spec.platform = Gem::Platform::RUBY
7
7
  spec.required_ruby_version = '>= 1.9.3'
8
8
  spec.authors = ['Isaac Datlof']
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wiremock_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-02-09 00:00:00.000000000 Z
12
+ date: 2017-06-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: codeclimate-test-reporter