Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added last_modified header for show action, presuming the record resp…

…onds to :updated_at
  • Loading branch information...
commit 88891aa8f5866b956d45786e4d42c0b52368db80 1 parent f5741be
@nakajima authored
View
16 features/headers.feature
@@ -0,0 +1,16 @@
+Feature: Setting appropriate headers
+ In order to understand more about a response
+ As a developer
+ I want Sinatra's Hat to set useful headers
+
+ Scenario: Setting the last_modified for show
+ Given a model that has a record
+ And I mount the model
+ When I get the show page for that record
+ Then "Last-Modified" should be the record "updated_at" time
+
+ # Scenario: Setting the last_modified for index
+ # Given a model that has a record
+ # And I mount the model
+ # When Make a GET request to the index without a format
+ # Then "Last-Modified" should be the record "updated_at" time
View
2  features/steps/common_steps.rb
@@ -1,6 +1,7 @@
Before do
build_model(:people) do
string :name
+ timestamps
has_many :comments
@@ -10,6 +11,7 @@
build_model(:comments) do
integer :person_id
string :name
+ timestamps
belongs_to :person
end
View
3  features/steps/header_steps.rb
@@ -0,0 +1,3 @@
+Then /^"(.*)" should be the record "(.*)" time$/ do |header, key|
+ DateTime.parse(response.headers[header].to_s).should == @record.send(key)
+end
View
1  lib/sinatras-hat/actions.rb
@@ -33,6 +33,7 @@ def self.included(map)
map.action :show, '/:id' do |request|
record = model.find(request.params) || responder.not_found(request)
+ request.last_modified(record.updated_at) if record.respond_to?(:updated_at)
responder.success(:show, request, record)
end
View
3  lib/sinatras-hat/response.rb
@@ -13,8 +13,9 @@ def initialize(maker, request)
@request = request
end
- def render(action)
+ def render(action, options={})
begin
+ options.each { |sym, value| @request.send(sym, value) }
@request.erb "#{maker.prefix}/#{action}".to_sym
rescue Errno::ENOENT
no_template! "Can't find #{File.expand_path(File.join(views, action.to_s))}.erb"
View
23 spec/actions/show_spec.rb
@@ -28,11 +28,16 @@ def handle(*args)
before(:each) do
params = { :format => "yaml", :id => @article.to_param }
@request = fake_request(params)
- stub(maker.model).find(params).returns(:article)
+ stub(maker.model).find(params).returns(@article)
end
it "serializes the data in the appropriate format" do
- mock.proxy(maker.responder).serialize(request, :article)
+ mock.proxy(maker.responder).serialize(request, @article)
+ handle(request)
+ end
+
+ it "sets last_modified param" do
+ mock(request).last_modified(@article.updated_at)
handle(request)
end
end
@@ -41,11 +46,16 @@ def handle(*args)
before(:each) do
params = { :id => @article.to_param }
@request = fake_request(params)
- stub(maker.model).find(params).returns(:article)
+ stub(maker.model).find(params).returns(@article)
end
it "renders the show template" do
- mock.proxy(maker.responder).success(:show, request, :article)
+ mock.proxy(maker.responder).success(:show, request, @article)
+ handle(request)
+ end
+
+ it "sets last_modified param" do
+ mock(request).last_modified(@article.updated_at)
handle(request)
end
end
@@ -61,5 +71,10 @@ def handle(*args)
mock.proxy(maker.responder).not_found(request)
handle(request)
end
+
+ it "does not set last_modified param" do
+ mock(request).last_modified(@article.updated_at).never
+ handle(request)
+ end
end
end
View
7 spec/response_spec.rb
@@ -28,6 +28,13 @@ def new_response
end
end
+ context "when there are options passed" do
+ it "sends the options to the request" do
+ mock(request).last_modified :yesterday
+ new_response.render(:show, :last_modified => :yesterday)
+ end
+ end
+
context "when there is no views dir" do
before(:each) do
stub(request.options).views { nil }
View
2  spec/spec_helper.rb
@@ -29,6 +29,7 @@ def fixture(path)
build_model(:articles) do
string :name
string :description
+ timestamps
has_many :comments
end
@@ -68,6 +69,7 @@ def fake_request(options={})
stub(request).env.returns({ })
stub(request).params.returns(options)
stub(request).response.returns(Sinatra::Response.new)
+ stub(request).last_modified(anything)
request
end
Please sign in to comment.
Something went wrong with that request. Please try again.