proxy_tester 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +13 -0
- data/lib/proxy_tester.rb +1 -0
- data/lib/proxy_tester/capybara_proxy_pac.rb +18 -13
- data/lib/proxy_tester/pac_result.rb +8 -3
- data/lib/proxy_tester/rspec/helper.rb +26 -18
- data/lib/proxy_tester/rspec/matchers/return_proxy.rb +18 -0
- data/lib/proxy_tester/version.rb +1 -1
- data/spec/capybara_proxy_pac_spec.rb +17 -0
- data/spec/matchers/returns_proxy_spec.rb +42 -0
- data/spec/pac_result_spec.rb +6 -0
- data/spec/rspec_helper_spec.rb +8 -0
- metadata +4 -1
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -596,6 +596,19 @@ it 'blocks www.example.org' do
|
|
596
596
|
expect(page.status_code).to eq 403
|
597
597
|
end
|
598
598
|
```
|
599
|
+
* set_offline
|
600
|
+
|
601
|
+
Disable online activities issued by visit.
|
602
|
+
|
603
|
+
```ruby
|
604
|
+
it 'does not send out requests if is offline' do
|
605
|
+
set_offline true
|
606
|
+
visit 'http://www.example.org'
|
607
|
+
|
608
|
+
expect(page).to eq "<html><head></head><body></body></html>"
|
609
|
+
end
|
610
|
+
|
611
|
+
* offline?
|
599
612
|
|
600
613
|
### Custom matchers
|
601
614
|
|
data/lib/proxy_tester.rb
CHANGED
@@ -1,18 +1,19 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
module ProxyTester
|
3
3
|
class CapybaraProxyPac
|
4
|
+
private
|
4
5
|
|
5
|
-
|
6
|
+
attr_reader :content
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
public
|
9
|
+
|
10
|
+
attr_accessor :client_ip, :time, :pac_file, :url, :result
|
9
11
|
|
12
|
+
def host
|
10
13
|
result.proxy
|
11
14
|
end
|
12
15
|
|
13
16
|
def port
|
14
|
-
@result ||= result
|
15
|
-
|
16
17
|
result.proxy_port
|
17
18
|
end
|
18
19
|
|
@@ -21,29 +22,33 @@ module ProxyTester
|
|
21
22
|
end
|
22
23
|
|
23
24
|
def direct?
|
24
|
-
@result ||= result
|
25
|
-
|
26
25
|
result.request_type == 'DIRECT'
|
27
26
|
end
|
28
27
|
|
28
|
+
def verbatim
|
29
|
+
result.verbatim
|
30
|
+
end
|
31
|
+
|
29
32
|
def pac_file=(source)
|
33
|
+
@pac_file = source
|
34
|
+
|
30
35
|
uri = Addressable::URI.heuristic_parse(source)
|
31
36
|
|
32
37
|
if uri.host.blank?
|
33
|
-
@
|
38
|
+
@content = File.read(source)
|
34
39
|
else
|
35
|
-
@
|
40
|
+
@content = open(uri, { proxy: false })
|
36
41
|
end
|
37
42
|
|
38
|
-
@
|
43
|
+
@content
|
39
44
|
rescue Errno::ENOENT
|
40
|
-
raise Exceptions::PacFileNotFound, JSON.dump(file:
|
45
|
+
raise Exceptions::PacFileNotFound, JSON.dump(file: source)
|
41
46
|
end
|
42
47
|
|
43
48
|
private
|
44
49
|
|
45
50
|
def result
|
46
|
-
return PacResult.new if
|
51
|
+
return PacResult.new if content.blank?
|
47
52
|
|
48
53
|
env_hash = {}
|
49
54
|
env_hash[:client_ip] = client_ip if client_ip
|
@@ -51,7 +56,7 @@ module ProxyTester
|
|
51
56
|
|
52
57
|
parser = ProxyPacRb::Parser.new(ProxyPacRb::Environment.new(env_hash))
|
53
58
|
|
54
|
-
file = parser.source(
|
59
|
+
file = parser.source(content)
|
55
60
|
result = PacResult.new(file.find(url))
|
56
61
|
|
57
62
|
raise Exceptions::PacResultResult, JSON.dump(file: pac_file, result: result) if result.blank?
|
@@ -4,12 +4,13 @@ module ProxyTester
|
|
4
4
|
|
5
5
|
attr_reader :proxy, :proxy_port, :request_type
|
6
6
|
|
7
|
-
def initialize(
|
7
|
+
def initialize(verbatim = '')
|
8
|
+
@verbatim = verbatim
|
8
9
|
|
9
|
-
if
|
10
|
+
if verbatim.blank?
|
10
11
|
parsed_content = {}
|
11
12
|
else
|
12
|
-
parsed_content = parse(
|
13
|
+
parsed_content = parse(verbatim) || {}
|
13
14
|
end
|
14
15
|
|
15
16
|
@proxy = parsed_content[:proxy]
|
@@ -17,6 +18,10 @@ module ProxyTester
|
|
17
18
|
@request_type = parsed_content[:request_type]
|
18
19
|
end
|
19
20
|
|
21
|
+
def verbatim
|
22
|
+
@verbatim.to_s
|
23
|
+
end
|
24
|
+
|
20
25
|
private
|
21
26
|
|
22
27
|
def parse(string)
|
@@ -12,12 +12,24 @@ module ProxyTester
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
+
def offline
|
16
|
+
@__offline ||= false
|
17
|
+
end
|
18
|
+
|
19
|
+
def offline=(status)
|
20
|
+
if status == true
|
21
|
+
@__offline = true
|
22
|
+
else
|
23
|
+
@__offline = false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
15
27
|
def proxy
|
16
|
-
@__proxy
|
28
|
+
@__proxy ||= ProxyTester::CapybaraProxy.new
|
17
29
|
end
|
18
30
|
|
19
31
|
def proxy_pac
|
20
|
-
@__proxy_pac
|
32
|
+
@__proxy_pac ||= ProxyTester::CapybaraProxyPac.new
|
21
33
|
end
|
22
34
|
|
23
35
|
def keep_report_directories
|
@@ -44,9 +56,6 @@ module ProxyTester
|
|
44
56
|
end
|
45
57
|
|
46
58
|
def visit(url)
|
47
|
-
@__proxy ||= ProxyTester::CapybaraProxy.new
|
48
|
-
@__proxy_pac ||= ProxyTester::CapybaraProxyPac.new
|
49
|
-
|
50
59
|
proxy_pac.url = url
|
51
60
|
|
52
61
|
if !proxy_pac.blank? and !proxy_pac.direct?
|
@@ -58,6 +67,8 @@ module ProxyTester
|
|
58
67
|
use_driver proxy
|
59
68
|
use_user_agent 'Mozilla/5.0 (X11; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0'
|
60
69
|
|
70
|
+
return if offline?
|
71
|
+
|
61
72
|
begin
|
62
73
|
super(url)
|
63
74
|
rescue ::Capybara::Poltergeist::TimeoutError
|
@@ -67,8 +78,6 @@ module ProxyTester
|
|
67
78
|
end
|
68
79
|
|
69
80
|
def use_user(name, options = nil)
|
70
|
-
@__proxy ||= ProxyTester::CapybaraProxy.new
|
71
|
-
|
72
81
|
if name == :ask
|
73
82
|
user_name = HighLine.new.ask('Please enter username: ')
|
74
83
|
user_password = HighLine.new.ask('Please enter user password: ') { |q| q.echo = '*' }
|
@@ -93,21 +102,14 @@ module ProxyTester
|
|
93
102
|
end
|
94
103
|
|
95
104
|
def use_client_ip(ip)
|
96
|
-
@__proxy_pac ||= ProxyTester::CapybaraProxyPac.new
|
97
|
-
|
98
105
|
proxy_pac.client_ip = ip
|
99
106
|
end
|
100
107
|
|
101
108
|
def use_time(time)
|
102
|
-
@__proxy_pac ||= ProxyTester::CapybaraProxyPac.new
|
103
|
-
|
104
109
|
proxy_pac.time = time
|
105
110
|
end
|
106
111
|
|
107
112
|
def use_proxy(*args)
|
108
|
-
@__proxy ||= ProxyTester::CapybaraProxy.new
|
109
|
-
@__proxy_pac ||= ProxyTester::CapybaraProxyPac.new
|
110
|
-
|
111
113
|
if args.first.kind_of? Symbol
|
112
114
|
case args.first
|
113
115
|
when :host
|
@@ -133,6 +135,16 @@ module ProxyTester
|
|
133
135
|
::Capybara.default_wait_time = old_timeout
|
134
136
|
end
|
135
137
|
|
138
|
+
def offline?
|
139
|
+
return true if offline == true
|
140
|
+
|
141
|
+
false
|
142
|
+
end
|
143
|
+
|
144
|
+
def set_offline(status)
|
145
|
+
self.offline = status
|
146
|
+
end
|
147
|
+
|
136
148
|
private
|
137
149
|
|
138
150
|
def use_driver(proxy)
|
@@ -216,10 +228,6 @@ end
|
|
216
228
|
RSpec.configure do |c|
|
217
229
|
c.include ProxyTester::SpecHelper::Capybara
|
218
230
|
|
219
|
-
c.before :each do
|
220
|
-
@__proxy = ProxyTester::CapybaraProxy.new
|
221
|
-
end
|
222
|
-
|
223
231
|
c.before :all do
|
224
232
|
cleanup_reports
|
225
233
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
RSpec::Matchers.define :return_proxy do |expected|
|
3
|
+
match do |actual|
|
4
|
+
actual.verbatim == expected
|
5
|
+
end
|
6
|
+
|
7
|
+
failure_message_for_should do |actual|
|
8
|
+
"expected that \"#{actual.pac_file}\" returns \"#{expected}\", but it returns \"#{actual.verbatim}\" as proxy."
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message_for_should_not do |actual|
|
12
|
+
"expected that \"#{actual.pac_file}\" not returns \"#{expected}\", but it returns \"#{actual.verbatim}\" as proxy."
|
13
|
+
end
|
14
|
+
|
15
|
+
description do
|
16
|
+
"to return proxy \"#{expected}\"."
|
17
|
+
end
|
18
|
+
end
|
data/lib/proxy_tester/version.rb
CHANGED
@@ -39,4 +39,21 @@ describe CapybaraProxyPac do
|
|
39
39
|
expect(pac.port).to eq('3128')
|
40
40
|
end
|
41
41
|
end
|
42
|
+
|
43
|
+
context '#verbatim' do
|
44
|
+
it 'verbatim output' do
|
45
|
+
file = create_file 'proxy.pac', valid_pac_file1
|
46
|
+
|
47
|
+
pac = CapybaraProxyPac.new
|
48
|
+
pac.pac_file = file
|
49
|
+
pac.url = 'http://www.heise.de'
|
50
|
+
|
51
|
+
expect(pac.verbatim).to eq('PROXY localhost:3128')
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'handles not set file' do
|
55
|
+
pac = CapybaraProxyPac.new
|
56
|
+
expect { pac.verbatim }.not_to raise_error
|
57
|
+
end
|
58
|
+
end
|
42
59
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'returns proxy matcher' do
|
5
|
+
let(:valid_proxy_pac) {
|
6
|
+
<<-EOS.strip_heredoc
|
7
|
+
function FindProxyForURL(url, host) {
|
8
|
+
if (url == "http://example.com") {
|
9
|
+
return "PROXY localhost2:8080";
|
10
|
+
} else {
|
11
|
+
return "DIRECT";
|
12
|
+
}
|
13
|
+
}
|
14
|
+
EOS
|
15
|
+
}
|
16
|
+
|
17
|
+
before :each do
|
18
|
+
set_offline true
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'checks return proxy string' do
|
22
|
+
file = create_file 'proxy.pac', valid_proxy_pac
|
23
|
+
use_proxy :pac, file
|
24
|
+
|
25
|
+
visit 'http://example.com'
|
26
|
+
expect(proxy_pac).to return_proxy('PROXY localhost2:8080')
|
27
|
+
|
28
|
+
visit 'http://otherdomain.com'
|
29
|
+
expect(proxy_pac).to return_proxy('DIRECT')
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'checks that a proxy string is not returned' do
|
33
|
+
file = create_file 'proxy.pac', valid_proxy_pac
|
34
|
+
use_proxy :pac, file
|
35
|
+
|
36
|
+
visit 'http://example.com'
|
37
|
+
expect(proxy_pac).not_to return_proxy('DIRECT')
|
38
|
+
|
39
|
+
visit 'http://otherdomain.com'
|
40
|
+
expect(proxy_pac).not_to return_proxy('PROXY localhost2:8080')
|
41
|
+
end
|
42
|
+
end
|
data/spec/pac_result_spec.rb
CHANGED
@@ -10,6 +10,12 @@ describe PacResult do
|
|
10
10
|
expect(result.request_type).to eq('PROXY')
|
11
11
|
end
|
12
12
|
|
13
|
+
it 'returns verbatim' do
|
14
|
+
string = 'PROXY localhost:3128'
|
15
|
+
result = PacResult.new(string)
|
16
|
+
expect(result.verbatim).to eq(string)
|
17
|
+
end
|
18
|
+
|
13
19
|
it 'parses pac result' do
|
14
20
|
result = PacResult.new('DIRECT')
|
15
21
|
|
data/spec/rspec_helper_spec.rb
CHANGED
@@ -167,4 +167,12 @@ describe 'Spec helper' do
|
|
167
167
|
expect(proxy.user.password).to eq('my_password')
|
168
168
|
end
|
169
169
|
end
|
170
|
+
|
171
|
+
context '#offline' do
|
172
|
+
it 'disable requests to website' do
|
173
|
+
set_offline true
|
174
|
+
visit 'http://www.asdfasdfasdfasdfasdfsadfsadfsdaf1231234567890123456789.de'
|
175
|
+
expect(page.source).to eq '<html><head></head><body></body></html>'
|
176
|
+
end
|
177
|
+
end
|
170
178
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: proxy_tester
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -327,6 +327,7 @@ files:
|
|
327
327
|
- lib/proxy_tester/rspec/helper.rb
|
328
328
|
- lib/proxy_tester/rspec/matchers/have_requests_with_status_code.rb
|
329
329
|
- lib/proxy_tester/rspec/matchers/reference_domains.rb
|
330
|
+
- lib/proxy_tester/rspec/matchers/return_proxy.rb
|
330
331
|
- lib/proxy_tester/rspec_runner.rb
|
331
332
|
- lib/proxy_tester/template_file.rb
|
332
333
|
- lib/proxy_tester/template_repository.rb
|
@@ -365,6 +366,7 @@ files:
|
|
365
366
|
- spec/main_spec.rb
|
366
367
|
- spec/matchers/have_requests_with_status_code_spec.rb
|
367
368
|
- spec/matchers/reference_domains_spec.rb
|
369
|
+
- spec/matchers/returns_proxy_spec.rb
|
368
370
|
- spec/pac_result_spec.rb
|
369
371
|
- spec/remote_repository_spec.rb
|
370
372
|
- spec/reporters/fetch_urls_spec.rb
|
@@ -443,6 +445,7 @@ test_files:
|
|
443
445
|
- spec/main_spec.rb
|
444
446
|
- spec/matchers/have_requests_with_status_code_spec.rb
|
445
447
|
- spec/matchers/reference_domains_spec.rb
|
448
|
+
- spec/matchers/returns_proxy_spec.rb
|
446
449
|
- spec/pac_result_spec.rb
|
447
450
|
- spec/remote_repository_spec.rb
|
448
451
|
- spec/reporters/fetch_urls_spec.rb
|