Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Mock xhr early

Needed for window.onload-type ajax requests
  • Loading branch information...
commit 6c814d5dcb2cbad3e9224471ca934db8ee5d4f67 1 parent de579bc
@mynyml authored
Showing with 65 additions and 47 deletions.
  1. +15 −22 lib/holygrail.rb
  2. +45 −21 test/integration_test.rb
  3. +5 −4 test/test_helper.rb
View
37 lib/holygrail.rb
@@ -1,15 +1,14 @@
require 'harmony'
module HolyGrail
- class XhrProxy
- class << self
- attr_accessor :context
+ module XhrProxy
+ extend self
+ attr_accessor :context
- def request(info, data="")
- context.instance_eval do
- xhr(info["method"].downcase, info["url"])
- @response.body.to_s
- end
+ def request(info, data="")
+ context.instance_eval do
+ xhr(info["method"].downcase, info["url"])
+ @response.body.to_s
end
end
end
@@ -33,7 +32,6 @@ module HolyGrail
#
# @private
def process(*args) #:nodoc:
- #::HolyGrail::XhrProxy.context = self
@__page = nil
super
end
@@ -57,21 +55,14 @@ def process(*args) #:nodoc:
# javascript code exception
#
def js(code)
- @__page ||=
- begin
- ::HolyGrail::XhrProxy.context = self
- page = Harmony::Page.new(rewrite_script_paths(@response.body.to_s))
- page.execute_js(mock_xhr)
- page
- end
+ ::HolyGrail::XhrProxy.context = self
+ @__page ||= Harmony::Page.new(XHR_MOCK_SCRIPT + rewrite_script_paths(@response.body.to_s))
@__page.execute_js(code)
end
alias :execute_javascript :js
- private
-
- def mock_xhr
- <<-JS
+ XHR_MOCK_SCRIPT = <<-JS
+ <script>
XMLHttpRequest.prototype.open = function(method, url, async, username, password) {
this.info = { method: method, url: url }
}
@@ -80,8 +71,10 @@ def mock_xhr
this.readyState = 4
this.onreadystatechange()
}
- JS
- end
+ </script>
+ JS
+
+ private
# Rewrite relative src paths in <script> tags
#
View
66 test/integration_test.rb
@@ -2,30 +2,48 @@
class IntegrationController < ActionController::Base
+ def perform_xhr_function
+ <<-JS
+ function perform_xhr(method, url) {
+ var xhr = new XMLHttpRequest()
+ xhr.open(method, url, false) //false == synchronous
+ xhr.onreadystatechange = function() {
+ if (this.readyState != 4) { return }
+ document.body.innerHTML = this.responseText
+ }
+ xhr.send(null) // POST request sends data here
+ }
+ JS
+ end
+
def baz
render :text => <<-HTML
<html>
<head>
+ <script>#{perform_xhr_function}</script>
+ </head>
+ <body></body>
+ </html>
+ HTML
+ end
+
+ def boo
+ render :text => <<-HTML
+ <html>
+ <head>
+ <script>#{perform_xhr_function}</script>
<script>
- function perform_xhr(method, url) {
- var xhr = new XMLHttpRequest()
- xhr.open(method, url, false) //false == synchronous
- xhr.onreadystatechange = function() {
- if (this.readyState != 4) { return }
- document.getElementById("xhr_result").innerHTML = this.responseText
- }
- xhr.send(null) // POST request sends data here
+ window.onload = function() {
+ perform_xhr("GET", "xhr")
}
</script>
</head>
- <body>
- <div id="xhr_result">orig</div>
- </body>
+ <body></body>
</html>
HTML
end
- def baz_xhr
+ def xhr
render :text => "xhr response"
end
end
@@ -34,7 +52,6 @@ class IntegrationControllerTest < ActionController::IntegrationTest
# TODO test xhr POST
# TODO test xhr uris with initial "/"
- # TODO test xhr is mocked early, e.g. requests triggered on page load
test "api" do
assert_respond_to self, :execute_javascript
@@ -44,25 +61,32 @@ class IntegrationControllerTest < ActionController::IntegrationTest
## xhr
test "xhr calls controller" do
- get "baz"
+ get 'baz'
- assert_equal "orig", js(<<-JS)
- document.getElementById("xhr_result").innerHTML
+ assert_equal "", js(<<-JS).gsub("\n",'').strip
+ document.body.innerHTML
JS
assert_equal "xhr response", js(<<-JS)
- perform_xhr("GET", "baz_xhr")
- document.getElementById("xhr_result").innerHTML
+ perform_xhr("GET", "xhr")
+ document.body.innerHTML
JS
end
test "xhr identifes properly" do
- get "baz"
+ get 'baz'
assert_nil request.headers['X-Requested-With']
js(<<-JS)
- perform_xhr("GET", "baz_xhr")
+ perform_xhr("GET", "xhr")
+ JS
+ assert_equal 'XMLHttpRequest', request.headers['X-Requested-With']
+ end
+
+ test "xhr is mocked early" do
+ get 'boo'
+ assert_equal "xhr response", js(<<-JS)
+ document.body.innerHTML
JS
- assert_equal "XMLHttpRequest", request.headers['X-Requested-With']
end
end
View
9 test/test_helper.rb
@@ -15,10 +15,11 @@ def self.root
end
ActionController::Routing::Routes.draw do |map|
- map.connect '/foo', :controller => 'functionals', :action => 'foo'
- map.connect '/bar', :controller => 'functionals', :action => 'bar'
- map.connect '/baz', :controller => 'integration', :action => 'baz'
- map.connect '/baz_xhr', :controller => 'integration', :action => 'baz_xhr'
+ map.connect '/foo', :controller => 'functionals', :action => 'foo'
+ map.connect '/bar', :controller => 'functionals', :action => 'bar'
+ map.connect '/baz', :controller => 'integration', :action => 'baz'
+ map.connect '/xhr', :controller => 'integration', :action => 'xhr'
+ map.connect '/boo', :controller => 'integration', :action => 'boo'
end
ActionController::Base.session = {
Please sign in to comment.
Something went wrong with that request. Please try again.