This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
/
browser_spec.rb
261 lines (224 loc) · 7.78 KB
/
browser_spec.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
require 'spec_helper'
require 'self_signed_ssl_cert'
require 'stringio'
require 'capybara/webkit/driver'
require 'socket'
require 'base64'
describe Capybara::Webkit::Browser do
let(:browser) { Capybara::Webkit::Browser.new(Capybara::Webkit::Connection.new) }
let(:browser_ignore_ssl_err) do
Capybara::Webkit::Browser.new(Capybara::Webkit::Connection.new).tap do |browser|
browser.ignore_ssl_errors
end
end
let(:browser_skip_images) do
Capybara::Webkit::Browser.new(Capybara::Webkit::Connection.new).tap do |browser|
browser.set_skip_image_loading(true)
end
end
context 'handling of SSL validation errors' do
before do
# set up minimal HTTPS server
@host = "127.0.0.1"
@server = TCPServer.new(@host, 0)
@port = @server.addr[1]
# set up SSL layer
ssl_serv = OpenSSL::SSL::SSLServer.new(@server, $openssl_self_signed_ctx)
@server_thread = Thread.new(ssl_serv) do |serv|
while conn = serv.accept do
# read request
request = []
until (line = conn.readline.strip).empty?
request << line
end
# write response
html = "<html><body>D'oh!</body></html>"
conn.write "HTTP/1.1 200 OK\r\n"
conn.write "Content-Type:text/html\r\n"
conn.write "Content-Length: %i\r\n" % html.size
conn.write "\r\n"
conn.write html
conn.close
end
end
end
after do
@server_thread.kill
@server.close
end
it "doesn't accept a self-signed certificate by default" do
lambda { browser.visit "https://#{@host}:#{@port}/" }.should raise_error
end
it 'accepts a self-signed certificate if configured to do so' do
browser_ignore_ssl_err.visit "https://#{@host}:#{@port}/"
end
it "doesn't accept a self-signed certificate in a new window by default" do
browser.execute_script("window.open('about:blank')")
browser.window_focus(browser.get_window_handles.last)
lambda { browser.visit "https://#{@host}:#{@port}/" }.should raise_error
end
it 'accepts a self-signed certificate in a new window if configured to do so' do
browser_ignore_ssl_err.execute_script("window.open('about:blank')")
browser_ignore_ssl_err.window_focus(browser_ignore_ssl_err.get_window_handles.last)
browser_ignore_ssl_err.visit "https://#{@host}:#{@port}/"
end
end
context "skip image loading" do
before(:each) do
# set up minimal HTTP server
@host = "127.0.0.1"
@server = TCPServer.new(@host, 0)
@port = @server.addr[1]
@received_requests = []
@server_thread = Thread.new do
while conn = @server.accept
Thread.new(conn) do |conn|
# read request
request = []
until (line = conn.readline.strip).empty?
request << line
end
@received_requests << request.join("\n")
# write response
html = <<-HTML
<html>
<head>
<style>
body {
background-image: url(/path/to/bgimage);
}
</style>
</head>
<body>
<img src="/path/to/image"/>
</body>
</html>
HTML
conn.write "HTTP/1.1 200 OK\r\n"
conn.write "Content-Type:text/html\r\n"
conn.write "Content-Length: %i\r\n" % html.size
conn.write "\r\n"
conn.write html
conn.write("\r\n\r\n")
conn.close
end
end
end
end
after(:each) do
@server_thread.kill
@server.close
end
it "should load images in image tags by default" do
browser.visit("http://#{@host}:#{@port}/")
@received_requests.find {|r| r =~ %r{/path/to/image} }.should_not be_nil
end
it "should load images in css by default" do
browser.visit("http://#{@host}:#{@port}/")
@received_requests.find {|r| r =~ %r{/path/to/image} }.should_not be_nil
end
it "should not load images in image tags when skip_image_loading is true" do
browser_skip_images.visit("http://#{@host}:#{@port}/")
@received_requests.find {|r| r =~ %r{/path/to/image} }.should be_nil
end
it "should not load images in css when skip_image_loading is true" do
browser_skip_images.visit("http://#{@host}:#{@port}/")
@received_requests.find {|r| r =~ %r{/path/to/bgimage} }.should be_nil
end
end
describe "forking", :skip_on_windows => true do
it "only shuts down the server from the main process" do
browser.reset!
pid = fork {}
Process.wait(pid)
expect { browser.reset! }.not_to raise_error
end
end
describe '#set_proxy' do
before do
@host = '127.0.0.1'
@user = 'user'
@pass = 'secret'
@url = "http://example.org/"
@server = TCPServer.new(@host, 0)
@port = @server.addr[1]
@proxy_requests = []
@proxy = Thread.new(@server, @proxy_requests) do |serv, proxy_requests|
while conn = serv.accept do
# read request
request = []
until (line = conn.readline.strip).empty?
request << line
end
# send response
auth_header = request.find { |h| h =~ /Authorization:/i }
if auth_header || request[0].split(/\s+/)[1] =~ /^\//
html = "<html><body>D'oh!</body></html>"
conn.write "HTTP/1.1 200 OK\r\n"
conn.write "Content-Type:text/html\r\n"
conn.write "Content-Length: %i\r\n" % html.size
conn.write "\r\n"
conn.write html
conn.close
proxy_requests << request if auth_header
else
conn.write "HTTP/1.1 407 Proxy Auth Required\r\n"
conn.write "Proxy-Authenticate: Basic realm=\"Proxy\"\r\n"
conn.write "\r\n"
conn.close
proxy_requests << request
end
end
end
browser.set_proxy(:host => @host,
:port => @port,
:user => @user,
:pass => @pass)
browser.visit @url
@proxy_requests.size.should == 2
@request = @proxy_requests[-1]
end
after do
@proxy.kill
@server.close
end
it 'uses the HTTP proxy correctly' do
@request[0].should match /^GET\s+http:\/\/example.org\/\s+HTTP/i
@request.find { |header|
header =~ /^Host:\s+example.org$/i }.should_not be nil
end
it 'sends correct proxy authentication' do
auth_header = @request.find { |header|
header =~ /^Proxy-Authorization:\s+/i }
auth_header.should_not be nil
user, pass = Base64.decode64(auth_header.split(/\s+/)[-1]).split(":")
user.should == @user
pass.should == @pass
end
it "uses the proxies' response" do
browser.body.should include "D'oh!"
end
it 'uses original URL' do
browser.url.should == @url
end
it 'uses URLs changed by javascript' do
browser.execute_script "window.history.pushState('', '', '/blah')"
browser.requested_url.should == 'http://example.org/blah'
end
it 'is possible to disable proxy again' do
@proxy_requests.clear
browser.clear_proxy
browser.visit "http://#{@host}:#{@port}/"
@proxy_requests.size.should == 0
end
end
it "doesn't try to read an empty response" do
connection = stub("connection")
connection.stub(:puts)
connection.stub(:print)
connection.stub(:gets).and_return("ok\n", "0\n")
connection.stub(:read).and_raise(StandardError.new("tried to read empty response"))
browser = Capybara::Webkit::Browser.new(connection)
expect { browser.visit("/") }.not_to raise_error(/empty response/)
end
end