summaryrefslogtreecommitdiff
path: root/spec/bundler/support/artifice/helpers/artifice.rb
blob: 788268295c7d94475e673e2fab46206167b27bee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true

# This module was initially borrowed from https://github.com/wycats/artifice
module Artifice
  # Activate Artifice with a particular Rack endpoint.
  #
  # Calling this method will replace the Gem::Net::HTTP system
  # with a replacement that routes all requests to the
  # Rack endpoint.
  #
  # @param [#call] endpoint A valid Rack endpoint
  def self.activate_with(endpoint)
    require_relative "rack_request"

    Net::HTTP.endpoint = endpoint
    replace_net_http(Artifice::Net::HTTP)
  end

  # Deactivate the Artifice replacement.
  def self.deactivate
    replace_net_http(::Gem::Net::HTTP)
  end

  def self.replace_net_http(value)
    ::Gem::Net.class_eval do
      remove_const(:HTTP)
      const_set(:HTTP, value)
    end
  end
end