Skip to content

Commit

Permalink
Makes Merb::Cache::CacheRequest play more like a real request which a…
Browse files Browse the repository at this point in the history
…llows it to work with page store (query_string_present? didn't work before)
  • Loading branch information
giom committed Dec 19, 2008
1 parent d3c1545 commit 22f29e1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
24 changes: 16 additions & 8 deletions lib/merb-cache/cache_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@ module Merb
module Cache
class CacheRequest < Merb::Request

attr_accessor :path, :params
attr_accessor :params

def initialize(path, params = {}, env = {})
def initialize(uri = "", params = {}, env = {})
uri = URI(uri || '/')

env[Merb::Const::REQUEST_URI] = uri.respond_to?(:request_uri) ? uri.request_uri : uri.to_s
env[Merb::Const::HTTP_HOST] = uri.host + (uri.port != 80 ? ":#{uri.port}" : '') if uri.host
env[Merb::Const::SERVER_PORT] = uri.port.to_s if uri.port
env[Merb::Const::QUERY_STRING] = uri.query.to_s if uri.query

env[Merb::Const::REQUEST_METHOD] = params.delete(:method).to_s.upcase if params[:method]

super(DEFAULT_ENV.merge(env))

@path, @params = path, params

self.env[Merb::Const::REQUEST_PATH] = self.env[Merb::Const::PATH_INFO] = self.path
@params = params
end

DEFAULT_ENV = Mash.new({
'SERVER_NAME' => 'localhost',
'PATH_INFO' => '/',
'HTTP_ACCEPT_ENCODING' => 'gzip,deflate',
'HTTP_USER_AGENT' => 'Ruby/Merb (ver: ' + Merb::VERSION + ') merb-cache',
'SCRIPT_NAME' => '/',
Expand All @@ -26,12 +35,11 @@ def initialize(path, params = {}, env = {})
'HTTP_REFERER' => 'http://localhost/',
'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'HTTP_VERSION' => 'HTTP/1.1',
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'GET',
'SERVER_PORT' => '80',
'GATEWAY_INTERFACE' => 'CGI/1.2',
'HTTP_ACCEPT' => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
'HTTP_CONNECTION' => 'keep-alive',
'REQUEST_METHOD' => 'GET'
'HTTP_CONNECTION' => 'keep-alive'
}) unless defined?(DEFAULT_ENV)
end
end
Expand Down
51 changes: 48 additions & 3 deletions spec/merb-cache/cache_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,42 @@
Merb::Cache::CacheRequest.superclass.should == Merb::Request
end

describe "#env" do
it "can be specified in the constructor" do
Merb::Cache::CacheRequest.new('', {}, 'foo' => 'bar').env['foo'].should == 'bar'
end
end

describe "#env[Merb::Const::REQUEST_URI]" do
it "should give the uri with the query string" do
Merb::Cache::CacheRequest.new('/test?q=1').env[Merb::Const::REQUEST_URI].should == '/test?q=1'
end
end

describe "#host" do
it "should return the correct host if the uri is absolute" do
Merb::Cache::CacheRequest.new('http://example.org:453/').host.should == "example.org:453"
end
end

describe "#method" do
it "should be :get by default" do
Merb::Cache::CacheRequest.new('/test?q=1').method.should == :get
end

it "should be set by the :method parameter" do
Merb::Cache::CacheRequest.new('/test?q=1', :method => :put).method.should == :put
end
end

describe "#path" do
it "can be specified without manipulating the env" do
Merb::Cache::CacheRequest.new('/path/to/foo').path.should == '/path/to/foo'
end

it "should return the path without the query string" do
Merb::Cache::CacheRequest.new('/path/to/foo?q=1').path.should == '/path/to/foo'
end
end

describe "#params" do
Expand All @@ -17,11 +49,24 @@
end
end

describe "#env" do
it "can be specified in the constructor" do
Merb::Cache::CacheRequest.new('', {}, 'foo' => 'bar').env['foo'].should == 'bar'
describe "#subdomains" do
it "should return the correct subdomains if the uri is absolute" do
Merb::Cache::CacheRequest.new('http://test.example.org:453/').subdomains.should == ['test']
end
end

describe "#uri" do
it "should give the uri without the query string" do
Merb::Cache::CacheRequest.new('/test?q=1').uri.should == '/test'
end
end

it "should be compatiple with page store's way of detecting the presence of a query string" do
request = Merb::Cache::CacheRequest.new("/test?q=1")
(request.env[Merb::Const::REQUEST_URI] == request.uri).should be_false
request = Merb::Cache::CacheRequest.new("/test")
(request.env[Merb::Const::REQUEST_URI] == request.uri).should be_true
end

it "should setup a default env" do
Merb::Cache::CacheRequest.new('').env.should_not be_empty
Expand Down

0 comments on commit 22f29e1

Please sign in to comment.