wiremock_mapper 1.0.1 → 1.1.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.
- data/Rakefile +1 -1
- data/lib/builders/helpers.rb +4 -2
- data/lib/builders/match_builder.rb +1 -1
- data/lib/wiremock_mapper.rb +45 -20
- data/spec/wiremock_mapper_spec.rb +35 -7
- data/wiremock_mapper.gemspec +1 -1
- metadata +2 -2
    
        data/Rakefile
    CHANGED
    
    
    
        data/lib/builders/helpers.rb
    CHANGED
    
    
| @@ -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
         | 
    
        data/lib/wiremock_mapper.rb
    CHANGED
    
    | @@ -2,29 +2,54 @@ require 'net/http' | |
| 2 2 | 
             
            require_relative 'configuration'
         | 
| 3 3 |  | 
| 4 4 | 
             
            module WireMockMapper
         | 
| 5 | 
            -
               | 
| 6 | 
            -
                 | 
| 7 | 
            -
             | 
| 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 | 
            -
             | 
| 10 | 
            +
                  yield request_builder, response_builder
         | 
| 10 11 |  | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 12 | 
            +
                  response = send_to_wiremock(url, request: request_builder, response: response_builder)
         | 
| 13 | 
            +
                  JSON.parse(response.body).fetch('id')
         | 
| 14 | 
            +
                end
         | 
| 13 15 |  | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            +
                def delete_mapping(mapping_id, url = Configuration.wiremock_url)
         | 
| 17 | 
            +
                  delete_from_wiremock(url, mapping_id)
         | 
| 18 | 
            +
                end
         | 
| 16 19 |  | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 23 | 
            -
                 | 
| 24 | 
            -
                 | 
| 25 | 
            -
             | 
| 26 | 
            -
                 | 
| 27 | 
            -
             | 
| 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 | 
            -
             | 
| 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' => ' | 
| 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 | 
            -
                   | 
| 40 | 
            -
             | 
| 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
         | 
    
        data/wiremock_mapper.gemspec
    CHANGED
    
    | @@ -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 | 
| 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 | 
| 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- | 
| 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
         |