proxy_pac_rb 0.4.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -3
  3. data/CONTRIBUTING.md +61 -0
  4. data/Gemfile +2 -2
  5. data/Gemfile.lock +6 -14
  6. data/README.md +319 -14
  7. data/Rakefile +2 -96
  8. data/features/init_proxy_pac.feature +47 -0
  9. data/features/resolve_proxy.feature +1 -1
  10. data/features/step_definitions.rb +11 -0
  11. data/lib/proxy_pac_rb/cli/compress_proxy_pac.rb +1 -1
  12. data/lib/proxy_pac_rb/cli/find_proxy.rb +2 -0
  13. data/lib/proxy_pac_rb/cli/init.rb +10 -0
  14. data/lib/proxy_pac_rb/cli/init_proxy_pac.rb +98 -0
  15. data/lib/proxy_pac_rb/cli/lint.rb +0 -1
  16. data/lib/proxy_pac_rb/cli/lint_proxy_pac.rb +0 -1
  17. data/lib/proxy_pac_rb/cli/runner.rb +3 -0
  18. data/lib/proxy_pac_rb/environment.rb +13 -10
  19. data/lib/proxy_pac_rb/errors.rb +4 -0
  20. data/lib/proxy_pac_rb/javascript_compiler.rb +29 -0
  21. data/lib/proxy_pac_rb/main.rb +6 -0
  22. data/lib/proxy_pac_rb/parser.rb +2 -7
  23. data/lib/proxy_pac_rb/proxy_pac_file.rb +53 -5
  24. data/lib/proxy_pac_rb/proxy_pac_linter.rb +11 -5
  25. data/lib/proxy_pac_rb/proxy_pac_loader.rb +38 -5
  26. data/lib/proxy_pac_rb/proxy_pac_parser.rb +9 -21
  27. data/lib/proxy_pac_rb/rack/proxy_pac_compressor.rb +3 -3
  28. data/lib/proxy_pac_rb/rack/proxy_pac_linter.rb +3 -3
  29. data/lib/proxy_pac_rb/rspec/helpers/.keep +0 -0
  30. data/lib/proxy_pac_rb/rspec/helpers.rb +52 -0
  31. data/lib/proxy_pac_rb/rspec/matchers/.keep +0 -0
  32. data/lib/proxy_pac_rb/rspec/matchers/proxy.rb +23 -0
  33. data/lib/proxy_pac_rb/rspec/matchers/url.rb +13 -0
  34. data/lib/proxy_pac_rb/rspec/shared_contexts/.keep +0 -0
  35. data/lib/proxy_pac_rb/rspec/shared_examples/.keep +0 -0
  36. data/lib/proxy_pac_rb/rspec.rb +18 -0
  37. data/lib/proxy_pac_rb/version.rb +1 -1
  38. data/lib/proxy_pac_rb.rb +5 -1
  39. data/script/config.ru +8 -0
  40. data/spec/api/proxy_pac_compressor_spec.rb +31 -0
  41. data/spec/api/proxy_pac_dumper_spec.rb +67 -0
  42. data/spec/api/proxy_pac_file_spec.rb +90 -0
  43. data/spec/api/proxy_pac_linter_spec.rb +61 -0
  44. data/spec/api/proxy_pac_loader_spec.rb +88 -0
  45. data/spec/api/proxy_pac_parser_spec.rb +56 -0
  46. data/spec/environment_spec.rb +121 -78
  47. data/spec/parser_spec.rb +1 -1
  48. data/spec/rspec/compare_proxy_pac_files_spec.rb +62 -0
  49. data/spec/rspec/parse_proxy_pac_spec.rb +88 -0
  50. data/spec/rspec/readability_spec.rb +57 -0
  51. data/spec/rspec/rspec_spec.rb +7 -0
  52. data/spec/rspec/validitiy_spec.rb +48 -0
  53. data/spec/support/aruba.rb +0 -27
  54. data/spec/support/shared_examples/loader.rb +27 -0
  55. data/templates/build/middleman/config.rb +12 -0
  56. data/templates/build/middleman/script/build +2 -0
  57. data/templates/build/middleman/source/.keep +0 -0
  58. data/templates/default/.gitignore +24 -0
  59. data/templates/default/Gemfile +3 -0
  60. data/templates/default/Rakefile +1 -0
  61. data/templates/default/script/bootstrap +22 -0
  62. data/templates/new_proxy_pac.pac.erb +3 -0
  63. data/templates/test_framework/rspec/spec_helper.rb +9 -0
  64. data/templates/test_framework/rspec/support/aruba.rb +23 -0
  65. data/templates/test_framework/rspec/support/matchers/shared_contexts/.keep +0 -0
  66. data/templates/test_framework/rspec/support/proxy_pac_rb.rb +1 -0
  67. data/templates/test_framework/rspec/support/rspec.rb +20 -0
  68. data/templates/test_framework/rspec/support/shared_contexts/.keep +0 -0
  69. data/templates/test_framework/rspec/support/shared_examples/.keep +0 -0
  70. metadata +56 -6
  71. data/lib/proxy_pac_rb/proxy_pac.rb +0 -24
  72. data/script/test_web +0 -16
  73. data/spec/api_spec.rb +0 -207
