# encoding: utf-8 require "spec_helper" require 'action_controller' require 'action_controller/test_case' require "tempfile" class FootnotesController < ActionController::Base attr_accessor :template, :performed_render end module Footnotes::Notes class TestNote < AbstractNote def self.to_sym; :test; end def valid?; true; end end class NoteXNote < TestNote; end class NoteYNote < TestNote; end class NoteZNote < TestNote; end class FileURINote < TestNote def link "/example/local file path/with-special-chars/öäü/file" end end end describe "Footnotes" do before do @controller = FootnotesController.new @controller.template = Object.new @controller.request = ActionDispatch::TestRequest.create @controller.response = ActionDispatch::TestResponse.new @controller.response_body = HTML_DOCUMENT.dup @controller.params = {} Footnotes::Filter.notes = [ :test ] Footnotes::Filter.multiple_notes = false @footnotes = Footnotes::Filter.new(@controller) end it "footnotes_controller" do index = @controller.response.body.index(/This is the HTML page/) expect(index).to eql 334 end context "response_body is file" do before do @file = Tempfile.new("test") def @file.body; read; end @file.write "foobarbaz" @file.rewind end after do @file.close! end it "should not change file position" do @controller.response_body = @file expect { @footnotes = Footnotes::Filter.new(@controller) }.not_to change{ @controller.response_body.pos } end end it "foonotes_included" do footnotes_perform! expect(@controller.response_body).not_to eq(HTML_DOCUMENT) end it "should escape links with special chars" do note_with_link = Footnotes::Notes::FileURINote.new link = Footnotes::Filter.prefix(note_with_link.link, 1, 1, 1) expect(link).to eql "txmt://open?url=file:///example/local%20file%20path/with-special-chars/%C3%B6%C3%A4%C3%BC/file&line=1&column=1" end specify "footnotes_not_included_when_request_is_xhr" do @controller.request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' @controller.request.env['HTTP_ACCEPT'] = 'text/javascript, text/html, application/xml, text/xml, */*' footnotes_perform! expect(@controller.response.body).to eql HTML_DOCUMENT end specify "footnotes_not_included_when_content_type_is_javascript" do @controller.response.content_type = 'text/javascript' footnotes_perform! expect(@controller.response.body).to eql HTML_DOCUMENT end specify "footnotes_included_when_content_type_is_html" do @controller.response.content_type = 'text/html; charset=utf-8' footnotes_perform! expect(@controller.response.body).not_to eql HTML_DOCUMENT end specify "footnotes_included_when_content_type_is_nil" do footnotes_perform! expect(@controller.response.body).not_to eql HTML_DOCUMENT end specify "not_included_when_body_is_not_a_string" do allow(@controller.response).to receive(:body).and_return(Time.now)# = Proc.new { Time.now } expect(Footnotes::Filter.new(@controller).send(:valid?)).not_to be expect(@controller.response.body).not_to match(/')).to be end specify "insert_footnotes_inside_body" do @controller.response.body = "