yahns.git  about / heads / tags
sleepy, multi-threaded, non-blocking application server for Ruby
blob c6c8cef48a0e07aa3a0be2e817350e939e1192d3 5820 bytes (raw)
$ git show maint:test/test_extras_try_gzip_static.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
 
# Copyright (C) 2013-2016 all contributors <yahns-public@yhbt.net>
# License: GPL-3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
# frozen_string_literal: true
require_relative 'server_helper'
require 'zlib'
require 'time'

class TestExtrasTryGzipStatic < Testcase
  ENV["N"].to_i > 1 and parallelize_me!
  include ServerHelper
  GPL_TEXT = IO.binread("COPYING").freeze

  def setup
    @tmpdir = yahns_mktmpdir
    server_helper_setup
    skip 'Ruby 2.x required' unless ''.respond_to?(:b)
  end

  def teardown
    server_helper_teardown
    FileUtils.rm_rf @tmpdir
  end

  def test_gzip_static
    err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1]
    tmpdir = @tmpdir
    pid = mkserver(cfg) do
      require './extras/try_gzip_static'
      cfg.instance_eval do
        app(:rack, TryGzipStatic.new(tmpdir)) do
          listen "#{host}:#{port}"
        end
        stderr_path err.path
      end
    end

    begin # setup
      gpl = "#{tmpdir}/COPYING"
      gplgz = "#{tmpdir}/COPYING.gz"
      FileUtils.cp("COPYING", gpl)
      _, status = Process.waitpid2(fork do
        File.open(gplgz, "w") do |fp|
          Zlib::GzipWriter.wrap(fp.dup) { |io| io.write(GPL_TEXT) }
        end
        exit!(0)
      end)
      assert status.success?, status.inspect
      st = File.stat(gpl)
      gz_st = File.stat(gplgz)
      assert_equal GPL_TEXT, `zcat #{gplgz}`, "Eric screwed up using zlib"
      File.utime(st.atime, st.mtime, gplgz)
    end

    check = lambda do |req, &blk|
      c = get_tcp_client(host, port)
      begin
        c.write "#{req}\r\n\r\n"
        buf = c.read(666000)
        head, body = buf.split(/\r\n\r\n/)
        blk.call(head)
        body
      ensure
        c.close
      end
    end

    Timeout.timeout(30) do # basic tests
      %w(GET HEAD).each do |m|
        body = check.call("#{m} /COPYING HTTP/1.0") do |head|
          refute_match %r{^Content-Encoding: gzip\b}, head
          assert_match %r{^Content-Type: text/plain\b}, head
          assert_match %r{^Content-Length: #{st.size}\b}, head
        end
        case m
        when "GET" then assert_equal GPL_TEXT, body
        when "HEAD" then assert_nil body
        end

        req = "#{m} /COPYING HTTP/1.0\r\nAccept-Encoding: gzip"
        body = check.call(req) do |head|
          assert_match %r{^Content-Encoding: gzip\b}, head
          assert_match %r{^Content-Type: text/plain\b}, head
          assert_match %r{^Content-Length: #{gz_st.size}\b}, head
        end
        case m
        when "GET"
          assert_equal GPL_TEXT, Zlib::GzipReader.new(StringIO.new(body)).read
        when "HEAD" then assert_nil body
        end
      end
    end

    Timeout.timeout(30) do # range tests
      %w(HEAD GET).each do |m|
        req = "#{m} /COPYING HTTP/1.0\r\n" \
              "Range: bytes=5-46\r\nAccept-Encoding: gzip"
        body = check.call(req) do |head|
          assert_match %r{\AHTTP/1\.1 206 Partial Content\r\n}, head
          refute_match %r{^Content-Encoding: gzip\b}, head
          assert_match %r{^Content-Type: text/plain\b}, head
          assert_match %r{^Content-Length: 42\b}, head
          assert_match %r{^Content-Range: bytes 5-46/#{st.size}\r\n}, head
        end
        case m
        when "GET" then assert_equal GPL_TEXT[5..46], body
        when "HEAD" then assert_nil body
        end

        req = "#{m} /COPYING HTTP/1.0\r\n" \
              "Range: bytes=66666666-\r\nAccept-Encoding: gzip"
        body = check.call(req) do |head|
          assert_match %r{^Content-Range: bytes \*/#{st.size}\r\n}, head
          assert_match %r{\AHTTP/1\.1 416 }, head
        end
        assert_nil body
      end
    end

    Timeout.timeout(30) do # gzip counterpart is nonexistent
      File.link(gpl, "#{gpl}.hardlink")
      %w(GET HEAD).each do |m|
        req = "#{m} /COPYING.hardlink HTTP/1.0\r\nAccept-Encoding: gzip"
        body = check.call(req) do |head|
          refute_match %r{^Content-Encoding: gzip\b}, head
          assert_match %r{^Content-Type: text/plain\b}, head
          assert_match %r{^Content-Length: #{st.size}\b}, head
        end
        case m
        when "GET" then assert_equal GPL_TEXT, body
        when "HEAD" then assert_nil body
        end
      end
    end

    Timeout.timeout(30) do # If-Modified-Since
      %w(GET HEAD).each do |m|
        req = "#{m} /COPYING HTTP/1.0\r\n" \
              "If-Modified-Since: #{st.mtime.httpdate}"
        body = check.call(req) do |head|
          assert_match %r{\AHTTP/1\.1 304 Not Modified}, head
        end
        assert_nil body
      end
    end

    # skew the times of the gzip file, should now fail to use gzipped
    Timeout.timeout(30) do
      File.utime(Time.at(0), Time.at(0), gplgz)

      %w(GET HEAD).each do |m|
        req = "#{m} /COPYING HTTP/1.0\r\nAccept-Encoding: gzip"
        body = check.call(req) do |head|
          refute_match %r{^Content-Encoding: gzip\b}, head
          assert_match %r{^Content-Type: text/plain\b}, head
          assert_match %r{^Content-Length: #{st.size}\b}, head
        end
        case m
        when "GET" then assert_equal GPL_TEXT, body
        when "HEAD" then assert_nil body
        end
      end
    end

    Timeout.timeout(30) do # 404
      %w(GET HEAD).each do |m|
        req = "#{m} /cp-ing HTTP/1.0\r\nAccept-Encoding: gzip"
        body = check.call(req) do |head|
          assert_match %r{HTTP/1\.1 404 }, head
        end
        assert_nil body
      end
      body = check.call("FOO /COPYING HTTP/1.0") do |head|
        assert_match %r{HTTP/1\.1 405 }, head
      end
      assert_nil body
    end

    Net::HTTP.start(host, port) do |http|
      res = http.request(Net::HTTP::Get.new('/COPYING/foo'))
      assert_equal 404, res.code.to_i
    end
  ensure
    quit_wait(pid)
  end
end

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