rack.git  about / heads / tags
a modular Ruby webserver interface
blob 7496c032cb7b93ff59e5812ca1ac771b1ced4f76 8581 bytes (raw)
$ git show chunk:test/spec_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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
 
# frozen_string_literal: true

require_relative 'helper'
require 'zlib'

separate_testing do
  require_relative '../lib/rack/static'
  require_relative '../lib/rack/lint'
  require_relative '../lib/rack/mock_request'
end

class DummyApp
  def call(env)
    [200, { "content-type" => "text/plain" }, ["Hello World"]]
  end
end

describe Rack::Static do
  DOCROOT = File.expand_path(File.dirname(__FILE__)) unless defined? DOCROOT

  def static(app, *args)
    Rack::Lint.new Rack::Static.new(app, *args)
  end

  root = File.expand_path(File.dirname(__FILE__))

  OPTIONS = { urls: ["/cgi"], root: root }
  CASCADE_OPTIONS = { urls: ["/cgi"], root: root, cascade: true }
  STATIC_OPTIONS = { urls: [""], root: "#{root}/static", index: 'index.html' }
  STATIC_URLS_OPTIONS = { urls: ["/static"], root: "#{root}", index: 'index.html' }
  HASH_OPTIONS = { urls: { "/cgi/sekret" => 'cgi/test' }, root: root }
  HASH_ROOT_OPTIONS = { urls: { "/" => "static/foo.html" }, root: root }
  GZIP_OPTIONS = { urls: ["/cgi"], root: root, gzip: true }

  before do
  @request = Rack::MockRequest.new(static(DummyApp.new, OPTIONS))
  @cascade_request = Rack::MockRequest.new(static(DummyApp.new, CASCADE_OPTIONS))
  @static_request = Rack::MockRequest.new(static(DummyApp.new, STATIC_OPTIONS))
  @static_urls_request = Rack::MockRequest.new(static(DummyApp.new, STATIC_URLS_OPTIONS))
  @hash_request = Rack::MockRequest.new(static(DummyApp.new, HASH_OPTIONS))
  @hash_root_request = Rack::MockRequest.new(static(DummyApp.new, HASH_ROOT_OPTIONS))
  @gzip_request = Rack::MockRequest.new(static(DummyApp.new, GZIP_OPTIONS))
  @header_request = Rack::MockRequest.new(static(DummyApp.new, HEADER_OPTIONS))
  end

  it "serves files" do
    res = @request.get("/cgi/test")
    res.must_be :ok?
    res.body.must_match(/ruby/)
  end

  it "404s if url root is known but it can't find the file" do
    res = @request.get("/cgi/foo")
    res.must_be :not_found?
  end

  it "serves files when using :cascade option" do
    res = @cascade_request.get("/cgi/test")
    res.must_be :ok?
    res.body.must_match(/ruby/)
  end

  it "calls down the chain if if can't find the file when using the :cascade option" do
    res = @cascade_request.get("/cgi/foo")
    res.must_be :ok?
    res.body.must_equal "Hello World"
  end

  it "calls down the chain if url root is not known" do
    res = @request.get("/something/else")
    res.must_be :ok?
    res.body.must_equal "Hello World"
  end

  it "calls index file when requesting root in the given folder" do
    res = @static_request.get("/")
    res.must_be :ok?
    res.body.must_match(/index!/)

    res = @static_request.get("/other/")
    res.must_be :not_found?

    res = @static_request.get("/another/")
    res.must_be :ok?
    res.body.must_match(/another index!/)
  end

  it "does not call index file when requesting folder with unknown prefix" do
    res = @static_urls_request.get("/static/another/")
    res.must_be :ok?
    res.body.must_match(/index!/)

    res = @static_urls_request.get("/something/else/")
    res.must_be :ok?
    res.body.must_equal "Hello World"
  end

  it "doesn't call index file if :index option was omitted" do
    res = @request.get("/")
    res.body.must_equal "Hello World"
  end

  it "serves hidden files" do
    res = @hash_request.get("/cgi/sekret")
    res.must_be :ok?
    res.body.must_match(/ruby/)
  end

  it "calls down the chain if the URI is not specified" do
    res = @hash_request.get("/something/else")
    res.must_be :ok?
    res.body.must_equal "Hello World"
  end

  it "allows the root URI to be configured via hash options" do
    res = @hash_root_request.get("/")
    res.must_be :ok?
    res.body.must_match(/foo.html!/)
  end

  it "serves gzipped files if client accepts gzip encoding and gzip files are present" do
    res = @gzip_request.get("/cgi/test", 'HTTP_ACCEPT_ENCODING' => 'deflate, gzip')
    res.must_be :ok?
    res.headers['content-encoding'].must_equal 'gzip'
    res.headers['content-type'].must_equal 'text/plain'
    Zlib::GzipReader.wrap(StringIO.new(res.body), &:read).must_match(/ruby/)
  end

  it "serves regular files if client accepts gzip encoding and gzip files are not present" do
    res = @gzip_request.get("/cgi/rackup_stub.rb", 'HTTP_ACCEPT_ENCODING' => 'deflate, gzip')
    res.must_be :ok?
    res.headers['content-encoding'].must_be_nil
    res.headers['content-type'].must_equal 'text/x-script.ruby'
    res.body.must_match(/ruby/)
  end

  it "serves regular files if client does not accept gzip encoding" do
    res = @gzip_request.get("/cgi/test")
    res.must_be :ok?
    res.headers['content-encoding'].must_be_nil
    res.headers['content-type'].must_equal 'text/plain'
    res.body.must_match(/ruby/)
  end

  it "returns 304 if gzipped file isn't modified since last serve" do
    path = File.join(DOCROOT, "/cgi/test")
    res = @gzip_request.get("/cgi/test", 'HTTP_IF_MODIFIED_SINCE' => File.mtime(path).httpdate)
    res.status.must_equal 304
    res.body.must_be :empty?
    res.headers['content-encoding'].must_be_nil
    res.headers['content-type'].must_be_nil
  end

  it "return 304 if gzipped file isn't modified since last serve" do
    path = File.join(DOCROOT, "/cgi/test")
    res = @gzip_request.get("/cgi/test", 'HTTP_IF_MODIFIED_SINCE' => File.mtime(path+'.gz').httpdate, 'HTTP_ACCEPT_ENCODING' => 'deflate, gzip')

    res.status.must_equal 304
    res.body.must_be :empty?
  end

  it "supports serving fixed cache-control (legacy option)" do
    opts = OPTIONS.merge(cache_control: 'public')
    request = Rack::MockRequest.new(static(DummyApp.new, opts))
    res = request.get("/cgi/test")
    res.must_be :ok?
    res.headers['cache-control'].must_equal 'public'
  end

  HEADER_OPTIONS = { urls: ["/cgi"], root: root, header_rules: [
    [:all, { 'cache-control' => 'public, max-age=100' }],
    [:fonts, { 'cache-control' => 'public, max-age=200' }],
    [%w(png jpg), { 'cache-control' => 'public, max-age=300' }],
    ['/cgi/assets/folder/', { 'cache-control' => 'public, max-age=400' }],
    ['cgi/assets/javascripts', { 'cache-control' => 'public, max-age=500' }],
    [/\.(css|erb)\z/, { 'cache-control' => 'public, max-age=600' }],
    [false, { 'cache-control' => 'public, max-age=600' }]
  ] }

  it "supports header rule :all" do
    # Headers for all files via :all shortcut
    res = @header_request.get('/cgi/assets/index.html')
    res.must_be :ok?
    res.headers['cache-control'].must_equal 'public, max-age=100'
  end

  it "supports header rule :fonts" do
    # Headers for web fonts via :fonts shortcut
    res = @header_request.get('/cgi/assets/fonts/font.eot')
    res.must_be :ok?
    res.headers['cache-control'].must_equal 'public, max-age=200'
  end

  it "supports file extension header rules provided as an Array" do
    # Headers for file extensions via array
    res = @header_request.get('/cgi/assets/images/image.png')
    res.must_be :ok?
    res.headers['cache-control'].must_equal 'public, max-age=300'
  end

  it "supports folder rules provided as a String" do
    # Headers for files in folder via string
    res = @header_request.get('/cgi/assets/folder/test.js')
    res.must_be :ok?
    res.headers['cache-control'].must_equal 'public, max-age=400'
  end

  it "supports folder header rules provided as a String not starting with a slash" do
    res = @header_request.get('/cgi/assets/javascripts/app.js')
    res.must_be :ok?
    res.headers['cache-control'].must_equal 'public, max-age=500'
  end

  it "supports flexible header rules provided as Regexp" do
    # Flexible Headers via Regexp
    res = @header_request.get('/cgi/assets/stylesheets/app.css')
    res.must_be :ok?
    res.headers['cache-control'].must_equal 'public, max-age=600'
  end

  it "prioritizes header rules over fixed cache-control setting (legacy option)" do
    opts = OPTIONS.merge(
      cache_control: 'public, max-age=24',
      header_rules: [
        [:all, { 'cache-control' => 'public, max-age=42' }]
      ])

    request = Rack::MockRequest.new(static(DummyApp.new, opts))
    res = request.get("/cgi/test")
    res.must_be :ok?
    res.headers['cache-control'].must_equal 'public, max-age=42'
  end

  it "expands the root path upon the middleware initialization" do
    relative_path = STATIC_OPTIONS[:root].sub("#{Dir.pwd}/", '')
    opts = { urls: [""], root: relative_path, index: 'index.html' }
    request = Rack::MockRequest.new(static(DummyApp.new, opts))
    Dir.chdir '..' do
      res = request.get("")
      res.must_be :ok?
      res.body.must_match(/index!/)
    end
  end
end

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