Sha256: 636e7072720767f6222f6b9100b68de4a2a7019338afe722662ddeacbeaf15ce

Contents?: true

Size: 1.98 KB

Versions: 10

Compression:

Stored size: 1.98 KB

Contents

# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
require_relative 'server_helper'

class TestRackHijack < Testcase
  ENV["N"].to_i > 1 and parallelize_me!
  include ServerHelper
  alias setup server_helper_setup
  alias teardown server_helper_teardown

  class DieIfUsed
    def each
      abort "body.each called after response hijack\n"
    end

    def close
      abort "body.close called after response hijack\n"
    end
  end

  HIJACK_APP = lambda { |env|
    case env["PATH_INFO"]
    when "/hijack_req"
      io = env["rack.hijack"].call
      if io.respond_to?(:read_nonblock) &&
         env["rack.hijack_io"].respond_to?(:read_nonblock)

        # exercise both, since we Rack::Lint may use different objects
        env["rack.hijack_io"].write("HTTP/1.0 200 OK\r\n\r\n")
        io.write("request.hijacked")
        io.close
        return [ 500, {}, DieIfUsed.new ]
      end
      [ 500, {}, [ "hijack BAD\n" ] ]
    when "/hijack_res"
      r = "response.hijacked"
      [ 200,
        {
          "X-Test" => "zzz",
          "Content-Length" => r.bytesize.to_s,
          "rack.hijack" => proc { |x| x.write(r); x.close }
        },
        DieIfUsed.new
      ]
    end
  }

  def test_hijack
    err = @err
    cfg = Yahns::Config.new
    host, port = @srv.addr[3], @srv.addr[1]
    cfg.instance_eval do
      GTL.synchronize { app(:rack, HIJACK_APP) { listen "#{host}:#{port}" } }
      logger(Logger.new(err.path))
    end
    pid = mkserver(cfg)
    res = Net::HTTP.start(host, port) { |h| h.get("/hijack_req") }
    assert_equal "request.hijacked", res.body
    assert_equal 200, res.code.to_i
    assert_equal "1.0", res.http_version

    res = Net::HTTP.start(host, port) { |h| h.get("/hijack_res") }
    assert_equal "response.hijacked", res.body
    assert_equal 200, res.code.to_i
    assert_equal "zzz", res["X-Test"]
    assert_equal "1.1", res.http_version
  ensure
    quit_wait(pid)
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
yahns-1.6.0 test/test_rack_hijack.rb
yahns-1.5.0 test/test_rack_hijack.rb
yahns-1.4.0 test/test_rack_hijack.rb
yahns-1.3.1 test/test_rack_hijack.rb
yahns-1.3.0 test/test_rack_hijack.rb
yahns-1.2.0 test/test_rack_hijack.rb
yahns-1.1.0 test/test_rack_hijack.rb
yahns-1.0.0 test/test_rack_hijack.rb
yahns-0.0.3 test/test_rack_hijack.rb
yahns-0.0.2 test/test_rack_hijack.rb