puffing-billy 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,13 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Billy::Handler do
4
- let(:handler) { Class.new{ include Billy::Handler }.new }
4
+ let(:handler) { Class.new { include Billy::Handler }.new }
5
5
  it '#handle_request raises an error if not overridden' do
6
- expect(handler.handle_request(nil,nil,nil,nil)).to eql({ error: 'The handler has not overridden the handle_request method!' })
6
+ expect(handler.handle_request(nil, nil, nil, nil)).to eql(error: 'The handler has not overridden the handle_request method!')
7
7
  end
8
8
 
9
9
  it '#handles_request returns false by default' do
10
- expect(handler.handles_request?(nil,nil,nil,nil)).to be false
10
+ expect(handler.handles_request?(nil, nil, nil, nil)).to be false
11
11
  end
12
12
 
13
13
  it 'responds to #reset' do
@@ -2,13 +2,15 @@ require 'spec_helper'
2
2
 
3
3
  describe Billy::ProxyHandler do
4
4
  subject { Billy::ProxyHandler.new }
5
- let(:request) { {
5
+ let(:request) do
6
+ {
6
7
  method: 'post',
7
8
  url: 'http://example.test:8080/index?some=param',
8
- headers: {'Accept-Encoding' => 'gzip',
9
- 'Cache-Control' => 'no-cache' },
9
+ headers: { 'Accept-Encoding' => 'gzip',
10
+ 'Cache-Control' => 'no-cache' },
10
11
  body: 'Some body'
11
- } }
12
+ }
13
+ end
12
14
 
13
15
  describe '#handles_request?' do
14
16
  context 'with non-whitelisted requests enabled' do
@@ -108,31 +110,31 @@ describe Billy::ProxyHandler do
108
110
  header
109
111
  end
110
112
 
111
- let(:em_response) { double("response") }
112
- let(:em_request) {
113
- double("EM::HttpRequest", :error => nil, :response => em_response, :response_header => response_header)
114
- }
113
+ let(:em_response) { double('response') }
114
+ let(:em_request) do
115
+ double('EM::HttpRequest', error: nil, response: em_response, response_header: response_header)
116
+ end
115
117
 
116
118
  before do
117
119
  allow(subject).to receive(:handles_request?).and_return(true)
118
- allow(em_response).to receive(:force_encoding).and_return("The response body")
120
+ allow(em_response).to receive(:force_encoding).and_return('The response body')
119
121
  allow(EventMachine::HttpRequest).to receive(:new).and_return(em_request)
120
122
  expect(em_request).to receive(:post).and_return(em_request)
121
123
  end
122
124
 
123
125
  it 'returns any error in the response' do
124
- allow(em_request).to receive(:error).and_return("ERROR!")
126
+ allow(em_request).to receive(:error).and_return('ERROR!')
125
127
  expect(subject.handle_request(request[:method],
126
128
  request[:url],
127
129
  request[:headers],
128
- request[:body])).to eql({ :error => "Request to #{request[:url]} failed with error: ERROR!"})
130
+ request[:body])).to eql(error: "Request to #{request[:url]} failed with error: ERROR!")
129
131
  end
130
132
 
131
133
  it 'returns a hashed response if the request succeeds' do
132
134
  expect(subject.handle_request(request[:method],
133
135
  request[:url],
134
136
  request[:headers],
135
- request[:body])).to eql({:status=>200, :headers=>{"Connection"=>"close"}, :content=>"The response body"})
137
+ request[:body])).to eql(status: 200, headers: { 'Connection' => 'close' }, content: 'The response body')
136
138
  end
137
139
 
138
140
  it 'returns nil if both the error and response are for some reason nil' do
@@ -156,10 +158,10 @@ describe Billy::ProxyHandler do
156
158
  allow(Billy.config).to receive(:proxied_request_inactivity_timeout).and_return(42)
157
159
  allow(Billy.config).to receive(:proxied_request_connect_timeout).and_return(24)
158
160
 
