unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
blob a9510578d755dc388281bd65a865093aed16dbc7 5524 bytes (raw)
name: test/unit/test_request.rb 	 # note: path name is non-authoritative(*)

  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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
 
# -*- encoding: binary -*-

# Copyright (c) 2009 Eric Wong
# You can redistribute it and/or modify it under the same terms as Ruby 1.8 or
# the GPLv2+ (GPLv3+ preferred)

require './test/test_helper'

include Unicorn

class RequestTest < Test::Unit::TestCase

  class MockRequest < StringIO
    def remote_address
      Addrinfo.tcp('127.0.0.1', 55608)
    end
  end

  def setup
    @request = HttpRequest.new
    @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")
    env = @request.read(client)
    assert_equal '', env['REQUEST_PATH']
    assert_equal '', env['PATH_INFO']
    assert_equal '*', env['REQUEST_URI']
    assert_kind_of Array, @lint.call(env)
  end

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

  def test_absolute_uri_with_fragment
    client = MockRequest.new("GET http://e:3/x#frag HTTP/1.1\r\n" \
                             "Host: foo\r\n\r\n")
    env = @request.read(client)
    assert_equal '/x', env['REQUEST_PATH']
    assert_equal '/x', env['PATH_INFO']
    assert_equal '', env['QUERY_STRING']
    assert_equal 'frag', env['FRAGMENT']
    assert_kind_of Array, @lint.call(env)
  end

  def test_absolute_uri_with_query_and_fragment
    client = MockRequest.new("GET http://e:3/x?a=b#frag HTTP/1.1\r\n" \
                             "Host: foo\r\n\r\n")
    env = @request.read(client)
    assert_equal '/x', env['REQUEST_PATH']
    assert_equal '/x', env['PATH_INFO']
    assert_equal 'a=b', env['QUERY_STRING']
    assert_equal 'frag', env['FRAGMENT']
    assert_kind_of Array, @lint.call(env)
  end

  def test_absolute_uri_unsupported_schemes
    %w(ssh+http://e/ ftp://e/x http+ssh://e/x).each do |abs_uri|
      client = MockRequest.new("GET #{abs_uri} HTTP/1.1\r\n" \
                               "Host: foo\r\n\r\n")
      assert_raises(HttpParserError) { @request.read(client) }
    end
  end

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

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

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

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

  def test_no_content_stringio
    client = MockRequest.new("GET / HTTP/1.1\r\nHost: foo\r\n\r\n")
    env = @request.read(client)
    assert_equal StringIO, env['rack.input'].class
  end

  def test_zero_content_stringio
    client = MockRequest.new("PUT / HTTP/1.1\r\n" \
                             "Content-Length: 0\r\n" \
                             "Host: foo\r\n\r\n")
    env = @request.read(client)
    assert_equal StringIO, env['rack.input'].class
  end

  def test_real_content_not_stringio
    client = MockRequest.new("PUT / HTTP/1.1\r\n" \
                             "Content-Length: 1\r\n" \
                             "Host: foo\r\n\r\n")
    env = @request.read(client)
    assert_equal Unicorn::TeeInput, env['rack.input'].class
  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")
    env = @request.read(client)
    assert ! env.include?(:http_body)
    assert_kind_of Array, @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.remote_address; Addrinfo.tcp('127.0.0.1', 55608); 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)
    env = @request.read(client)
    assert ! env.include?(:http_body)
    assert_equal length, env['rack.input'].size
    count.times {
      tmp = env['rack.input'].read(bs)
      tmp << env['rack.input'].read(bs - tmp.size) if tmp.size != bs
      assert_equal buf, tmp
    }
    assert_nil env['rack.input'].read(bs)
    env['rack.input'].rewind
    assert_kind_of Array, @lint.call(env)
  end
end

debug log:

solving a951057 ...
found a951057 in https://yhbt.net/unicorn-public/2c3b457c-7525-44ca-8bdb-493f5a785dad@shopify.com/
found 6cb0268 in https://yhbt.net/unicorn.git/
preparing index
index prepared:
100644 6cb02683ec0564c35f1e000d77803d936eb5755d	test/unit/test_request.rb

applying [1/1] https://yhbt.net/unicorn-public/2c3b457c-7525-44ca-8bdb-493f5a785dad@shopify.com/
diff --git a/test/unit/test_request.rb b/test/unit/test_request.rb
index 6cb0268..a951057 100644

Checking patch test/unit/test_request.rb...
error: while searching for:
 class RequestTest < Test::Unit::TestCase

   class MockRequest < StringIO
    alias_method :readpartial, :sysread
    alias_method :kgio_read!, :sysread
    alias_method :read_nonblock, :sysread
    def kgio_addr
      '127.0.0.1'
     end
   end


error: patch failed: test/unit/test_request.rb:11
error: test/unit/test_request.rb: patch does not apply

git apply error:; exit status=1
trying https://yhbt.net/unicorn-public/2c3b457c-7525-44ca-8bdb-493f5a785dad@shopify.com/

applying [2/1] https://yhbt.net/unicorn-public/2c3b457c-7525-44ca-8bdb-493f5a785dad@shopify.com/
diff --git a/test/unit/test_request.rb b/test/unit/test_request.rb
index 6cb0268..a951057 100644

Checking patch test/unit/test_request.rb...
Applied patch test/unit/test_request.rb cleanly.

index at:
100644 a9510578d755dc388281bd65a865093aed16dbc7	test/unit/test_request.rb

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).