@@ -4,38 +4,26 @@ module ProxyPacRb
4
4
  class ProxyPacParser
5
5
  private
6
6
 
7
- attr_reader :environment, :runtime
7
+ attr_reader :environment, :runtime, :compiler
8
8
 
9
9
  public
10
10
 
11
11
  def initialize(
12
12
  environment: Environment.new,
13
- runtime: Runtimes.autodetect
13
+ compiler: JavascriptCompiler.new
14
14
  )
15
- @runtime = runtime
16
15
  @environment = environment
16
+ @compiler = compiler
17
17
  end
18
18
 
19
19
  def parse(proxy_pac)
20
- fail Exceptions::RuntimeUnavailable, "#{runtime.name} is unavailable on this system" unless runtime.available?
20
+ return unless proxy_pac.valid?
21
21
 
22
- ProxyPac.new(
23
- javascript: compile_javascript(proxy_pac.content),
24
- file: proxy_pac
25
- )
26
- end
27
-
28
- private
29
-
30
- def compile_javascript(content)
31
- environment.prepare(content)
32
-
33
- context = runtime.compile(content)
34
- context.include environment
35
-
36
- Javascript.new(context)
37
- rescue StandardError => err
38
- raise ParserError, err.message
22
+ proxy_pac.javascript = compiler.compile(content: proxy_pac.content, environment: environment)
23
+ proxy_pac.parsable = true
24
+ rescue => err
25
+ proxy_pac.parsable = false
26
+ proxy_pac.message = err.message
39
27
  end
40
28
  end
41
29
  end
@@ -38,10 +38,10 @@ module ProxyPacRb
38
38
  status, headers, body = @app.call(env)
39
39
 
40
40
  return [status, headers, body] if enabled == false
41
- # rubocop:disable Style/CaseEquality
41
+ # rubocop:disable Style/SpaceAroundOperators
42
42
  return [status, headers, body] unless headers.key?('Content-Type') \
43
- && %r{application/x-ns-proxy-autoconfig} === headers['Content-Type']
44
- # rubocop:enable Style/CaseEquality
43
+ && %r{application/x-ns-proxy-autoconfig} === headers['Content-Type']
44
+ # rubocop:enabled Style/SpaceAroundOperators
45
45
 
46
46
  content = ''
47
47
  body.each { |part| content << part }
@@ -37,10 +37,10 @@ module ProxyPacRb
37
37
  status, headers, body = @app.call(env)
38
38
 
39
39
  return [status, headers, body] if enabled == false
40
- # rubocop:disable Style/CaseEquality
40
+ # rubocop:disable Style/SpaceAroundOperators
41
41
  return [status, headers, body] unless headers.key?('Content-Type') \
