Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Switched test coverage over to mocha from fakeweb because fakeweb doe…

…sn't support patron http lib
  • Loading branch information...
commit 3c2f8ae7ac0ec22ee5f94ec1f559dad31e8ea4f2 1 parent 3fc0483
Tobias Lütke authored
8 lib/image.rb
View
@@ -14,14 +14,6 @@ def initialize(server, path)
download(path)
end
- def content_type
- headers['Content-Type']
- end
-
- def cache_control
- headers['Cache-Control']
- end
-
def found?
@status == 200
end
21 lib/send_file.rb
View
@@ -1,4 +1,9 @@
+require 'time'
+
module SendFile
+
+ CopyHeaders = ['Content-Type', 'Cache-Control', 'Last-Modified', 'ETag']
+
ContentTypes = {
'.gif' => 'image/gif',
'.jpg' => 'image/jpeg',
@@ -8,11 +13,19 @@ module SendFile
'.svg' => 'image/svg+xml'
}
- def send_file(file)
+ def send_file(file)
+ headers = {'Content-Length' => file.content.length}
- cache_control = file.respond_to?(:cache_control) ? file.cache_control : 'public, max-age: 31557600'
- content_type = file.respond_to?(:content_type) ? file.content_type || ContentTypes[file.ext] || raise(ArgumentError, 'illegal content type') : 'application/octet-stream'
+ if file.respond_to?(:headers)
+ CopyHeaders.each do |key|
+ headers[key] = file.headers[key] if file.headers.has_key?(key)
+ end
+ end
- [200, {'Content-Type' => content_type, 'Cache-Control' => cache_control, 'Content-Length' => file.content.length}, [file.content]]
+ headers['ETag'] ||= Digest::MD5.hexdigest(file.content)
+ headers['Cache-Control'] ||= 'public, max-age: 31557600'
+ headers['Last-Modified'] ||= Time.new.httpdate
+
+ [200, headers, [file.content]]
end
end
13 test/test_image_variant_generator.rb
View
@@ -3,6 +3,7 @@
require "test/unit"
require 'rubygems'
require 'fakeweb'
+require 'mocha'
require 'rack'
require File.dirname(__FILE__) + '/../image_server'
@@ -12,6 +13,18 @@
class TestRemoteProxy < Test::Unit::TestCase
+ def setup
+ @headers = {'Content-Type' => "image/png", 'Cache-Control' => 'public, max-age=0', 'ETag' => 'abc', 'Last-Modified' => "Mon, 24 Aug 2009 18:07:15 GMT"}
+
+ Patron::Session.any_instance.stubs(:get).with('/image.png').returns(
+ stub(:headers => @headers, :body => File.read( File.dirname(__FILE__) + '/assets/fish.png'), :status => 200)
+ )
+
+ Patron::Session.any_instance.stubs(:get).with('/failed_image.png').returns(
+ stub(:headers => {}, :status => 404)
+ )
+ end
+
def test_successfull_call
assert ImageVariantGenerator.from_url('static.shopify.com', '/image_pico.png')
assert ImageVariantGenerator.from_url('static.shopify.com', '/image_small.png')
30 test/test_remote_proxy.rb
View
@@ -2,22 +2,38 @@
require "test/unit"
require 'rubygems'
-require 'fakeweb'
require 'rack'
-require File.dirname(__FILE__) + '/../image_server'
+require 'mocha'
-FakeWeb.allow_net_connect = false
-FakeWeb.register_uri(:get, "http://static.shopify.com/test.txt", :body => "Hello World!", :content_type => "text/plain", :cache_control => 'public, max-age=0')
-FakeWeb.register_uri(:get, "http://static.shopify.com/test2.txt", :status => 404)
-FakeWeb.register_uri(:get, "http://static.shopify.com/test3.txt?abc", :body => "Hello World!", :content_type => "text/plain", :cache_control => 'public, max-age=0')
+require File.dirname(__FILE__) + '/../image_server'
+# FakeWeb.allow_net_connect = false
+# FakeWeb.register_uri(:get, "http://static.shopify.com/test.txt", :body => "Hello World!", :content_type => "text/plain", :cache_control => 'public, max-age=0')
+# FakeWeb.register_uri(:get, "http://static.shopify.com/test2.txt", :status => 404)
+# FakeWeb.register_uri(:get, "http://static.shopify.com/test3.txt?abc", :body => "Hello World!", :content_type => "text/plain", :cache_control => 'public, max-age=0')
+#
class TestRemoteProxy < Test::Unit::TestCase
StandardResponse = [200, {}, ['OK']]
ExpectedResponse = [200,
- {"Cache-Control"=>"public, max-age=0", "Content-Type"=>"text/plain"},
+ {"Cache-Control"=>"public, max-age=0", "Content-Type"=>"text/plain", "ETag"=>"abc", "Content-Length"=>12, 'Last-Modified' => "Mon, 24 Aug 2009 18:07:15 GMT"},
["Hello World!"]]
def setup
+ @headers = {'Content-Type' => "text/plain", 'Cache-Control' => 'public, max-age=0', 'ETag' => 'abc', 'Last-Modified' => "Mon, 24 Aug 2009 18:07:15 GMT"}
+
+ Patron::Session.any_instance.stubs(:get).with('/test.txt').returns(
+ stub(:headers => @headers, :body => 'Hello World!', :status => 200)
+ )
+
+ Patron::Session.any_instance.stubs(:get).with('/test2.txt').returns(
+ stub(:headers => {}, :status => 404)
+ )
+
+ Patron::Session.any_instance.stubs(:get).with('/test3.txt?abc').returns(
+ stub(:headers => @headers, :body => 'Hello World!', :status => 200)
+ )
+
+
@app = RemoteProxy.new lambda { StandardResponse }
end
12 test/test_svg_generation.rb
View
@@ -2,24 +2,26 @@
require "test/unit"
require 'rubygems'
-require 'fakeweb'
+require 'mocha'
require 'rack'
require File.dirname(__FILE__) + '/../image_server'
-FakeWeb.allow_net_connect = false
-FakeWeb.register_uri(:get, "http://static.shopify.com/image.svg", :body => File.read( File.dirname(__FILE__) + '/assets/fish.svg'), :content_type => "image/svg+xml", :cache_control => 'public, max-age=0')
-FakeWeb.register_uri(:get, "http://static.shopify.com/image.svg.png", :status => 404)
class TestRemoteProxy < Test::Unit::TestCase
StandardResponse = [200, {}, ['OK']]
ExpectedResponse = [200, {"Cache-Control"=>"public, max-age=0", "Content-Type"=>"text/plain"}, ["Hello World!"]]
-
+ def setup
+ end
+
def test_successfull_call
+ Patron::Session.any_instance.expects(:get).with('/image.svg').returns( stub(:headers => {}, :body => File.read( File.dirname(__FILE__) + '/assets/fish.svg'), :status => 200))
+
assert SvgGenerator.from_url('static.shopify.com', '/image.svg.png')
end
def test_wrong_filename
+
assert_equal nil, SvgGenerator.from_url('static.shopify.com', '/image.svg')
assert_equal nil, SvgGenerator.from_url('static.shopify.com', '/image.png')
assert_equal nil, SvgGenerator.from_url('static.shopify.com', '/image.svg.bmp')
Please sign in to comment.
Something went wrong with that request. Please try again.