Sha256: ba4a98394bc31be3bb3c052ff9ceab930b4691b9345061c82e6ca85aca4149a4

Contents?: true

Size: 949 Bytes

Versions: 3

Compression:

Stored size: 949 Bytes

Contents

# frozen_string_literal: true

require 'rubygems'
require 'rack'

module Httplog
  module Test
    class Server
      def call(env)
        @root = __dir__
        path = Rack::Utils.unescape(env['PATH_INFO'])
        path += 'index.html' if path == '/'
        file = @root + path.to_s

        params = Rack::Utils.parse_nested_query(env['QUERY_STRING'])

        headers = { 'Content-Type' => 'text/html' }

        if params['redirect']
          [301, { 'Location' => '/index.html' }, '']

        elsif File.exist?(file)
          headers['Content-Type'] = 'application/octet-stream' if File.extname(file) == '.bin'
          headers['Content-Type'] = 'text/html; charset=UTF-8' if path =~ /utf8/
          headers['Content-Encoding'] = 'gzip' if File.extname(file) == '.gz'
          [200, headers, File.binread(file)]
        else
          [404, { 'Content-Type' => 'text/plain' }, 'file not found']
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
httplog-1.1.1 spec/support/test_server.rb
httplog-1.1.0 spec/support/test_server.rb
httplog-1.0.3 spec/support/test_server.rb