42
- && %r{application/x-ns-proxy-autoconfig} === headers['Content-Type']
43
- # rubocop:enable Style/CaseEquality
42
+ && %r{application/x-ns-proxy-autoconfig} === headers['Content-Type']
43
+ # rubocop:enabled Style/SpaceAroundOperators
44
44
 
45
45
  content = ''
46
46
  body.each { |part| content << part }
File without changes
@@ -0,0 +1,52 @@
1
+ module ProxyPacRb
2
+ module Rspec
3
+ # Helpers for proxy.pac tests
4
+ module Helpers
5
+ def proxy_pac
6
+ source = if subject.nil? || Addressable::URI.parse(subject).host || /FindProxyForURL/ === subject
7
+ subject
8
+ else
9
+ File.join(root_path, subject)
10
+ end
11
+
12
+ file = ProxyPacRb::ProxyPacFile.new(source: source)
13
+
14
+ _proxy_pac_loader.load(file)
15
+ _proxy_pac_linter.lint(file)
16
+ _proxy_pac_parser.parse(file)
17
+
18
+ file
19
+ end
20
+
21
+ def time
22
+ '1970-01-01 00:00:00'
23
+ end
24
+
25
+ def client_ip
26
+ '127.0.0.1'
27
+ end
28
+
29
+ def root_path
30
+ @root_path ||= Dir.getwd
31
+ end
32
+
33
+ private
34
+
35
+ def _environment
36
+ Environment.new(time: time, client_ip: client_ip)
37
+ end
38
+
39
+ def _proxy_pac_parser
40
+ ProxyPacRb::ProxyPacParser.new(environment: _environment)
41
+ end
42
+
43
+ def _proxy_pac_loader
44
+ ProxyPacRb::ProxyPacLoader.new
45
+ end
46
+
47
+ def _proxy_pac_linter
48
+ ProxyPacRb::ProxyPacLinter.new
49
+ end
50
+ end
51
+ end
52
+ end
File without changes
@@ -0,0 +1,23 @@
1
+ RSpec::Matchers.define :be_the_same_proxy_pac_file do |expected|
2
+ define_method :loader do
3
+ ProxyPacRb::ProxyPacLoader.new
4
+ end
5
+
6
+ match do |actual|
7
+ @file_a = ProxyPacRb::ProxyPacFile.new(source: actual)
8
+ loader.load(@file_a)
9
+
10
+ @file_b = ProxyPacRb::ProxyPacFile.new(source: expected)
11
+ loader.load(@file_b)
12
+
13
+ @file_a == @file_b
14
+ end
15
+
16
+ failure_message do
17
+ format(%(expected that proxy.pac "%s" is equal to proxy.pac "%s", but it is not equal.\n\nActual:\n%s\n\nExpected:\n%s), @file_a.source, @file_b.source, @file_a.content, @file_b.content)
18
+ end
19
+
20
+ failure_message_when_negated do
21
+ format(%(expected that proxy.pac "%s" is not equal to proxy.pac "%s", but it is equal.\n\nActual:\n%s\n\nExpected:\n%s), @file_a.source, @file_b.source, @file_a.content, @file_b.content)
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ RSpec::Matchers.define :be_downloaded_via do |expected|
2
+ match do |actual|
3
+ proxy_pac.find(actual) == expected
4
+ end
5
+
6
+ failure_message do |actual|
7
+ format(%(expected that url "%s" is downloaded via "%s", but it is downloaded via "%s".), actual, expected, proxy_pac.find(actual))
8
+ end
9
+
10
+ failure_message_when_negated do |actual|
11
+ format(%(expected that url "%s" is not downloaded via "%s", but it is downloaded via "%s".), actual, expected, proxy_pac.find(actual))
12
+ end
13
+ end
File without changes
File without changes
@@ -0,0 +1,18 @@
1
+ require 'proxy_pac_rb'
2
+ require 'rspec'
3
+
4
+ require 'proxy_pac_rb/rspec/helpers'
5
+ ProxyPacRb.require_file_matching_pattern('rspec/matchers/*.rb')
6
+ ProxyPacRb.require_file_matching_pattern('rspec/shared_examples/*.rb')
7
+ ProxyPacRb.require_file_matching_pattern('rspec/shared_contexts/*.rb')
8
+
9
+ # Main Module
10
+ module ProxyPacRb
11
+ # Main Module
12
+ module Rspec
13
+ end
14
+ end
15
+
16
+ RSpec.configure do |config|
17
+ config.include ProxyPacRb::Rspec::Helpers, type: :proxy_pac
18
+ end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
  # ProxyPacRb
