Sha256: 00af0ca125c12e046e00b010cec63dea42cab8a33772646acb58017b060e7b58

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# -*- encoding : utf-8 -*-
require 'webrick'
require 'forwardable'
require 'tempfile'

module Pacto
  module DummyServer
    class Servlet < WEBrick::HTTPServlet::AbstractServlet
      extend Forwardable

      def initialize(server, json)
        super(server)

        @doer = PlaybackServlet.new(
          status: 200,
          headers: { 'Content-Type' => 'application/json', 'Vary' => 'Accept' },
          body: json
        )
      end

      def_delegator :@doer, :do_GET # rubocop:disable SymbolName
    end

    class Dummy
      def initialize(port, path, response)
        log_file = File.exist?('/dev/null') ? '/dev/null' : Tempfile.new('log') # So tests run on Windows
        params = {
          Port: port,
          AccessLog: [],
          Logger: WEBrick::Log.new(log_file, 7)
        }
        @server = WEBrick::HTTPServer.new params
        @server.mount path, Servlet, response
      end

      def start
        @pid = Thread.new do
          trap 'INT' do
            @server.shutdown
          end
          @server.start
        end
      end

      def terminate
        @server.shutdown
        @pid.kill
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pacto-0.4.0.rc1 spec/pacto/dummy_server/dummy.rb