159
- expect(EventMachine::HttpRequest).to receive(:new).with(request[:url], {
160
- inactivity_timeout: 42,
161
- connect_timeout: 24
162
- })
161
+ expect(EventMachine::HttpRequest).to receive(:new).with(request[:url],
162
+ inactivity_timeout: 42,
163
+ connect_timeout: 24
164
+ )
163
165
 
164
166
  subject.handle_request(request[:method],
165
167
  request[:url],
@@ -22,15 +22,17 @@ describe Billy::RequestHandler do
22
22
  end
23
23
 
24
24
  context 'with stubbed handlers' do
25
- let(:args) { ["get","url","headers","body"] }
26
- let(:stub_handler) { double("StubHandler") }
27
- let(:cache_handler) { double("CacheHandler") }
28
- let(:proxy_handler) { double("ProxyHandler") }
29
- let(:handlers) { {
30
- :stubs => stub_handler,
31
- :cache => cache_handler,
32
- :proxy => proxy_handler
33
- } }
25
+ let(:args) { %w(get url headers body) }
26
+ let(:stub_handler) { double('StubHandler') }
27
+ let(:cache_handler) { double('CacheHandler') }
28
+ let(:proxy_handler) { double('ProxyHandler') }
29
+ let(:handlers) do
30
+ {
31
+ stubs: stub_handler,
32
+ cache: cache_handler,
33
+ proxy: proxy_handler
34
+ }
35
+ end
34
36
 
35
37
  before do
36
38
  allow(subject).to receive(:handlers).and_return(handlers)
@@ -38,82 +40,82 @@ describe Billy::RequestHandler do
38
40
 
39
41
  describe '#handles_request?' do
40
42
  it 'returns false if no handlers handle the request' do
41
- handlers.each do |key,handler|
43
+ handlers.each do |_key, handler|
42
44
  expect(handler).to receive(:handles_request?).with(*args).and_return(false)
43
45
  end
44
46
  expect(subject.handles_request?(*args)).to be false
45
47
  end
46
48
 
47
49
  it 'returns true immediately if the stub handler handles the request' do
48
- expect(stub_handler).to receive(:handles_request?).with(*args).and_return(true)
49
- expect(cache_handler).to_not receive(:handles_request?)
50
- expect(proxy_handler).to_not receive(:handles_request?)
50
+ expect(stub_handler).to receive(:handles_request?).with(*args).and_return(true)
51
+ expect(cache_handler).to_not receive(:handles_request?)
52
+ expect(proxy_handler).to_not receive(:handles_request?)
51
53
  expect(subject.handles_request?(*args)).to be true
52
54
  end
53
55
 
54
56
  it 'returns true if the cache handler handles the request' do
55
- expect(stub_handler).to receive(:handles_request?).with(*args).and_return(false)
56
- expect(cache_handler).to receive(:handles_request?).with(*args).and_return(true)
57
- expect(proxy_handler).to_not receive(:handles_request?)
57
+ expect(stub_handler).to receive(:handles_request?).with(*args).and_return(false)
58
+ expect(cache_handler).to receive(:handles_request?).with(*args).and_return(true)
59
+ expect(proxy_handler).to_not receive(:handles_request?)
58
60
  expect(subject.handles_request?(*args)).to be true
59
61
  end
60
62
 
61
63
  it 'returns true if the proxy handler handles the request' do
62
- expect(stub_handler).to receive(:handles_request?).with(*args).and_return(false)
63
- expect(cache_handler).to receive(:handles_request?).with(*args).and_return(false)
64
- expect(proxy_handler).to receive(:handles_request?).with(*args).and_return(true)
64
+ expect(stub_handler).to receive(:handles_request?).with(*args).and_return(false)
65
+ expect(cache_handler).to receive(:handles_request?).with(*args).and_return(false)
66
+ expect(proxy_handler).to receive(:handles_request?).with(*args).and_return(true)
65
67
  expect(subject.handles_request?(*args)).to be true
66
68
  end
67
69
  end
68
70
 
69
71
  describe '#handle_request' do
70
72
  it 'returns stubbed responses' do
71
- expect(stub_handler).to receive(:handle_request).with(*args).and_return("foo")
72
- expect(cache_handler).to_not receive(:handle_request)
73
- expect(proxy_handler).to_not receive(:handle_request)
74
- expect(subject.handle_request(*args)).to eql "foo"
73
+ expect(stub_handler).to receive(:handle_request).with(*args).and_return('foo')
74
+ expect(cache_handler).to_not receive(:handle_request)
75
+ expect(proxy_handler).to_not receive(:handle_request)
76
+ expect(subject.handle_request(*args)).to eql 'foo'
75
77
  end
76
78
 
77
79
  it 'returns cached responses' do
78
- expect(stub_handler).to receive(:handle_request).with(*args)
79
- expect(cache_handler).to receive(:handle_request).with(*args).and_return("bar")
80
- expect(proxy_handler).to_not receive(:handle_request)
81
- expect(subject.handle_request(*args)).to eql "bar"
80
+ expect(stub_handler).to receive(:handle_request).with(*args)
81
+ expect(cache_handler).to receive(:handle_request).with(*args).and_return('bar')
82
+ expect(proxy_handler).to_not receive(:handle_request)
83
+ expect(subject.handle_request(*args)).to eql 'bar'
82
84
  end
83
85
 
84
86
  it 'returns proxied responses' do
85
- expect(stub_handler).to receive(:handle_request).with(*args)
86
- expect(cache_handler).to receive(:handle_request).with(*args)
87
- expect(proxy_handler).to receive(:handle_request).with(*args).and_return("baz")
88
- expect(subject.handle_request(*args)).to eql "baz"
87
+ expect(stub_handler).to receive(:handle_request).with(*args)
88
+ expect(cache_handler).to receive(:handle_request).with(*args)
89
+ expect(proxy_handler).to receive(:handle_request).with(*args).and_return('baz')
90
+ expect(subject.handle_request(*args)).to eql 'baz'
89
91
  end
90
92
 
91
93
  it 'returns an error hash if request is not handled' do
92
- expect(stub_handler).to receive(:handle_request).with(*args)
93
- expect(cache_handler).to receive(:handle_request).with(*args)
94
- expect(proxy_handler).to receive(:handle_request).with(*args)
95
- expect(subject.handle_request(*args)).to eql({ :error => "Connection to url not cached and new http connections are disabled" })
94
+ expect(stub_handler).to receive(:handle_request).with(*args)
95
+ expect(cache_handler).to receive(:handle_request).with(*args)
96
+ expect(proxy_handler).to receive(:handle_request).with(*args)
97
+ expect(subject.handle_request(*args)).to eql(error: 'Connection to url not cached and new http connections are disabled')
96
98
  end
97
99
 
98
100
  it 'returns an error hash with body message if POST request is not handled' do
99
101
  args[0] = 'post'
100
- expect(stub_handler).to receive(:handle_request).with(*args)
101
- expect(cache_handler).to receive(:handle_request).with(*args)
102
- expect(proxy_handler).to receive(:handle_request).with(*args)
103
- expect(subject.handle_request(*args)).to eql({ :error => "Connection to url with body 'body' not cached and new http connections are disabled" })
102
+ expect(stub_handler).to receive(:handle_request).with(*args)
103
+ expect(cache_handler).to receive(:handle_request).with(*args)
104
+ expect(proxy_handler).to receive(:handle_request).with(*args)
105
+ expect(subject.handle_request(*args)).to eql(error: "Connection to url with body 'body' not cached and new http connections are disabled")
104
106
  end
105
107
  end
106
108
 
107
109
  describe '#stub' do
108
110
  it 'delegates to the stub_handler' do
109
- expect(stub_handler).to receive(:stub).with("some args")
110
- subject.stub("some args")
111
+ expect(stub_handler).to receive(:stub).with('some args')
112
+ subject.stub('some args')
111
113
  end
112
114
  end
113
115
 
114
116
  describe '#reset' do
115
117
  it 'resets all of the handlers' do
116
- handlers.each do |key,handler|
118
+ handlers.each do |_key, handler|
117
119
  expect(handler).to receive(:reset)
118
120
  end
119
121
  subject.reset
@@ -2,42 +2,44 @@ require 'spec_helper'
2
2
 
3
3
  describe Billy::StubHandler do
4
4
  let(:handler) { Billy::StubHandler.new }
5
- let(:request) { {
5
+ let(:request) do
6
+ {
6
7
  method: 'GET',
7
8
  url: 'http://example.test:8080/index?some=param',
8
- headers: {'Accept-Encoding' => 'gzip',
9
- 'Cache-Control' => 'no-cache' },
9
+ headers: { 'Accept-Encoding' => 'gzip',
10
+ 'Cache-Control' => 'no-cache' },
10
11
  body: 'Some body'
11
- } }
12
+ }
13
+ end
12
14
 
13
15
  describe '#handles_request?' do
14
16
  it 'handles the request if it is stubbed' do
15
- expect(handler).to receive(:find_stub).and_return("a stub")
16
- expect(handler.handles_request?(nil,nil,nil,nil)).to be true
17
+ expect(handler).to receive(:find_stub).and_return('a stub')
18
+ expect(handler.handles_request?(nil, nil, nil, nil)).to be true
17
19
  end
18
20
 
19
21
  it 'does not handle the request if it is not stubbed' do
20
22
  expect(handler).to receive(:find_stub).and_return(nil)
21
- expect(handler.handles_request?(nil,nil,nil,nil)).to be false
23
+ expect(handler.handles_request?(nil, nil, nil, nil)).to be false
22
24
  end
23
25
  end
24
26
 
25
27
  describe '#handle_request' do
26
28
  it 'returns nil if the request is not stubbed' do
27
29
  expect(handler).to receive(:handles_request?).and_return(false)
28
- expect(handler.handle_request(nil,nil,nil,nil)).to be nil
30
+ expect(handler.handle_request(nil, nil, nil, nil)).to be nil
29
31
  end
30
32
 
31
33
  it 'returns a response hash if the request is stubbed' do
32
- stub = double("stub", :call => [200, {'Content-Type' => 'application/json'}, "Some content"])
34
+ stub = double('stub', call: [200, { 'Content-Type' => 'application/json' }, 'Some content'])
33
35
  expect(handler).to receive(:handles_request?).and_return(true)
34
36
  expect(handler).to receive(:find_stub).and_return(stub)
35
37
  expect(handler.handle_request('GET',
36
38
  request[:url],
37
39
  request[:headers],
38
- request[:body])).to eql({:status => 200,
39
- :headers => {'Content-Type' => 'application/json'},
40
- :content => "Some content"})
40
+ request[:body])).to eql(status: 200,
41
+ headers: { 'Content-Type' => 'application/json' },
42
+ content: 'Some content')
41
43
  end