3
3
  module ProxyPacRb
4
- VERSION = '0.4.2'
4
+ VERSION = '0.5.0'
5
5
  end
data/lib/proxy_pac_rb.rb CHANGED
@@ -7,7 +7,9 @@ require 'uglifier'
7
7
  require 'optparse'
8
8
  require 'ostruct'
9
9
  require 'resolv'
10
+ require 'net/http'
10
11
  require 'time'
12
+ require 'timeout'
11
13
 
12
14
  require 'proxy_pac_rb/version'
13
15
  require 'proxy_pac_rb/main'
@@ -15,6 +17,7 @@ require 'proxy_pac_rb/errors'
15
17
  require 'proxy_pac_rb/encoding'
16
18
  require 'proxy_pac_rb/exceptions'
17
19
  require 'proxy_pac_rb/environment'
20
+ require 'proxy_pac_rb/javascript_compiler'
18
21
  require 'proxy_pac_rb/runtime'
19
22
  require 'proxy_pac_rb/runtimes/rubyracer'
20
23
  require 'proxy_pac_rb/runtimes/rubyrhino'
@@ -22,7 +25,6 @@ require 'proxy_pac_rb/runtimes'
22
25
  require 'proxy_pac_rb/parser'
23
26
  require 'proxy_pac_rb/javascript'
24
27
  require 'proxy_pac_rb/proxy_pac_js'
25
- require 'proxy_pac_rb/proxy_pac'
26
28
  require 'proxy_pac_rb/proxy_pac_file'
27
29
  require 'proxy_pac_rb/proxy_pac_template'
28
30
  require 'proxy_pac_rb/proxy_pac_compressor'
@@ -35,6 +37,8 @@ require 'proxy_pac_rb/cli_validator'
35
37
 
36
38
  require 'proxy_pac_rb/cli/shared'
37
39
  require 'proxy_pac_rb/cli/find_proxy'
40
+ require 'proxy_pac_rb/cli/init_proxy_pac'
41
+ require 'proxy_pac_rb/cli/init'
38
42
  require 'proxy_pac_rb/cli/lint_proxy_pac'
39
43
  require 'proxy_pac_rb/cli/lint'
40
44
  require 'proxy_pac_rb/cli/compress_proxy_pac'
