PHP 8.2.31
Preview: spec_chunked.rb Size: 3.87 KB
/proc/thread-self/root/opt/alt/ruby21/lib64/ruby/gems/2.1.0/gems/rack-1.6.4/test/spec_chunked.rb

require 'rack/chunked'
require 'rack/lint'
require 'rack/mock'

describe Rack::Chunked do
  def chunked(app)
    proc do |env|
      app = Rack::Chunked.new(app)
      response = Rack::Lint.new(app).call(env)
      # we want to use body like an array, but it only has #each
      response[2] = response[2].to_enum.to_a
      response
    end
  end

  before do
    @env = Rack::MockRequest.
      env_for('/', 'HTTP_VERSION' => '1.1', 'REQUEST_METHOD' => 'GET')
  end

  should 'chunk responses with no Content-Length' do
    app = lambda { |env| [200, {"Content-Type" => "text/plain"}, ['Hello', ' ', 'World!']] }
    response = Rack::MockResponse.new(*chunked(app).call(@env))
    response.headers.should.not.include 'Content-Length'
    response.headers['Transfer-Encoding'].should.equal 'chunked'
    response.body.should.equal "5\r\nHello\r\n1\r\n \r\n6\r\nWorld!\r\n0\r\n\r\n"
  end

  should 'chunks empty bodies properly' do
    app = lambda { |env| [200, {"Content-Type" => "text/plain"}, []] }
    response = Rack::MockResponse.new(*chunked(app).call(@env))
    response.headers.should.not.include 'Content-Length'
    response.headers['Transfer-Encoding'].should.equal 'chunked'
    response.body.should.equal "0\r\n\r\n"
  end

  should 'chunks encoded bodies properly' do
    body = ["\uFFFEHello", " ", "World"].map {|t| t.encode("UTF-16LE") }
    app  = lambda { |env| [200, {"Content-Type" => "text/plain"}, body] }
    response = Rack::MockResponse.new(*chunked(app).call(@env))
    response.headers.should.not.include 'Content-Length'
    response.headers['Transfer-Encoding'].should.equal 'chunked'
    response.body.encoding.to_s.should.equal "ASCII-8BIT"
    response.body.should.equal "c\r\n\xFE\xFFH\x00e\x00l\x00l\x00o\x00\r\n2\r\n \x00\r\na\r\nW\x00o\x00r\x00l\x00d\x00\r\n0\r\n\r\n".force_encoding("BINARY")
  end if RUBY_VERSION >= "1.9"

  should 'not modify response when Content-Length header present' do
    app = lambda { |env|
      [200, {"Content-Type" => "text/plain", 'Content-Length'=>'12'}, ['Hello', ' ', 'World!']]
    }
    status, headers, body = chunked(app).call(@env)
    status.should.equal 200
    headers.should.not.include 'Transfer-Encoding'
    headers.should.include 'Content-Length'
    body.join.should.equal 'Hello World!'
  end

  should 'not modify response when client is HTTP/1.0' do
    app = lambda { |env| [200, {"Content-Type" => "text/plain"}, ['Hello', ' ', 'World!']] }
    @env['HTTP_VERSION'] = 'HTTP/1.0'
    status, headers, body = chunked(app).call(@env)
    status.should.equal 200
    headers.should.not.include 'Transfer-Encoding'
    body.join.should.equal 'Hello World!'
  end

  should 'not modify response when client is ancient, pre-HTTP/1.0' do
    app = lambda { |env| [200, {"Content-Type" => "text/plain"}, ['Hello', ' ', 'World!']] }
    check = lambda do
      status, headers, body = chunked(app).call(@env.dup)
      status.should.equal 200
      headers.should.not.include 'Transfer-Encoding'
      body.join.should.equal 'Hello World!'
    end

    @env.delete('HTTP_VERSION') # unicorn will do this on pre-HTTP/1.0 requests
    check.call

    @env['HTTP_VERSION'] = 'HTTP/0.9' # not sure if this happens in practice
    check.call
  end

  should 'not modify response when Transfer-Encoding header already present' do
    app = lambda { |env|
      [200, {"Content-Type" => "text/plain", 'Transfer-Encoding' => 'identity'}, ['Hello', ' ', 'World!']]
    }
    status, headers, body = chunked(app).call(@env)
    status.should.equal 200
    headers['Transfer-Encoding'].should.equal 'identity'
    body.join.should.equal 'Hello World!'
  end

  [100, 204, 205, 304].each do |status_code|
    should "not modify response when status code is #{status_code}" do
      app = lambda { |env| [status_code, {}, []] }
      status, headers, _ = chunked(app).call(@env)
      status.should.equal status_code
      headers.should.not.include 'Transfer-Encoding'
    end
  end
end

Directory Contents

Dirs: 7 × Files: 51

Name Size Perms Modified Actions
builder DIR
- drwxr-xr-x 2024-03-03 22:43:09
Edit Download
cgi DIR
- drwxr-xr-x 2024-03-03 22:43:09
Edit Download
multipart DIR
- drwxr-xr-x 2024-03-03 22:43:09
Edit Download
rackup DIR
- drwxr-xr-x 2024-03-03 22:43:09
Edit Download
- drwxr-xr-x 2024-03-03 22:43:09
Edit Download
static DIR
- drwxr-xr-x 2024-03-03 22:43:09
Edit Download
- drwxr-xr-x 2024-03-03 22:43:09
Edit Download
298 B lrw-r--r-- 2019-12-02 01:00:44
Edit Download
2.26 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
8.08 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
2.20 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
6.20 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
2.11 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
2.92 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
3.87 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
2.37 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
3.28 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
544 B lrw-r--r-- 2019-12-02 01:00:44
Edit Download
2.80 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
1.47 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
10.04 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
2.19 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
3.84 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
3.08 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
6.32 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
1.87 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
1.36 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
19.23 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
1.23 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
4.33 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
622 B lrw-r--r-- 2019-12-02 01:00:44
Edit Download
2.38 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
1.81 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
9.34 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
5.73 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
23.62 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
514 B lrw-r--r-- 2019-12-02 01:00:44
Edit Download
1.83 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
42.52 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
10.08 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
2.78 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
1.53 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
4.12 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
5.57 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
1.29 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
12.94 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
11.12 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
6.53 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
2.01 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
2.74 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
4.60 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
1.57 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
2.55 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
8.82 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
24.89 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
504 B lrw-r--r-- 2019-12-02 01:00:44
Edit Download
5.50 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download
1.96 KB lrw-r--r-- 2019-12-02 01:00:44
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).