Sha256: 226f46fbe7240a498a68e45428bf85cdda3dd7adb610016c40d56fc94f8c6a7b

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

require 'webmock'
require 'singleton'
require 'set'

module WebValve
  # @api private
  class Manager
    include Singleton

    def register(fake_service, **args)
      raise "#{fake_service.inspect} already registered" if fake_service_configs.any? { |c| c.service == fake_service }
      fake_service_configs << FakeServiceConfig.new(service: fake_service, **args)
    end

    def whitelist_url(url)
      raise "#{url} already registered" if whitelisted_urls.include?(url)
      whitelisted_urls << url
    end

    def setup
      fake_service_configs.each do |config|
        if config.should_intercept?
          webmock_service config
        else
          whitelist_service config
        end
      end

      WebMock.enable!
      WebMock.disable_net_connect! webmock_disable_options
    end

    # @api private
    def reset
      whitelisted_urls.clear
      fake_service_configs.clear
    end

    # @api private
    def fake_service_configs
      @fake_service_configs ||= []
    end

    # @api private
    def whitelisted_urls
      @whitelisted_urls ||= Set.new
    end

    private

    def webmock_disable_options
      { allow_localhost: true }.tap do |opts|
        opts[:allow] = whitelisted_url_regexps unless Rails.env.test?
      end
    end

    def whitelisted_url_regexps
      whitelisted_urls.map { |url| url_to_regexp url }
    end

    def webmock_service(config)
      WebMock.stub_request(
        :any,
        url_to_regexp(config.service_url)
      ).to_rack(FakeServiceWrapper.new(config.service))
    end

    def whitelist_service(config)
      whitelisted_urls << config.service_url
    end

    def url_to_regexp(url)
      %r(\A#{Regexp.escape url})
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
webvalve-0.9.3 lib/webvalve/manager.rb
webvalve-0.9.2 lib/webvalve/manager.rb
webvalve-0.9.1 lib/webvalve/manager.rb
webvalve-0.9.0 lib/webvalve/manager.rb