Skip to content

Commit

Permalink
Added etag support for PostsController#show.
Browse files Browse the repository at this point in the history
  • Loading branch information
nakajima committed Feb 8, 2009
1 parent 5e0cb25 commit b4554ab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
2 changes: 2 additions & 0 deletions app/controllers/posts_controller.rb
Expand Up @@ -25,6 +25,8 @@ def index
def show
redirect_to root_path and return unless current_post.type.match(/Article|Snippet/)

fresh_when(:etag => current_post)

@comment = flash[:comment] || current_post.comments.build
respond_to do |format|
format.html { render :template => 'posts/show.html.erb' }
Expand Down
43 changes: 24 additions & 19 deletions test/functional/posts_controller_test.rb
Expand Up @@ -4,38 +4,43 @@ class PostsControllerTest < ActionController::TestCase

# Generated tests

def test_should_get_index
test "should_get_index" do
get :index
assert_response :success
assert_not_nil assigns(:posts)
end

def test_should_get_show
test "should_get_show" do
get :show, :id => posts(:one).permalink
assert_response :success
end

def test_should_redirect_to_admin_index_if_logged_in
test "should provide etag" do
get :show, :id => posts(:one).permalink
assert_not_nil @response.etag
end

test "should_redirect_to_admin_index_if_logged_in" do
login_as :quentin
get :index
assert_redirected_to admin_posts_path
end

def test_should_redirect_to_admin_index_if_logged_in_even_for_relative_urls
test "should_redirect_to_admin_index_if_logged_in_even_for_relative_urls" do
set_relative_url do
login_as :quentin
get :index
assert_redirected_to admin_posts_path
end
end

def test_should_redirect_to_admin_show_if_logged_in
test "should_redirect_to_admin_show_if_logged_in" do
login_as :quentin
get :show, :id => posts(:one).permalink
assert_redirected_to admin_post_path(posts(:one))
end

def test_should_redirect_to_admin_show_if_logged_in_even_for_relative_urls
test "should_redirect_to_admin_show_if_logged_in_even_for_relative_urls" do
set_relative_url do
login_as :quentin
get :show, :id => posts(:one).permalink
Expand All @@ -48,7 +53,7 @@ def test_should_redirect_to_admin_show_if_logged_in_even_for_relative_urls
# NOTE Since these are all now handled by dynamically created controllers,
# we'll need a different way of testing them.
#
# def test_should_retrieve_links_only
# test "should_retrieve_links_only" do
# @request.stubs(:path).returns('/links')
# Link.expects(:paginate_index).returns(posts_stub)
# Post.expects(:paginate_index).never
Expand All @@ -57,7 +62,7 @@ def test_should_redirect_to_admin_show_if_logged_in_even_for_relative_urls
# assert_equal 'links', assigns(:post_type)
# end
#
# def test_should_retrieve_articles_only
# test "should_retrieve_articles_only" do
# @request.stubs(:path).returns('/articles')
# Article.expects(:paginate_index).returns(posts_stub)
# Post.expects(:paginate_index).never
Expand All @@ -66,7 +71,7 @@ def test_should_redirect_to_admin_show_if_logged_in_even_for_relative_urls
# assert_equal 'articles', assigns(:post_type)
# end
#
# def test_should_retrieve_snippets_only
# test "should_retrieve_snippets_only" do
# @request.stubs(:path).returns('/snippets')
# Snippet.expects(:paginate_index).returns(posts_stub)
# Post.expects(:paginate_index).never
Expand All @@ -75,7 +80,7 @@ def test_should_redirect_to_admin_show_if_logged_in_even_for_relative_urls
# assert_equal 'snippets', assigns(:post_type)
# end
#
# def test_should_retrieve_tweets_only
# test "should_retrieve_tweets_only" do
# @request.stubs(:path).returns('/tweets')
# Tweet.expects(:paginate_index).returns(posts_stub)
# Post.expects(:paginate_index).never
Expand All @@ -84,7 +89,7 @@ def test_should_redirect_to_admin_show_if_logged_in_even_for_relative_urls
# assert_equal 'tweets', assigns(:post_type)
# end
#
# def test_should_retrieve_quotes_only
# test "should_retrieve_quotes_only" do
# @request.stubs(:path).returns('/quotes')
# Quote.expects(:paginate_index).returns(posts_stub)
# Post.expects(:paginate_index).never
Expand All @@ -93,7 +98,7 @@ def test_should_redirect_to_admin_show_if_logged_in_even_for_relative_urls
# assert_equal 'quotes', assigns(:post_type)
# end
#
# def test_should_retrieve_pictures_only
# test "should_retrieve_pictures_only" do
# @request.stubs(:path).returns('/pictures')
# Picture.expects(:paginate_index).returns(posts_stub)
# Post.expects(:paginate_index).never
Expand All @@ -102,36 +107,36 @@ def test_should_redirect_to_admin_show_if_logged_in_even_for_relative_urls
# assert_equal 'pictures', assigns(:post_type)
# end

def test_should_show_post
test "should_show_post" do
get :show, :id => posts(:one).permalink
assert_response :success
end

def test_should_redirect_to_index_from_show_unless_article
test "should_redirect_to_index_from_show_unless_article" do
get :show, :id => posts(:two).id
assert_redirected_to root_path
end

def test_should_redirect_to_index_from_show_unless_article_even_for_relative_urls
test "should_redirect_to_index_from_show_unless_article_even_for_relative_urls" do
set_relative_url do
get :show, :id => posts(:two).id
assert_redirected_to root_path
end
end

def test_should_redirect_to_root_when_post_not_found
test "should_redirect_to_root_when_post_not_found" do
get :show, :id => 999999
assert_redirected_to root_path
end

def test_should_redirect_to_root_when_post_not_found_even_for_relative_urls
test "should_redirect_to_root_when_post_not_found_even_for_relative_urls" do
set_relative_url do
get :show, :id => 999999
assert_redirected_to root_path
end
end

def test_feed_tag
test "feed_tag" do
get :index
assert(h = Hpricot.parse(@response.body))
links = h.search("link[@type='application/rss+xml']")
Expand All @@ -141,7 +146,7 @@ def test_feed_tag
end
end

def test_feed_tag_with_relative_urls
test "feed_tag_with_relative_urls" do
set_relative_url do
get :index
assert(h = Hpricot.parse(@response.body))
Expand Down

0 comments on commit b4554ab

Please sign in to comment.