Skip to content

Commit

Permalink
added support for Etag and Last-Modified headers
Browse files Browse the repository at this point in the history
  • Loading branch information
rickenharp committed Dec 18, 2010
1 parent 98010f7 commit e5b2595
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
12 changes: 8 additions & 4 deletions lib/rack/gridfs.rb
Expand Up @@ -31,18 +31,22 @@ def initialize(app, options = {})
def call(env)
request = Rack::Request.new(env)
if request.path_info =~ /^\/#{prefix}\/(.+)$/
gridfs_request($1)
gridfs_request($1, request)
else
@app.call(env)
end
end

def gridfs_request(id)
def gridfs_request(id, request)
grid = Mongo::GridFileSystem.new(db)
file = grid.open(id, 'r')
[200, {'Content-Type' => file.content_type}, [file.read]]
if request.env['If-None-Match'] == file.files_id.to_s || request.env['If-Modified-Since'] == file.upload_date.httpdate
[304, {'Content-Type' => 'text/plain'}, ['Not modified']]
else
[200, {'Content-Type' => file.content_type, 'Last-Modified' => file.upload_date.httpdate, 'Etag' => file.files_id.to_s}, [file.read]]
end
rescue Mongo::GridError, BSON::InvalidObjectId
[404, {'Content-Type' => 'text/plain'}, ['File not found.']]
[404, {'Content-Type' => 'text/plain'}, ['File not found.' + id]]
rescue Mongo::GridFileNotFound
[404, {'Content-Type' => 'text/plain'}, ['File not found.']]
end
Expand Down
29 changes: 23 additions & 6 deletions test/gridfs_test.rb
@@ -1,5 +1,5 @@
require 'test_helper'

require 'pp'
class Rack::GridFSTest < Test::Unit::TestCase
include Rack::Test::Methods

Expand Down Expand Up @@ -103,22 +103,22 @@ def load_artifact(filename, content_type)
end

should "return TXT files stored in GridFS" do
get "/gridfs/#{@text_id}"
get "/gridfs/test.txt"
assert_equal "Lorem ipsum dolor sit amet.", last_response.body
end

should "return the proper content type for TXT files" do
get "/gridfs/#{@text_id}"
get "/gridfs/test.txt"
assert_equal 'text/plain', last_response.content_type
end

should "return HTML files stored in GridFS" do
get "/gridfs/#{@html_id}"
get "/gridfs/test.html"
assert_match /html.*?body.*Test/m, last_response.body
end

should "return the proper content type for HTML files" do
get "/gridfs/#{@html_id}"
get "/gridfs/test.html"
assert_equal 'text/html', last_response.content_type
end

Expand All @@ -129,9 +129,26 @@ def load_artifact(filename, content_type)

should "work for small images" do
image_id = load_artifact('3wolfmoon.jpg', 'image/jpeg')
get "/gridfs/#{image_id}"
gridfile = Mongo::Grid.new(db).get(image_id)
get "/gridfs/3wolfmoon.jpg"
assert last_response.ok?
assert_equal 'image/jpeg', last_response.content_type
assert_equal gridfile.upload_date.httpdate, last_response.headers["Last-Modified"]
assert_equal gridfile.files_id.to_s, last_response.headers["Etag"]
end

should "return 304 when Etag matches" do
image_id = load_artifact('3wolfmoon.jpg', 'image/jpeg')
gridfile = Mongo::Grid.new(db).get(image_id)
get "/gridfs/3wolfmoon.jpg", nil, {'If-None-Match' => gridfile.files_id.to_s}
assert_equal 304, last_response.status
end

should "return 304 when Last-Modified matches" do
image_id = load_artifact('3wolfmoon.jpg', 'image/jpeg')
gridfile = Mongo::Grid.new(db).get(image_id)
get "/gridfs/3wolfmoon.jpg", nil, {'If-Modified-Since' => gridfile.upload_date.httpdate}
assert_equal 304, last_response.status
end
end

Expand Down

0 comments on commit e5b2595

Please sign in to comment.