-
Notifications
You must be signed in to change notification settings - Fork 3
/
xhr_accept_headers.rb
61 lines (51 loc) · 1.75 KB
/
xhr_accept_headers.rb
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
52
53
54
55
56
57
58
59
60
61
require 'rubygems'
require 'rjb'
class_path = Dir[File.expand_path(File.dirname(__FILE__) + '/../../lib/htmlunit') + '/*.jar'].join(':')
Rjb::load(class_path)
Url = Rjb::import('java.net.URL')
WebClient = Rjb::import('com.gargoylesoftware.htmlunit.WebClient')
HtmlUnitMockWebConnection = Rjb::import('com.gargoylesoftware.htmlunit.MockWebConnection')
class MockConnection
attr_reader :connection
def initialize
@connection = HtmlUnitMockWebConnection.new
end
def method_missing(method, *args)
p [method, args.first.toString]
@connection.send(method, *args)
end
end
connection = MockConnection.new
# connection = HtmlUnitMockWebConnection.new
content = <<-html
<html>
<head>
<script>
document.title = 'pre ajax call';
function some_ajax(url) {
xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.setRequestHeader("Accept", "application/json, text/javascript, */*");
xhr.onreadystatechange = function() {
document.title = document.title + ' ' + xhr.readyState
if(xhr.readyState == 4) document.title = xhr.responseText
}
xhr.send(null);
}
some_ajax('/ajax/1');
</script>
</head>
</html>
html
url = Url.new('http://localhost:3000/foo')
connection.setResponse(url, content, 'text/html')
content = 'document.title = "FOO";'
url = Url.new('http://localhost:3000/ajax/1')
connection.setResponse(url, content, 'application/javascript')
client = WebClient.new
client.setCssEnabled(true)
client.setJavaScriptEnabled(true)
client.setWebConnection(Rjb::bind(connection, 'com.gargoylesoftware.htmlunit.WebConnection'))
# client.setWebConnection(connection)
page = client.getPage('http://localhost:3000/foo')
# puts page.asXml