42
44
  end
43
45
 
@@ -3,27 +3,27 @@ require 'spec_helper'
3
3
  describe Billy::ProxyRequestStub do
4
4
  context '#matches?' do
5
5
  it 'should match urls and methods' do
6
- expect(Billy::ProxyRequestStub.new('http://example.com').
7
- matches?('GET', 'http://example.com')).to be
8
- expect(Billy::ProxyRequestStub.new('http://example.com').
9
- matches?('POST', 'http://example.com')).to_not be
10
-
11
- expect(Billy::ProxyRequestStub.new('http://example.com', :method => :get).
12
- matches?('GET', 'http://example.com')).to be
13
- expect(Billy::ProxyRequestStub.new('http://example.com', :method => :post).
14
- matches?('GET', 'http://example.com')).to_not be
15
-
16
- expect(Billy::ProxyRequestStub.new('http://example.com', :method => :post).
17
- matches?('POST', 'http://example.com')).to be
18
- expect(Billy::ProxyRequestStub.new('http://fooxample.com', :method => :post).
19
- matches?('POST', 'http://example.com')).to_not be
6
+ expect(Billy::ProxyRequestStub.new('http://example.com')
7
+ .matches?('GET', 'http://example.com')).to be
8
+ expect(Billy::ProxyRequestStub.new('http://example.com')
9
+ .matches?('POST', 'http://example.com')).to_not be
10
+
11
+ expect(Billy::ProxyRequestStub.new('http://example.com', method: :get)
12
+ .matches?('GET', 'http://example.com')).to be
13
+ expect(Billy::ProxyRequestStub.new('http://example.com', method: :post)
14
+ .matches?('GET', 'http://example.com')).to_not be
15
+
16
+ expect(Billy::ProxyRequestStub.new('http://example.com', method: :post)
17
+ .matches?('POST', 'http://example.com')).to be
18
+ expect(Billy::ProxyRequestStub.new('http://fooxample.com', method: :post)
19
+ .matches?('POST', 'http://example.com')).to_not be
20
20
  end
