This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

fix issue where the gsub to replace the html head with javascript was…

… removing the head element entirely
  • Loading branch information...
1 parent c39b2a5 commit e0fae1a79b2c858f5f3b42d969de62de725aabcc @mjankowski mjankowski committed Jan 9, 2011
@@ -24,6 +24,10 @@ Feature: Install the Gem in a Rails application and enable the JavaScript notifi
And the notifier JavaScript should provide the following errorDefaults:
| url | component | action |
| http://example.com:123/test/index | test | index |
+ And I should see the following value as the html head:
+ """
+ <head profile="http://example.com">
+ """
Scenario: Include the Javascript notifier when enabled using custom configuration settings
When I generate a new Rails application
@@ -45,6 +49,10 @@ Feature: Install the Gem in a Rails application and enable the JavaScript notifi
Then I should see the notifier JavaScript for the following:
| api_key | environment | host |
| myapikey! | production | myhoptoad.com:3001 |
+ And I should see the following value as the html head:
+ """
+ <head>
+ """
Scenario: Don't include the Javascript notifier by default
When I generate a new Rails application
@@ -356,6 +356,11 @@ def rails_non_initializer_hoptoad_config_file
end
end
+Given /^I should see the following value as the html head:$/ do |value|
+ document_body = '<html>' + @terminal.output.split('<html>').last
+ document_body.should include(value)
+end
+
Then "the notifier JavaScript should provide the following errorDefaults:" do |table|
hash = table.hashes.first
@@ -34,9 +34,14 @@ def insert_hoptoad_javascript_notifier
end
if response.body.respond_to?(:gsub)
- response.body = response.body.gsub(/<(head.*?)>/i, "<\1>\n" + javascript)
+ response.body = insert_javascript_after_head response.body, javascript
end
end
+
+ def insert_javascript_after_head(body, javascript)
+ body.gsub /<(head.*?)>/i, "<\\1>\n#{javascript}\n"
+ end
+
end
end
end
@@ -0,0 +1,38 @@
+require File.dirname(__FILE__) + '/helper'
+
+require 'hoptoad_notifier/rails/javascript_notifier'
+
+class JavascriptNotifierTest < Test::Unit::TestCase
+
+ def self.after_filter(arg); end
+ include ::HoptoadNotifier::Rails::JavascriptNotifier
+
+ should "insert javascript after head" do
+ input_body =<<-EOS
+<html><head><title></title></head><body></body></html>
+ EOS
+ javascript = "js-code"
+ expected =<<-EOS
+<html><head>
+js-code
+<title></title></head><body></body></html>
+ EOS
+ output = send :insert_javascript_after_head, input_body, javascript
+ assert_equal expected, output
+ end
+
+ should "insert javascript after head when head has attributes" do
+ input_body =<<-EOS
+<html><head lang="en"><title></title></head><body></body></html>
+ EOS
+ javascript = "js-code"
+ expected =<<-EOS
+<html><head lang="en">
+js-code
+<title></title></head><body></body></html>
+ EOS
+ output = send :insert_javascript_after_head, input_body, javascript
+ assert_equal expected, output
+ end
+
+end

0 comments on commit e0fae1a

Please sign in to comment.