proxy_pac_rb 0.3.8 → 0.4.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 (51) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -0
  3. data/.travis.yml +2 -1
  4. data/Gemfile +13 -13
  5. data/Gemfile.lock +84 -58
  6. data/README.md +40 -13
  7. data/features/check_proxy_pac.feature +31 -0
  8. data/features/compress_proxy_pac.feature +3 -16
  9. data/features/resolve_proxy.feature +1 -1
  10. data/lib/proxy_pac_rb/cli/compress_proxy_pac.rb +25 -11
  11. data/lib/proxy_pac_rb/cli/find_proxy.rb +13 -8
  12. data/lib/proxy_pac_rb/cli/lint.rb +11 -0
  13. data/lib/proxy_pac_rb/cli/lint_proxy_pac.rb +37 -0
  14. data/lib/proxy_pac_rb/cli/runner.rb +5 -0
  15. data/lib/proxy_pac_rb/cli/shared.rb +4 -0
  16. data/lib/proxy_pac_rb/cli_validator.rb +14 -0
  17. data/lib/proxy_pac_rb/errors.rb +19 -0
  18. data/lib/proxy_pac_rb/javascript.rb +19 -0
  19. data/lib/proxy_pac_rb/main.rb +22 -0
  20. data/lib/proxy_pac_rb/parser.rb +32 -21
  21. data/lib/proxy_pac_rb/proxy_pac.rb +10 -20
  22. data/lib/proxy_pac_rb/proxy_pac_compressor.rb +9 -0
  23. data/lib/proxy_pac_rb/proxy_pac_dumper.rb +65 -0
  24. data/lib/proxy_pac_rb/proxy_pac_file.rb +20 -3
  25. data/lib/proxy_pac_rb/proxy_pac_linter.rb +82 -0
  26. data/lib/proxy_pac_rb/proxy_pac_loader.rb +98 -0
  27. data/lib/proxy_pac_rb/proxy_pac_parser.rb +41 -0
  28. data/lib/proxy_pac_rb/rack/proxy_pac_compressor.rb +68 -0
  29. data/lib/proxy_pac_rb/rack/proxy_pac_linter.rb +66 -0
  30. data/lib/proxy_pac_rb/runtimes/rubyracer.rb +2 -2
  31. data/lib/proxy_pac_rb/runtimes/rubyrhino.rb +2 -2
  32. data/lib/proxy_pac_rb/runtimes.rb +3 -3
  33. data/lib/proxy_pac_rb/version.rb +1 -1
  34. data/lib/proxy_pac_rb.rb +10 -3
  35. data/proxy_pac_rb.gemspec +2 -0
  36. data/spec/api_spec.rb +207 -0
  37. data/spec/parser_spec.rb +66 -53
  38. data/spec/rack/proxy_pac_compressor_spec.rb +59 -0
  39. data/spec/rack/proxy_pac_linter_spec.rb +65 -0
  40. data/spec/spec_helper.rb +1 -0
  41. data/spec/support/matchers/file.rb +23 -0
  42. data/spec/support/rack_test.rb +1 -0
  43. data/spec/support/rspec.rb +3 -1
  44. data/spec/support/webmock.rb +1 -0
  45. metadata +30 -12
  46. data/features/support/fixtures.rb +0 -15
  47. data/lib/proxy_pac_rb/cli.rb +0 -75
  48. data/lib/proxy_pac_rb/file.rb +0 -21
  49. data/lib/proxy_pac_rb/javascript_compressor.rb +0 -9
  50. data/lib/proxy_pac_rb/proxy_pac_template +0 -39
  51. data/spec/file_spec.rb +0 -45