21
21
 
22
22
  it 'should match regexps' do
23
- expect(Billy::ProxyRequestStub.new(/http:\/\/.+\.com/, :method => :post).
24
- matches?('POST', 'http://example.com')).to be
25
- expect(Billy::ProxyRequestStub.new(/http:\/\/.+\.co\.uk/, :method => :get).
26
- matches?('GET', 'http://example.com')).to_not be
23
+ expect(Billy::ProxyRequestStub.new(/http:\/\/.+\.com/, method: :post)
24
+ .matches?('POST', 'http://example.com')).to be
25
+ expect(Billy::ProxyRequestStub.new(/http:\/\/.+\.co\.uk/, method: :get)
26
+ .matches?('GET', 'http://example.com')).to_not be
27
27
  end
28
28
 
29
29
  it 'should match up to but not including query strings' do
@@ -34,11 +34,24 @@ describe Billy::ProxyRequestStub do
34
34
  end
35
35
  end
36
36
 
37
- context "#call (without #and_return)" do
37
+ context "#matches? (with strip_query_params false in config)" do
38
+ before do
39
+ Billy.config.strip_query_params = false
40
+ end
41
+
42
+ it 'should not match up to request with query strings' do
43
+ stub = Billy::ProxyRequestStub.new('http://example.com/foo/bar/')
44
+ expect(stub.matches?('GET', 'http://example.com/foo/')).to_not be
45
+ expect(stub.matches?('GET', 'http://example.com/foo/bar/')).to be
46
+ expect(stub.matches?('GET', 'http://example.com/foo/bar/?baz=bap')).to_not be
47
+ end
48
+ end
49
+
50
+ context '#call (without #and_return)' do
38
51
  let(:subject) { Billy::ProxyRequestStub.new('url') }
