Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 52 lines (43 sloc) 1.203 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
require File.expand_path '../helper', __FILE__
require 'airbrake/rails/javascript_notifier'
require 'ostruct'

class JavascriptNotifierTest < Test::Unit::TestCase
  module FakeRenderer
    def javascript_tag(text)
      "<script>#{text}</script>"
    end
    def escape_javascript(text)
      "ESC#{text}ESC"
    end
  end

  class FakeController
    def self.helper_method(*args)
    end

    include Airbrake::Rails::JavascriptNotifier

    def action_name
      "action"
    end

    def controller_name
      "controller"
    end

    def request
      @request ||= OpenStruct.new
    end

    def render_to_string(options)
      context = OpenStruct.new(options[:locals])
      context.extend(FakeRenderer)
      context.instance_eval do
        erb = ERB.new(IO.read(options[:file]))
        erb.result(binding)
      end
    end
  end

  should "make sure escape_javacript is called on the request.url" do
    Airbrake.configure do
    end
    controller = FakeController.new
    controller.request.url = "bad_javascript"
    assert controller.send(:airbrake_javascript_notifier)['"ESCbad_javascriptESC"']
    assert ! controller.send(:airbrake_javascript_notifier)['"bad_javascript"']
  end
end
Something went wrong with that request. Please try again.