From bd8e725578bc8cd1bd5b432f44f53b447855783f Mon Sep 17 00:00:00 2001 From: Alexey Verkhovsky Date: Mon, 17 Jan 2005 18:09:13 +0000 Subject: [PATCH] Added assertions for the response content in pdf action; changed all output file names to include seconds --- app/controllers/wiki_controller.rb | 12 +++--- test/functional/wiki_controller_test.rb | 56 ++++++++++++++++++++----- test/test_helper.rb | 18 ++++++++ 3 files changed, 69 insertions(+), 17 deletions(-) diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 0f6da854..8f4eb862 100755 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -80,16 +80,16 @@ class WikiController < ApplicationController end def export_pdf - file_name = "#{web.address}-tex-#{web.revised_on.strftime('%Y-%m-%d-%H-%M')}" + file_name = "#{web.address}-tex-#{web.revised_on.strftime('%Y-%m-%d-%H-%M-%S')}" file_path = WikiService.storage_path + file_name - export_web_to_tex(file_path + ".tex") unless FileTest.exists?(file_path + ".tex") - convert_tex_to_pdf(file_path + ".tex") - send_export(file_name + ".pdf", file_path + ".pdf") + export_web_to_tex "#{file_path}.tex" unless FileTest.exists? "#{file_path}.tex" + convert_tex_to_pdf "#{file_path}.tex" + send_export("#{file_name}.tex", "#{file_path}.tex") end def export_tex - file_name = "#{web.address}-tex-#{web.revised_on.strftime('%Y-%m-%d-%H-%M')}.tex" + file_name = "#{web.address}-tex-#{web.revised_on.strftime('%Y-%m-%d-%H-%M-%S')}.tex" file_path = WikiService.storage_path + file_name export_web_to_tex(file_path) unless FileTest.exists?(file_path) @@ -182,7 +182,7 @@ class WikiController < ApplicationController def pdf page = wiki.read_page(@web_name, @page_name) safe_page_name = @page.name.gsub(/\W/, '') - file_name = "#{safe_page_name}-#{@web.address}-#{@page.created_at.strftime("%Y-%m-%d-%H-%M")}" + file_name = "#{safe_page_name}-#{@web.address}-#{@page.created_at.strftime('%Y-%m-%d-%H-%M-%S')}" file_path = WikiService.storage_path + file_name export_page_to_tex(file_path + '.tex') unless FileTest.exists?(file_path + '.tex') diff --git a/test/functional/wiki_controller_test.rb b/test/functional/wiki_controller_test.rb index 7010c59e..2535b4b8 100755 --- a/test/functional/wiki_controller_test.rb +++ b/test/functional/wiki_controller_test.rb @@ -2,7 +2,7 @@ # Uncomment the line below to enable pdflatex tests; don't forget to comment them again # commiting to SVN -$INSTIKI_TEST_PDFLATEX = true +# $INSTIKI_TEST_PDFLATEX = true require File.dirname(__FILE__) + '/../test_helper' require 'wiki_controller' @@ -263,19 +263,53 @@ class WikiControllerTest < Test::Unit::TestCase end -if ENV['INSTIKI_TEST_LATEX'] or defined? $INSTIKI_TEST_PDFLATEX + if ENV['INSTIKI_TEST_LATEX'] or defined? $INSTIKI_TEST_PDFLATEX + + def test_pdf + assert RedClothForTex.available?, 'Cannot do test_pdf when pdflatex is not available' + r = process('pdf', 'web' => 'wiki1', 'id' => 'HomePage') + assert_success - def test_pdf - if RedClothForTex.available? - process('pdf', 'web' => 'wiki1', 'id' => 'HomePage') + content = r.binary_content + + assert_equal '%PDF', content[0..3] + assert_equal "EOF\n", content[-4..-1] + + assert_equal 'application/octet_stream', r.headers['Content-Type'] + assert_match /attachment; filename="HomePage-wiki1-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.pdf"/, + r.headers['Content-Disposition'] end - end + + def test_pdf + assert RedClothForTex.available?, 'Cannot do test_pdf when pdflatex is not available' + r = process('pdf', 'web' => 'wiki1', 'id' => 'HomePage') + assert_success + + sio = StringIO.new + begin + $stdout = sio + r.body.call + ensure + $stdout = STDOUT + end + + sio.rewind + content = sio.read + + assert_equal '%PDF', content[0..3] + assert_equal "EOF\n", content[-4..-1] -else - puts "Warning: tests involving pdflatex are very slow, therefore they are disable by default." - puts " Set environment variable INSTIKI_TEST_PDFLATEX or global Ruby variable" - puts " $INSTIKI_TEST_PDFLATEX to enable them." -end + assert_equal 'application/octet_stream', r.headers['Content-Type'] + assert_match /attachment; filename="HomePage-wiki1-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.pdf"/, + r.headers['Content-Disposition'] + end + + + else + puts 'Warning: tests involving pdflatex are very slow, therefore they are disable by default.' + puts ' Set environment variable INSTIKI_TEST_PDFLATEX or global Ruby variable' + puts ' $INSTIKI_TEST_PDFLATEX to enable them.' + end def test_print diff --git a/test/test_helper.rb b/test/test_helper.rb index fa83110b..e65c143b 100755 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -68,3 +68,21 @@ module ChunkMatch end end end + + +module AbstractController + class TestResponse + def binary_content + sio = StringIO.new + begin + $stdout = sio + r.body.call + ensure + $stdout = STDOUT + end + + sio.rewind + sio.read + end + end +end