39
52
 
40
- it "returns a 204 empty response" do
41
- expect(subject.call({}, {}, nil)).to eql [204, {"Content-Type" => "text/plain"}, ""]
53
+ it 'returns a 204 empty response' do
54
+ expect(subject.call({}, {}, nil)).to eql [204, { 'Content-Type' => 'text/plain' }, '']
42
55
  end
43
56
  end
44
57
 
@@ -46,7 +59,7 @@ describe Billy::ProxyRequestStub do
46
59
  let(:subject) { Billy::ProxyRequestStub.new('url') }
47
60
 
48
61
  it 'should generate bare responses' do
49
- subject.and_return :body => 'baz foo bar'
62
+ subject.and_return body: 'baz foo bar'
50
63
  expect(subject.call({}, {}, nil)).to eql [
51
64
  200,
52
65
  {},
@@ -55,75 +68,75 @@ describe Billy::ProxyRequestStub do
55
68
  end
56
69
 
57
70
  it 'should generate text responses' do
58
- subject.and_return :text => 'foo bar baz'
71
+ subject.and_return text: 'foo bar baz'
59
72
  expect(subject.call({}, {}, nil)).to eql [
60
73
  200,
61
- {'Content-Type' => 'text/plain'},
74
+ { 'Content-Type' => 'text/plain' },
62
75
  'foo bar baz'
63
76
  ]
64
77
  end
65
78
 
66
79
  it 'should generate JSON responses' do
67
- subject.and_return :json => { :foo => 'bar' }
80
+ subject.and_return json: { foo: 'bar' }
68
81
  expect(subject.call({}, {}, nil)).to eql [
69
82
  200,
70
- {'Content-Type' => 'application/json'},
83
+ { 'Content-Type' => 'application/json' },
71
84
  '{"foo":"bar"}'
72
85
  ]
73
86
  end
74
87
 
75
88
  context 'JSONP' do
76
89
  it 'should generate JSONP responses' do
77
- subject.and_return :jsonp => { :foo => 'bar' }
90
+ subject.and_return jsonp: { foo: 'bar' }
78
91
  expect(subject.call({ 'callback' => ['baz'] }, {}, nil)).to eql [
79
92
  200,
80
- {'Content-Type' => 'application/javascript'},
93
+ { 'Content-Type' => 'application/javascript' },
81
94
  'baz({"foo":"bar"})'
82
95
  ]
83
96
  end
84
97
 
85
98
  it 'should generate JSONP responses with custom callback parameter' do
86
- subject.and_return :jsonp => { :foo => 'bar' }, :callback_param => 'cb'
99
+ subject.and_return jsonp: { foo: 'bar' }, callback_param: 'cb'
87
100
  expect(subject.call({ 'cb' => ['bap'] }, {}, nil)).to eql [
88
101
  200,
89
- {'Content-Type' => 'application/javascript'},
102
+ { 'Content-Type' => 'application/javascript' },
90
103
  'bap({"foo":"bar"})'
91
104
  ]
92
105
  end
93
106
 
94
107
  it 'should generate JSONP responses with custom callback name' do
95
- subject.and_return :jsonp => { :foo => 'bar' }, :callback => 'cb'
108
+ subject.and_return jsonp: { foo: 'bar' }, callback: 'cb'
96
109
  expect(subject.call({}, {}, nil)).to eql [
97
110
  200,
98
- {'Content-Type' => 'application/javascript'},
111
+ { 'Content-Type' => 'application/javascript' },
99
112
  'cb({"foo":"bar"})'
100
113
  ]
101
114
  end
102
115
  end
103
116
 
104
117
  it 'should generate redirection responses' do
105
- subject.and_return :redirect_to => 'http://example.com'
118
+ subject.and_return redirect_to: 'http://example.com'
106
119
  expect(subject.call({}, {}, nil)).to eql [
107
120
  302,
108
- {'Location' => 'http://example.com'},
121
+ { 'Location' => 'http://example.com' },
109
122
  nil
110
123
  ]
111
124
  end
112
125
 
113
126
  it 'should set headers' do
114
- subject.and_return :text => 'foo', :headers => {'HTTP-X-Foo' => 'bar'}
127
+ subject.and_return text: 'foo', headers: { 'HTTP-X-Foo' => 'bar' }
115
128
  expect(subject.call({}, {}, nil)).to eql [
116
129
  200,
117
- {'Content-Type' => 'text/plain', 'HTTP-X-Foo' => 'bar'},
130
+ { 'Content-Type' => 'text/plain', 'HTTP-X-Foo' => 'bar' },
118
131
  'foo'
119
132
  ]
120
133
  end
121
134
 
122
135
  it 'should set status codes' do
123
- subject.and_return :text => 'baz', :code => 410
136
+ subject.and_return text: 'baz', code: 410
124
137
  expect(subject.call({}, {}, nil)).to eql [
125
138
  410,
126
- {'Content-Type' => 'text/plain'},
139
+ { 'Content-Type' => 'text/plain' },
127
140
  'baz'
128
141
  ]
129
142
  end
@@ -133,15 +146,15 @@ describe Billy::ProxyRequestStub do
133
146
  expected_headers = { 'header1' => 'three', 'header2' => 'four' }
134
147
  expected_body = 'body text'
135
148
 
136
- subject.and_return(Proc.new { |params, headers, body|
149
+ subject.and_return(proc do |params, headers, body|
137
150
  expect(params).to eql expected_params
138
151
  expect(headers).to eql expected_headers
139
152
  expect(body).to eql 'body text'
140
- {:code => 418, :text => 'success'}
141
- })
153
+ { code: 418, text: 'success' }
154
+ end)
142
155
  expect(subject.call(expected_params, expected_headers, expected_body)).to eql [
143
156
  418,
144
- {'Content-Type' => 'text/plain'},
157
+ { 'Content-Type' => 'text/plain' },
145
158
  'success'
146
159
  ]
147
160
  end