@@ -0,0 +1,66 @@
1
+ require 'proxy_pac_rb'
2
+ require 'rack'
3
+
4
+ module ProxyPacRb
5
+ module Rack
6
+ # Rack Middleware to lint proxy pac
7
+ #
8
+ # @example Sinatra's <server>.rb
9
+ # require 'proxy_pac_rb/rack/proxy_pac_linter'
10
+ # use ProxyPacRb::Rack::ProxyPacLinter
11
+ #
12
+ # @example Rack's config.ru
13
+ # require 'proxy_pac_rb/rack/proxy_pac_linter'
14
+ # use ProxyPacRb::Rack::ProxyPacLinter
15
+ #
16
+ # @example Middleman's config.rb
17
+ # require 'proxy_pac_rb/rack/proxy_pac_linter'
18
+ # use ProxyPacRb::Rack::ProxyPacLinter
19
+ class ProxyPacLinter
20
+ private
21
+
22
+ attr_reader :linter, :loader, :enabled
23
+
24
+ public
25
+
26
+ def initialize(
27
+ app,
28
+ enabled: true
29
+ )
30
+ @app = app
31
+ @linter = ProxyPacRb::ProxyPacLinter.new(silent: true)
32
+ @loader = ProxyPacRb::ProxyPacLoader.new
33
+ @enabled = enabled
34
+ end
35
+
36
+ def call(env)
37
+ status, headers, body = @app.call(env)
38
+
39
+ return [status, headers, body] if enabled == false
40
+ # rubocop:disable Style/CaseEquality
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
44
+
45
+ content = [body].flatten.each_with_object('') { |e, a| a << e.to_s }
46
+
47
+ proxy_pac = ProxyPacFile.new(source: content)
48
+
49
+ loader.load(proxy_pac)
50
+ linter.lint(proxy_pac)
51
+
52
+ if proxy_pac.valid?
53
+ content = proxy_pac.content
54
+ else
55
+ status = 500
56
+ content = proxy_pac.message
57
+ headers['Content-Length'] = content.bytesize.to_s if headers['Content-Length']
58
+ end
59
+
60
+ [status, headers, [content]]
61
+ ensure
62
+ body.close if body.respond_to?(:close)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -32,7 +32,7 @@ module ProxyPacRb
32
32
  if e.value['name'] == 'SyntaxError'
33
33
  raise e.value.to_s
34
34
  else
35
- raise Exceptions::ProgramError, e.value.to_s
35
+ raise ProgramError, e.value.to_s
36
36
  end
37
37
  end
38
38
  end
@@ -46,7 +46,7 @@ module ProxyPacRb
46
46
  if e.value['name'] == 'SyntaxError'
47
47
  raise e.value.to_s
48
48
  else
49
- raise Exceptions::ProgramError, e.value.to_s
49
+ raise ProgramError, e.value.to_s
50
50
  end
51
51
  end
52
52
  end
@@ -29,7 +29,7 @@ module ProxyPacRb
29
29
  if e.message =~ /^syntax error/
30
30
  raise e.message
31
31
  else
32
- raise Exceptions::ProgramError, e.message
32
+ raise ProgramError, e.message
33
33
  end
34
34
  end
35
35
 
@@ -39,7 +39,7 @@ module ProxyPacRb
39
39
  if e.message == 'syntax error'
40
40
  raise e.message
41
41
  else
42
- raise Exceptions::ProgramError, e.message
42
+ raise ProgramError, e.message
43
43
  end
44
44
  end
45
45
 
@@ -7,7 +7,7 @@ module ProxyPacRb
7
7
  class << self
8
8
  def autodetect
9
9
  from_environment || best_available ||
