unicorn.git  about / heads / tags
Rack HTTP server for Unix and fast clients
blob 06133264399d259b60a4a5aec0ee76d1d7a2d287 4134 bytes (raw)
$ git show v0.5.2:test/unit/test_request.rb	# shows this blob on the CLI

  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
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
 
# Copyright (c) 2009 Eric Wong
# You can redistribute it and/or modify it under the same terms as Ruby.

if RUBY_VERSION =~ /1\.9/
  warn "#$0 current broken under Ruby 1.9 with Rack"
  exit 0
end

require 'test/test_helper'
begin
  require 'rack'
  require 'rack/lint'
rescue LoadError
  warn "Unable to load rack, skipping test"
  exit 0
end

include Unicorn

class RequestTest < Test::Unit::TestCase

  class MockRequest < StringIO
    def unicorn_peeraddr
      '666.666.666.666'
    end
  end

  def setup
    @request = HttpRequest.new(Logger.new($stderr))
    @app = lambda do |env|
      [ 200, { 'Content-Length' => '0', 'Content-Type' => 'text/plain' }, [] ]
    end
    @lint = Rack::Lint.new(@app)
  end

  def test_options
    client = MockRequest.new("OPTIONS * HTTP/1.1\r\n" \
                             "Host: foo\r\n\r\n")
    res = env = nil
    assert_nothing_raised { env = @request.read(client) }
    assert_equal '*', env['REQUEST_PATH']
    assert_equal '*', env['PATH_INFO']
    assert_equal '*', env['REQUEST_URI']

    # assert_nothing_raised { res = @lint.call(env) } # fails Rack lint
  end

  def test_full_url_path
    client = MockRequest.new("GET http://e:3/x?y=z HTTP/1.1\r\n" \
                             "Host: foo\r\n\r\n")
    res = env = nil
    assert_nothing_raised { env = @request.read(client) }
    assert_equal '/x', env['REQUEST_PATH']
    assert_equal '/x', env['PATH_INFO']
    assert_nothing_raised { res = @lint.call(env) }
  end

  def test_x_forwarded_proto_https
    res = env = nil
    client = MockRequest.new("GET / HTTP/1.1\r\n" \
                             "X-Forwarded-Proto: https\r\n" \
                             "Host: foo\r\n\r\n")
    assert_nothing_raised { env = @request.read(client) }
    assert_equal "https", env['rack.url_scheme']
    assert_nothing_raised { res = @lint.call(env) }
  end

  def test_x_forwarded_proto_http
    res = env = nil
    client = MockRequest.new("GET / HTTP/1.1\r\n" \
                             "X-Forwarded-Proto: http\r\n" \
                             "Host: foo\r\n\r\n")
    assert_nothing_raised { env = @request.read(client) }
    assert_equal "http", env['rack.url_scheme']
    assert_nothing_raised { res = @lint.call(env) }
  end

  def test_x_forwarded_proto_invalid
    res = env = nil
    client = MockRequest.new("GET / HTTP/1.1\r\n" \
                             "X-Forwarded-Proto: ftp\r\n" \
                             "Host: foo\r\n\r\n")
    assert_nothing_raised { env = @request.read(client) }
    assert_equal "http", env['rack.url_scheme']
    assert_nothing_raised { res = @lint.call(env) }
  end

  def test_rack_lint_get
    client = MockRequest.new("GET / HTTP/1.1\r\nHost: foo\r\n\r\n")
    res = env = nil
    assert_nothing_raised { env = @request.read(client) }
    assert_equal "http", env['rack.url_scheme']
    assert_equal '666.666.666.666', env['REMOTE_ADDR']
    assert_nothing_raised { res = @lint.call(env) }
  end

  def test_rack_lint_put
    client = MockRequest.new(
      "PUT / HTTP/1.1\r\n" \
      "Host: foo\r\n" \
      "Content-Length: 5\r\n" \
      "\r\n" \
      "abcde")
    res = env = nil
    assert_nothing_raised { env = @request.read(client) }
    assert ! env.include?(:http_body)
    assert_nothing_raised { res = @lint.call(env) }
  end

  def test_rack_lint_big_put
    count = 100
    bs = 0x10000
    buf = (' ' * bs).freeze
    length = bs * count
    client = Tempfile.new('big_put')
    def client.unicorn_peeraddr
      '1.1.1.1'
    end
    client.syswrite(
      "PUT / HTTP/1.1\r\n" \
      "Host: foo\r\n" \
      "Content-Length: #{length}\r\n" \
      "\r\n")
    count.times { assert_equal bs, client.syswrite(buf) }
    assert_equal 0, client.sysseek(0)
    res = env = nil
    assert_nothing_raised { env = @request.read(client) }
    assert ! env.include?(:http_body)
    assert_equal length, env['rack.input'].size
    count.times { assert_equal buf, env['rack.input'].read(bs) }
    assert_nil env['rack.input'].read(bs)
    assert_nothing_raised { env['rack.input'].rewind }
    assert_nothing_raised { res = @lint.call(env) }
  end

end


git clone https://yhbt.net/unicorn.git