data/script/config.ru ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler'
4
+ Bundler.require :default, :test, :development
5
+
6
+ require 'rack/directory'
7
+
8
+ run Rack::Directory.new(File.expand_path('../../tmp/cucumber', __FILE__))
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ProxyPacCompressor do
4
+ subject(:proxy_pac) { instance_double('ProxyPac::ProxyPacFile') }
5
+
6
+ let(:compressor) { described_class.new }
7
+ let(:modified_content) { %(function FindProxyForURL(){return"DIRECT"}) }
8
+
9
+ let(:content) do
10
+ <<-EOS.strip_heredoc.chomp
11
+ function FindProxyForURL(url, host) {
12
+ return "DIRECT";
13
+ }
14
+ EOS
15
+ end
16
+
17
+ before :each do
18
+ allow(proxy_pac).to receive(:content).and_return(content)
19
+ allow(proxy_pac).to receive(:type?).with(:string).and_return(true)
20
+ end
21
+
22
+ before :each do
23
+ expect(proxy_pac).to receive(:content=).with(modified_content)
24
+ end
25
+
26
+ describe '#modify' do
27
+ context 'when string contains white paces' do
28
+ it { compressor.compress(proxy_pac) }
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ProxyPacDumper do
4
+ subject(:proxy_pac) { instance_double('ProxyPac::ProxyPacFile') }
5
+
6
+ let(:content) do
7
+ <<-EOS.strip_heredoc.chomp
8
+ function FindProxyForURL(url, host) {
9
+ return "DIRECT";
10
+ }
11
+ EOS
12
+ end
13
+
14
+ let(:source) do
15
+ <<-EOS.strip_heredoc.chomp
16
+ function FindProxyForURL(url, host) {
17
+ return "DIRECT";
18
+ }
19
+ EOS
20
+ end
21
+
22
+ before :each do
23
+ allow(proxy_pac).to receive(:content).and_return(content)
24
+ allow(proxy_pac).to receive(:source).and_return(source)
25
+ end
26
+
27
+ let(:dumper) { ProxyPacDumper.new }
28
+ let(:destination) { absolute_path('proxy.pac') }
29
+
30
+ describe '#dump' do
31
+ before :each do
32
+ allow(proxy_pac).to receive(:source).and_return(source)
33
+ end
34
+
35
+ context 'when proxy pac is string' do
36
+ before :each do
37
+ in_current_dir do
38
+ dumper.dump(proxy_pac, type: :string)
39
+ end
40
+ end
41
+
42
+ it { expect(destination).to be_existing_file }
43
+ it { expect(destination).to have_content proxy_pac.content }
44
+ end
45
+
46
+ context 'when proxy pac is file' do
47
+ let(:source) { 'proxy.pac.in' }
48
+
49
+ before :each do
50
+ write_file(source, content)
51
+ end
52
+
53
+ before :each do
54
+ in_current_dir do
55
+ dumper.dump(proxy_pac, type: :template)
56
+ end
57
+ end
58
+
59
+ around :example do |example|
60
+ in_current_dir { example.call }
61
+ end
62
+
63
+ it { expect(destination).to be_existing_file }
64
+ it { expect(destination).to have_content proxy_pac.content }
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ProxyPacFile do
4
+ subject(:proxy_pac) { ProxyPacFile.new source: source }
5
+
6
+ let(:source) do
7
+ <<-EOS.strip_heredoc.chomp
8
+ function FindProxyForURL(url, host) {
9
+ return "DIRECT";
10
+ }
11
+ EOS
12
+ end
13
+
14
+ describe '#initalize' do
15
+ context 'when source is ProxyPacFile' do
16
+ before :each do
17
+ proxy_pac.content = source
18
+ proxy_pac.javascript = BasicObject.new
19
+ proxy_pac.message = 'message'
20
+ proxy_pac.parsable = true
21
+ proxy_pac.readable = true
22
+ proxy_pac.type = :string
23
+ proxy_pac.valid = true
24
+ end
25
+
26
+ let(:proxy_pac_2) { ProxyPacFile.new(source: proxy_pac) }
27
+
28
+ it { expect(proxy_pac.content).to eq proxy_pac_2.content }
29
+ it { expect(proxy_pac.javascript).to eq proxy_pac_2.javascript }
30
+ it { expect(proxy_pac.message).to eq proxy_pac_2.message }
31
+ it { expect(proxy_pac.parsable).to eq proxy_pac_2.parsable }
32
+ it { expect(proxy_pac.readable).to eq proxy_pac_2.readable }
33
+ it { expect(proxy_pac.source).to eq proxy_pac_2.source }
34
+ it { expect(proxy_pac.type).to eq proxy_pac_2.type }
35
+ it { expect(proxy_pac.valid).to eq proxy_pac_2.valid }
36
+ end
37
+ end
38
+
39
+ describe '#content' do
40
+ let(:content) { 'content' }
41
+ before(:each) { proxy_pac.content = content }
42
+
43
+ context 'when is "content"' do
44
+ it { expect(proxy_pac.content).to eq content }
45
+ end
46
+
47
+ context 'when is "content"' do
48
+ let(:content) { nil }
49
+ it { expect(proxy_pac.content).to eq nil }
50
+ end
51
+ end
52
+
53
+ describe '#content?' do
54
+ let(:content) { 'content' }
55
+
56
+ context 'when has content' do
57
+ before(:each) { proxy_pac.content = content }
58
+ it { expect(proxy_pac).to be_content }
59
+ end
60
+
61
+ context 'when does not have content' do
62
+ it { expect(proxy_pac).not_to be_content }
63
+ end
64
+ end
65
+
66
+ describe '#valid?' do
67
+ context 'when is invalid' do
68
+ it { expect(proxy_pac).not_to be_valid }
69
+ end
70
+
71
+ context 'when is valid' do
72
+ before(:each) { proxy_pac.readable = true }
73
+ before(:each) { proxy_pac.valid = true }
74
+
75
+ it { expect(proxy_pac).to be_valid }
76
+ end
77
+ end
78
+
79
+ describe '#readable?' do
80
+ context 'when is invalid' do
81
+ it { expect(proxy_pac).not_to be_readable }
82
+ end
83
+
84
+ context 'when is valid' do
85
+ before(:each) { proxy_pac.readable = true }
86
+
87
+ it { expect(proxy_pac).to be_readable }
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ProxyPacLinter do
4
+ subject(:proxy_pac) { instance_double('ProxyPac::ProxyPacFile') }
5
+
6
+ let(:content) do
7
+ <<-EOS.strip_heredoc.chomp
8
+ function FindProxyForURL(url, host) {
9
+ return "DIRECT";
10
+ }
11
+ EOS
12
+ end
13
+
14
+ let(:source) do
15
+ <<-EOS.strip_heredoc.chomp
16
+ function FindProxyForURL(url, host) {
17
+ return "DIRECT";
18
+ }
19
+ EOS
20
+ end
21
+
22
+ before :each do
23
+ allow(proxy_pac).to receive(:source).and_return(source)
24
+ allow(proxy_pac).to receive(:content).and_return(content)
25
+ allow(proxy_pac).to receive(:valid).and_return(true)
26
+ allow(proxy_pac).to receive(:type?).with(:string).and_return(true)
27
+ end
28
+
29
+ describe '#lint' do
30
+ let(:linter) { ProxyPacLinter.new(silent: true) }
31
+ let(:result) { true }
32
+
33
+ before(:each) do
34
+ expect(proxy_pac).to receive(:valid=).with(result)
35
+ allow(proxy_pac).to receive(:message=)
36
+ end
37
+
38
+ context 'when is valid' do
39
+ it { linter.lint(proxy_pac) }
40
+ end
41
+
42
+ context 'when is proxy.pac does not contain FindProxyForURL' do
43
+ let(:result) { false }
44
+ let(:content) { '' }
45
+ it { linter.lint(proxy_pac) }
46
+ end
47
+
48
+ context 'when is proxy.pac cannot be compiled' do
49
+ let(:result) { false }
50
+ let(:content) do
51
+ <<-EOS.strip_heredoc.chomp
52
+ function FindProxyForURL(url, host) {
53
+ asdfasf $$ SDF
54
+ }
55
+ EOS
56
+ end
57
+
58
+ it { linter.lint(proxy_pac) }
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ProxyPacLoader do
4
+ subject(:proxy_pac) { instance_double('ProxyPac::ProxyPacFile') }
5
+
6
+ let(:content) do
7
+ <<-EOS.strip_heredoc.chomp
8
+ function FindProxyForURL(url, host) {
9
+ return "DIRECT";
10
+ }
11
+ EOS
12
+ end
13
+
14
+ let(:source) do
15
+ <<-EOS.strip_heredoc.chomp
16
+ function FindProxyForURL(url, host) {
17
+ return "DIRECT";
18
+ }
19
+ EOS
20
+ end
21
+
22
+ before :each do
23
+ allow(proxy_pac).to receive(:content).and_return(content)
24
+ allow(proxy_pac).to receive(:source).and_return(source)
25
+ end
26
+
27
+ let(:loader) { ProxyPacLoader.new }
28
+ let(:type) { :string }
29
+
30
+ before :each do
31
+ allow(proxy_pac).to receive(:source).and_return(source)
32
+ allow(proxy_pac).to receive(:type=).with(type)
33
+ end
34
+
35
+ describe '#load' do
36
+ context 'when proxy pac is string' do
37
+ it_behaves_like 'a readable proxy.pac'
38
+ end
39
+
40
+ context 'when proxy pac has already been loaded' do
41
+ before(:each) { allow(proxy_pac).to receive(:content?).and_return(true) }
42
+ before(:each) { expect(proxy_pac).not_to receive(:content=) }
43
+
44
+ it { loader.load(proxy_pac) }
45
+ end
46
+
47
+ context 'when proxy pac is nil' do
48
+ let(:source) { nil }
49
+ let(:type) { :null }
50
+ it_behaves_like 'an un-readable proxy.pac'
51
+ end
52
+
53
+ context 'when proxy pac is file' do
54
+ let(:file) { 'proxy.pac' }
55
+ let(:type) { :file }
56
+ let(:source) { absolute_path(file) }
57
+
58
+ before(:each) { allow(proxy_pac).to receive(:source).and_return(source) }
59
+
60
+ context 'when is readable' do
61
+ before(:each) { write_file(file, content) }
62
+
63
+ it_behaves_like 'a readable proxy.pac'
64
+ end
65
+
66
+ context 'when is not readable' do
67
+ it_behaves_like 'an un-readable proxy.pac'
68
+ end
69
+ end
70
+
71
+ context 'when proxy pac is url' do
72
+ let(:type) { :url }
73
+ let(:source) { 'http://example.com/proxy.pac' }
74
+
75
+ context 'when is readable' do
76
+ before(:each) { stub_request(:get, source).to_return(body: content, status: 200) }
77
+
78
+ it_behaves_like 'a readable proxy.pac'
79
+ end
80
+
81
+ context 'when is not readable' do
82
+ before(:each) { stub_request(:get, source).to_raise(StandardError) }
83
+
84
+ it_behaves_like 'an un-readable proxy.pac'
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ProxyPacParser do
4
+ subject(:proxy_pac) { instance_double('ProxyPac::ProxyPacFile') }
5
+
6
+ let(:content) do
7
+ <<-EOS.strip_heredoc.chomp
8
+ function FindProxyForURL(url, host) {
9
+ return "DIRECT";
10
+ }
11
+ EOS
12
+ end
13
+
14
+ let(:source) do
15
+ <<-EOS.strip_heredoc.chomp
16
+ function FindProxyForURL(url, host) {
17
+ return "DIRECT";
18
+ }
19
+ EOS
20
+ end
21
+
22
+ before :each do
23
+ allow(proxy_pac).to receive(:content).and_return(content)
24
+ allow(proxy_pac).to receive(:valid?).and_return(true)
25
+ allow(proxy_pac).to receive(:source).and_return(source)
26
+ allow(proxy_pac).to receive(:type?).with(:string).and_return(true)
27
+ end
28
+
29
+ describe '#parse' do
30
+ context 'when is valid' do
31
+ before(:each) do
32
+ expect(proxy_pac).to receive(:javascript=)
33
+ expect(proxy_pac).to receive(:parsable=).with(true)
34
+ end
35
+
36
+ before(:each) { ProxyPacParser.new.parse(proxy_pac) }
37
+ end
38
+
39
+ context 'when is invalid' do
40
+ let(:content) do
41
+ <<-EOS.strip_heredoc.chomp
42
+ function FindProxyForURL(url, host) {
43
+ asdfasf $$ SDF
44
+ }
45
+ EOS
46
+ end
47
+
48
+ before(:each) do
49
+ expect(proxy_pac).to receive(:message=)
50
+ expect(proxy_pac).to receive(:parsable=).with(false)
51
+ end
52
+
53
+ before(:each) { ProxyPacParser.new.parse(proxy_pac) }
54
+ end
55
+ end
56
+ end