10
- fail(Exceptions::RuntimeUnavailable, 'Could not find a JavaScript runtime. ' \
10
+ fail(RuntimeUnavailableError, 'Could not find a JavaScript runtime. ' \
11
11
  'See https://github.com/sstephenson/execjs for a list of available runtimes.')
12
12
  end
13
13
 
@@ -20,8 +20,8 @@ module ProxyPacRb
20
20
 
21
21
  runtime = const_get(ENV['JS_RUNTIME'])
22
22
 
23
- fail Exceptions::RuntimeUnavailable, "#{ENV['JS_RUNTIME']} runtime is not defined" unless runtime
24
- fail Exceptions::RuntimeUnavailable, "#{runtime.name} runtime is not available on this system" unless runtime.available?
23
+ fail RuntimeUnavailableError, "#{ENV['JS_RUNTIME']} runtime is not defined" unless runtime
24
+ fail RuntimeUnavailableError, "#{runtime.name} runtime is not available on this system" unless runtime.available?
25
25
 
26
26
  runtime
27
27
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
  # ProxyPacRb
3
3
  module ProxyPacRb
4
- VERSION = '0.3.8'
4
+ VERSION = '0.4.0'
5
5
  end
data/lib/proxy_pac_rb.rb CHANGED
@@ -4,32 +4,39 @@ require 'addressable/uri'
4
4
  require 'ipaddr'
5
5
  require 'thor'
6
6
  require 'uglifier'
7
- require 'open-uri'
8
7
  require 'optparse'
9
8
  require 'ostruct'
10
9
  require 'resolv'
11
10
  require 'time'
12
11
 
13
12
  require 'proxy_pac_rb/version'
13
+ require 'proxy_pac_rb/main'
14
+ require 'proxy_pac_rb/errors'
14
15
  require 'proxy_pac_rb/encoding'
15
16
  require 'proxy_pac_rb/exceptions'
16
- require 'proxy_pac_rb/file'
17
17
  require 'proxy_pac_rb/environment'
18
18
  require 'proxy_pac_rb/runtime'
19
19
  require 'proxy_pac_rb/runtimes/rubyracer'
20
20
  require 'proxy_pac_rb/runtimes/rubyrhino'
21
21
  require 'proxy_pac_rb/runtimes'
22
22
  require 'proxy_pac_rb/parser'
23
+ require 'proxy_pac_rb/javascript'
23
24
  require 'proxy_pac_rb/proxy_pac_js'
24
25
  require 'proxy_pac_rb/proxy_pac'
25
26
  require 'proxy_pac_rb/proxy_pac_file'
26
27
  require 'proxy_pac_rb/proxy_pac_template'
27
- require 'proxy_pac_rb/javascript_compressor'
28
+ require 'proxy_pac_rb/proxy_pac_compressor'
29
+ require 'proxy_pac_rb/proxy_pac_dumper'
30
+ require 'proxy_pac_rb/proxy_pac_loader'
31
+ require 'proxy_pac_rb/proxy_pac_linter'
32
+ require 'proxy_pac_rb/proxy_pac_parser'
28
33
 
29
34
  require 'proxy_pac_rb/cli_validator'
30
35
 
31
36
  require 'proxy_pac_rb/cli/shared'
32
37
  require 'proxy_pac_rb/cli/find_proxy'
38
+ require 'proxy_pac_rb/cli/lint_proxy_pac'
39
+ require 'proxy_pac_rb/cli/lint'
33
40
  require 'proxy_pac_rb/cli/compress_proxy_pac'
34
41
  require 'proxy_pac_rb/cli/compress'
35
42
  require 'proxy_pac_rb/cli/show'
data/proxy_pac_rb.gemspec CHANGED
@@ -26,4 +26,6 @@ DESC
26
26
  spec.add_runtime_dependency 'addressable'
27
27
  spec.add_runtime_dependency 'activesupport'
28
28
  spec.add_runtime_dependency 'uglifier'
29
+
30
+ spec.required_ruby_version = '~> 2.0'
29
31
  end
data/spec/api_spec.rb ADDED
@@ -0,0 +1,207 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ProxyPacRb 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 ProxyPacCompressor do
30
+ let(:compressor) { described_class.new }
31
+ let(:modified_content) { %(function FindProxyForURL(){return"DIRECT"}) }
32
+
33
+ before :each do
34
+ expect(proxy_pac).to receive(:content=).with(modified_content)
35
+ end
36
+
37
+ describe '#modify' do
38
+ context 'when string contains white paces' do
39
+ it { compressor.compress(proxy_pac) }
40
+ end
41
+ end
42
+ end
43
+
44
+ describe ProxyPacDumper do
45
+ let(:dumper) { ProxyPacDumper.new }
46
+ let(:destination) { absolute_path('proxy.pac') }
47
+
48
+ describe '#dump' do
49
+ before :each do
50
+ allow(proxy_pac).to receive(:source).and_return(source)
51
+ end
52
+
53
+ context 'when proxy pac is string' do
54
+ before :each do
55
+ in_current_dir do
56
+ dumper.dump(proxy_pac, type: :string)
57
+ end
58
+ end
59
+
60
+ it { expect(destination).to be_existing_file }
61
+ it { expect(destination).to have_content proxy_pac.content }
62
+ end
63
+
64
+ context 'when proxy pac is file' do
65
+ let(:source) { 'proxy.pac.in' }
66
+
67
+ before :each do
68
+ write_file(source, content)
69
+ end
70
+
71
+ before :each do
72
+ in_current_dir do
73
+ dumper.dump(proxy_pac, type: :template)
74
+ end
75
+ end
76
+
77
+ around :example do |example|
78
+ in_current_dir { example.call }
79
+ end
80
+
81
+ it { expect(destination).to be_existing_file }
82
+ it { expect(destination).to have_content proxy_pac.content }
83
+ end
84
+ end
85
+ end
86
+
87
+ describe ProxyPacLoader do
88
+ let(:loader) { ProxyPacLoader.new }
89
+ let(:type) { :string }
90
+
91
+ before :each do
92
+ allow(proxy_pac).to receive(:source).and_return(source)
93
+ allow(proxy_pac).to receive(:type=).with(type)
94
+ end
95
+
96
+ before :each do
97
+ expect(proxy_pac).to receive(:content=).with(content)
98
+ end
99
+
100
+ describe '#load' do
101
+ context 'when proxy pac is string' do
102
+ it { loader.load(proxy_pac) }
103
+ end
104
+
105
+ context 'when proxy pac is file' do
106
+ let(:file) { 'proxy.pac' }
107
+ let(:type) { :file }
108
+
109
+ before(:each) { allow(proxy_pac).to receive(:source).and_return(absolute_path(file)) }
110
+ before(:each) { write_file(file, content) }
111
+
112
+ it { loader.load(proxy_pac) }
113
+ end
114
+
115
+ context 'when proxy pac is url' do
116
+ let(:uri) { 'http://example.com/proxy.pac' }
117
+ let(:type) { :url }
118
+
119
+ before(:each) { allow(proxy_pac).to receive(:source).and_return(uri) }
120
+ before(:each) { stub_request(:get, 'http://example.com/proxy.pac').to_return(body: content) }
121
+
122
+ it { loader.load(proxy_pac) }
123
+ end
124
+ end
125
+ end
126
+
127
+ describe ProxyPacParser do
128
+ describe '#parse' do
129
+ let(:pac) { ProxyPacParser.new.parse(proxy_pac) }
130
+
131
+ context 'when is valid' do
132
+ it { expect(pac).to be_kind_of ProxyPac }
133
+ end
134
+
135
+ context 'when is valid' do
136
+ let(:content) do
137
+ <<-EOS.strip_heredoc.chomp
138
+ function FindProxyForURL(url, host) {
139
+ asdfasf $$ SDF
140
+ }
141
+ EOS
142
+ end
143
+
144
+ it { expect { pac }.to raise_error ParserError }
145
+ end
146
+ end
147
+ end
148
+
149
+ describe ProxyPacLinter do
150
+ describe '#lint' do
151
+ let(:linter) { ProxyPacLinter.new(silent: true) }
152
+ let(:result) { true }
153
+
154
+ before(:each) do
155
+ expect(proxy_pac).to receive(:valid=).with(result)
156
+ allow(proxy_pac).to receive(:message=)
157
+ end
158
+
159
+ context 'when is valid' do
160
+ it { linter.lint(proxy_pac) }
161
+ end
162
+
163
+ context 'when is proxy.pac does not contain FindProxyForURL' do
164
+ let(:result) { false }
165
+ let(:content) { '' }
166
+ it { linter.lint(proxy_pac) }
167
+ end
168
+
169
+ context 'when is proxy.pac cannot be compiled' do
170
+ let(:result) { false }
171
+ let(:content) do
172
+ <<-EOS.strip_heredoc.chomp
173
+ function FindProxyForURL(url, host) {
174
+ asdfasf $$ SDF
175
+ }
176
+ EOS
177
+ end
178
+
179
+ it { linter.lint(proxy_pac) }
180
+ end
181
+ end
182
+ end
183
+ end
184
+
185
+ RSpec.describe ProxyPacFile do
186
+ subject(:proxy_pac) { ProxyPacFile.new source: source }
187
+
188
+ let(:source) do
189
+ <<-EOS.strip_heredoc.chomp
190
+ function FindProxyForURL(url, host) {
191
+ return "DIRECT";
192
+ }
193
+ EOS
194
+ end
195
+
196
+ describe '#valid?' do
197
+ context 'when is invalid' do
198
+ it { expect(proxy_pac).not_to be_valid }
199
+ end
200
+
201
+ context 'when is valid' do
202
+ before(:each) { proxy_pac.valid = true }
203
+
204
+ it { expect(proxy_pac).to be_valid }
205
+ end
206
+ end
207
+ end
data/spec/parser_spec.rb CHANGED
@@ -2,41 +2,40 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  describe ProxyPacRb::Parser do
5
- before :each do
6
- write_file 'sample.pac', <<-EOS.strip_heredoc
5
+ subject(:proxy_pac) { Parser.new(environment: environment).parse(source) }
6
+
7
+ let(:environment) { Environment.new }
8
+
9
+ let(:content) do
10
+ <<-EOS.strip_heredoc.chomp
7
11
  function FindProxyForURL(url, host) {
8
12
  return "DIRECT";
9
13
  }
10
14
  EOS
11
15
  end
12
16
 
13
- let(:sample_pac) { absolute_path('sample.pac') }
17
+ let(:source) do
18
+ <<-EOS.strip_heredoc.chomp
19
+ function FindProxyForURL(url, host) {
20
+ return "DIRECT";
21
+ }
22
+ EOS
23
+ end
14
24
 
15
- context '.read' do
16
- it 'should load a file from a path' do
17
- pac = ProxyPacRb::Parser.new.read(sample_pac)
18
- expect(pac).not_to be_nil
19
- end
25
+ context 'when string is given' do
26
+ it { expect(proxy_pac).not_to be_nil }
20
27
  end
21
28
 
22
- context '.source' do
23
- let(:source) do
24
- <<-JS.strip_heredoc
25
- function FindProxyForURL(url, host) {
26
- return "DIRECT";
27
- }
28
- JS
29
- end
29
+ context 'when path is given' do
30
+ let(:source) { absolute_path('proxy.pac') }
31
+ before(:each) { write_file(source, content) }
30
32
 
31
- it 'should load source' do
32
- pac = ProxyPacRb::Parser.new.source(source)
33
- expect(pac).not_to be_nil
34
- end
33
+ it { expect(proxy_pac).not_to be_nil }
35
34
  end
36
35
 
37
- context 'compile' do
38
- it 'supports a changeable ip address' do
39
- string = <<-EOS
36
+ context 'when ip address is given' do
37
+ let(:source) do
38
+ <<-EOS
40
39
  function FindProxyForURL(url, host) {
41
40
  if ( myIpAddress() == '127.0.0.2' ) {
42
41
  return "DIRECT";
@@ -45,18 +44,22 @@ describe ProxyPacRb::Parser do
45
44
  }
46
45
  }
47
46
  EOS
47
+ end
48
48
 
49
- environment = ProxyPacRb::Environment.new(client_ip: '127.0.0.1')
50
- file = ProxyPacRb::Parser.new(environment).source(string)
51
- expect(file.find('http://localhost')).to eq('PROXY localhost:8080')
49
+ context 'when ip is 127.0.0.1' do
50
+ let(:environment) { Environment.new(client_ip: '127.0.0.1') }
51
+ it { expect(proxy_pac.find('http://localhost')).to eq('PROXY localhost:8080') }
52
+ end
52
53
 
53
- environment = ProxyPacRb::Environment.new(client_ip: '127.0.0.2')
54
- file = ProxyPacRb::Parser.new(environment).source(string)
55
- expect(file.find('http://localhost')).to eq('DIRECT')
54
+ context 'when ip is 127.0.0.2' do
55
+ let(:environment) { Environment.new(client_ip: '127.0.0.2') }
56
+ it { expect(proxy_pac.find('http://localhost')).to eq('DIRECT') }
56
57
  end
58
+ end
57
59
 
58
- it 'supports a changeable date' do
59
- string = <<-EOS
60
+ context 'when date is given' do
61
+ let(:source) do
62
+ <<-EOS
60
63
  function FindProxyForURL(url, host) {
61
64
  if (weekdayRange("FRI", "SAT")) {
62
65
  return "PROXY localhost:8080";
@@ -65,18 +68,22 @@ describe ProxyPacRb::Parser do
65
68
  }
66
69
  }
67
70
  EOS
71
+ end
68
72
 
69
- environment = Environment.new(time: Time.parse('2014-03-08 12:00'))
70
- file = ProxyPacRb::Parser.new(environment).source(string)
71
- expect(file.find('http://localhost')).to eq('PROXY localhost:8080')
73
+ context 'when time is 2014-03-08 12:00' do
74
+ let(:environment) { Environment.new(time: Time.parse('2014-03-08 12:00')) }
75
+ it { expect(proxy_pac.find('http://localhost')).to eq('PROXY localhost:8080') }
76
+ end
72
77
 
73
- environment = Environment.new(time: Time.parse('2014-03-06 12:00'))
74
- file = ProxyPacRb::Parser.new(environment).source(string)
75
- expect(file.find('http://localhost')).to eq('DIRECT')
78
+ context 'when time is 2014-03-06 12:0' do
79
+ let(:environment) { Environment.new(time: Time.parse('2014-03-06 12:00')) }
80
+ it { expect(proxy_pac.find('http://localhost')).to eq('DIRECT') }
76
81
  end
82
+ end
77
83
 
78
- it 'supports time range' do
79
- string = <<-EOS
84
+ context 'when time range is used' do
85
+ let(:source) do
86
+ <<-EOS
80
87
  function FindProxyForURL(url, host) {
81
88
  if (timeRange(8, 18)) {
82
89
  return "PROXY localhost:8080";
@@ -85,18 +92,22 @@ describe ProxyPacRb::Parser do
85
92
  }
86
93
  }
87
94
  EOS
95
+ end
88
96
 
89
- environment = ProxyPacRb::Environment.new(time: Time.parse('2014-03-06 12:00'))
90
- file = ProxyPacRb::Parser.new(environment).source(string)
91
- expect(file.find('http://localhost')).to eq('PROXY localhost:8080')
97
+ context 'when time is 2014-03-06 12:00' do
98
+ let(:environment) { ProxyPacRb::Environment.new(time: Time.parse('2014-03-06 12:00')) }
99
+ it { expect(proxy_pac.find('http://localhost')).to eq('PROXY localhost:8080') }
100
+ end
92
101
 
93
- environment = ProxyPacRb::Environment.new(time: Time.parse('2014-03-08 6:00'))
94
- file = ProxyPacRb::Parser.new(environment).source(string)
95
- expect(file.find('http://localhost')).to eq('DIRECT')
102
+ context 'when time is 2014-03-08 6:00' do
103
+ let(:environment) { ProxyPacRb::Environment.new(time: Time.parse('2014-03-08 6:00')) }
104
+ it { expect(proxy_pac.find('http://localhost')).to eq('DIRECT') }
96
105
  end
106
+ end
97
107
 
98
- it 'supports a date range' do
99
- string = <<-EOS
108
+ context 'when date range is used' do
109
+ let(:source) do
110
+ <<-EOS
100
111
  function FindProxyForURL(url, host) {
101
112
  if (dateRange("JUL", "SEP")) {
102
113
  return "PROXY localhost:8080";
@@ -105,14 +116,16 @@ describe ProxyPacRb::Parser do
105
116
  }
106
117
  }
107
118
  EOS
119
+ end
108
120
 
109
- environment = ProxyPacRb::Environment.new(time: Time.parse('2014-07-06 12:00'))
110
- file = ProxyPacRb::Parser.new(environment).source(string)
111
- expect(file.find('http://localhost')).to eq('PROXY localhost:8080')
121
+ context 'when time is 2014-07-06 12:00' do
122
+ let(:environment) { ProxyPacRb::Environment.new(time: Time.parse('2014-07-06 12:00')) }
123
+ it { expect(proxy_pac.find('http://localhost')).to eq('PROXY localhost:8080') }
124
+ end
112
125
 
113
- environment = ProxyPacRb::Environment.new(time: Time.parse('2014-03-08 6:00'))
114
- file = ProxyPacRb::Parser.new(environment).source(string)
115
- expect(file.find('http://localhost')).to eq('DIRECT')
126
+ context 'when time is 2014-03-08 6:00' do
127
+ let(:environment) { ProxyPacRb::Environment.new(time: Time.parse('2014-03-08 6:00')) }
128
+ it { expect(proxy_pac.find('http://localhost')).to eq('DIRECT') }
116
129
  end
117
130
  end
118
131
  end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+ require 'proxy_pac_rb/rack/proxy_pac_compressor'
3
+
4
+ RSpec.describe ProxyPacRb::Rack::ProxyPacCompressor, type: :rack_test do
5
+ let(:compressed_content) { %(function FindProxyForURL(){return\"DIRECT\"}) }
6
+
7
+ before(:each) { get '/' }
8
+ subject(:body) { last_response.body }
9
+
10
+ context 'when valid proxy pac is given' do
11
+ let(:app) do
12
+ a = Class.new(Sinatra::Base) do
13
+ before do
14
+ content_type 'application/x-ns-proxy-autoconfig'
15
+ end
16
+
17
+ get '/' do
18
+ <<-EOS.strip_heredoc.chomp
19
+ function FindProxyForURL(url, host) {
20
+ return "DIRECT";
21
+ }
22
+ EOS
23
+ end
24
+ end
25
+
26
+ a.use ProxyPacRb::Rack::ProxyPacCompressor
27
+
28
+ a.new
29
+ end
30
+
31
+ it { expect(body).to eq compressed_content }
32
+ end
33
+
34
+ context 'when invalid proxy pac is given' do
35
+ let(:compressed_content) { %{Unexpected token: string (§$ )} }
36
+
37
+ let(:app) do
38
+ a = Class.new(Sinatra::Base) do
39
+ before do
40
+ content_type 'application/x-ns-proxy-autoconfig'
41
+ end
42
+
43
+ get '/' do
44
+ <<-EOS.strip_heredoc.chomp
45
+ function FindProxyForURL(url, host) {
46
+ return $"§$ "DIRECT";
47
+ }
48
+ EOS
49
+ end
50
+ end
51
+
52
+ a.use ProxyPacRb::Rack::ProxyPacCompressor
53
+
54
+ a.new
55
+ end
56
+
57
+ it { expect(body).to include compressed_content }
58
+ end
59
+ end