sitehub 0.4.10 → 0.5.0.alpha2

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,9 +7,7 @@ class SiteHub
7
7
  include_context :rack_request
8
8
 
9
9
  subject(:app) do
10
- described_class.new(sitehub_cookie_name: :cookie_name,
11
- id: :id,
12
- mapped_path: mapped_path,
10
+ described_class.new(mapped_path: mapped_path,
13
11
  mapped_url: mapped_url)
14
12
  end
15
13
 
@@ -17,14 +15,6 @@ class SiteHub
17
15
  stub_request(:get, mapped_url).to_return(body: 'body')
18
16
  end
19
17
 
20
- it 'includes Resolver' do
21
- expect(app).to be_a(Resolver)
22
- end
23
-
24
- it 'includes Rules' do
25
- expect(app).to be_a(Rules)
26
- end
27
-
28
18
  describe '#call' do
29
19
  let(:rack_headers) { {} }
30
20
  let(:request) { Request.new(env: env_for(path: mapped_path, env: rack_headers)) }
@@ -42,42 +32,6 @@ class SiteHub
42
32
  expect(request.mapped_path).to eq(mapped_path)
43
33
  expect(request.mapped_url).to eq(mapped_url)
44
34
  end
45
-
46
- context 'recorded routes cookie' do
47
- it 'drops a cookie using the name of the sitehub_cookie_name containing the id' do
48
- expect(last_response.cookies[:cookie_name.to_s]).to eq(value: :id.to_s, path: mapped_path)
49
- end
50
-
51
- context 'cookie already set' do
52
- let(:rack_headers) { { 'HTTP_COOKIE' => 'cookie_name=existing_value' } }
53
-
54
- it 'replaces the value as this is the proxy it should stick with' do
55
- expect(last_response.cookies[:cookie_name.to_s]).to eq(value: :id.to_s, path: mapped_path)
56
- end
57
- end
58
-
59
- context 'recorded_routes_cookie_path not set' do
60
- it 'sets the path to be the request path' do
61
- expect(last_response.cookies[:cookie_name.to_s][:path]).to eq(mapped_path)
62
- end
63
- end
64
-
65
- context 'recorded_routes_cookie_path set' do
66
- let(:expected_path) { '/expected_path' }
67
-
68
- subject(:app) do
69
- described_class.new(id: :id,
70
- sitehub_cookie_path: expected_path,
71
- sitehub_cookie_name: :cookie_name,
72
- mapped_path: mapped_path,
73
- mapped_url: mapped_url)
74
- end
75
-
76
- it 'is set as the path' do
77
- expect(last_response.cookies[:cookie_name.to_s][:path]).to eq(expected_path)
78
- end
79
- end
80
- end
81
35
  end
82
36
  end
83
37
  end
@@ -0,0 +1,64 @@
1
+ class SiteHub
2
+ module Middleware
3
+ describe ConfigLoader do
4
+ let(:server_url) { 'http://www.server.url' }
5
+
6
+ let(:config) do
7
+ {
8
+ proxies: [
9
+ {
10
+ path: '/route_1',
11
+ sitehub_cookie_name: 'sitehub.recorded_route',
12
+
13
+ splits: {},
14
+ routes: [
15
+ {
16
+ label: :label_1,
17
+ url: 'http://lvl-up.uk/'
18
+ }
19
+ ]
20
+ }
21
+ ]
22
+ }
23
+ end
24
+
25
+ before do
26
+ stub_request(:get, server_url).to_return(body: config.to_json)
27
+ end
28
+
29
+ subject do
30
+ described_class.new(:app, server_url)
31
+ end
32
+
33
+ describe '#load_config' do
34
+ it 'loads config' do
35
+ expect(subject.app).to be_nil
36
+ subject.load_config
37
+
38
+ expected_core = Core.new do
39
+ sitehub_cookie_name 'sitehub.recorded_route'
40
+ proxy '/route_1' do
41
+ route label: :label_1, url: 'http://lvl-up.uk/'
42
+ end
43
+ end.build
44
+
45
+ expect(subject.app).to eq(expected_core)
46
+ end
47
+ end
48
+
49
+ describe '#call' do
50
+ it 'calls the loaded_app' do
51
+ response = [200, {}, []]
52
+
53
+ app = proc do |env|
54
+ expect(env).to eq(:env)
55
+ response
56
+ end
57
+ expect(Core).to receive(:from_hash).and_return(double(build: app))
58
+
59
+ expect(subject.call(:env)).to eq(response)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,53 @@
1
+ # it 'extends the middleware with Resolver' do
2
+ # subject.build
3
+ # expect(subject.endpoints[:current]).to be_a(Resolver)
4
+ # end
5
+ #
6
+ # it 'extends the middleware with Rules' do
7
+ # subject.build
8
+ # expect(subject.endpoints[:current]).to be_a(Rules)
9
+ # end
10
+ #
11
+ # context 'rule on route' do
12
+
13
+ # it 'adds it to the rule to the middleware object' do
14
+ # subject.build
15
+ # expect(subject.endpoints[:current].rule).to eq(rule)
16
+ # end
17
+ # end
18
+
19
+ # context 'recorded routes cookie' do
20
+ # it 'drops a cookie using the name of the sitehub_cookie_name containing the id' do
21
+ # expect(last_response.cookies[:cookie_name.to_s]).to eq(value: :id.to_s, path: mapped_path)
22
+ # end
23
+ #
24
+ # context 'cookie already set' do
25
+ # let(:rack_headers) { { 'HTTP_COOKIE' => 'cookie_name=existing_value' } }
26
+ #
27
+ # it 'replaces the value as this is the proxy it should stick with' do
28
+ # expect(last_response.cookies[:cookie_name.to_s]).to eq(value: :id.to_s, path: mapped_path)
29
+ # end
30
+ # end
31
+ #
32
+ # context 'recorded_routes_cookie_path not set' do
33
+ # it 'sets the path to be the request path' do
34
+ # expect(last_response.cookies[:cookie_name.to_s][:path]).to eq(mapped_path)
35
+ # end
36
+ # end
37
+ #
38
+ # context 'recorded_routes_cookie_path set' do
39
+ # let(:expected_path) { '/expected_path' }
40
+ #
41
+ # subject(:app) do
42
+ # described_class.new(id: :id,
43
+ # sitehub_cookie_path: expected_path,
44
+ # sitehub_cookie_name: :cookie_name,
45
+ # mapped_path: mapped_path,
46
+ # mapped_url: mapped_url)
47
+ # end
48
+ #
49
+ # it 'is set as the path' do
50
+ # expect(last_response.cookies[:cookie_name.to_s][:path]).to eq(expected_path)
51
+ # end
52
+ # end
53
+ # end
@@ -1,10 +1,11 @@
1
- require 'sitehub/middleware/forward_proxies'
1
+ require 'sitehub/middleware/routes'
2
2
 
3
3
  class SiteHub
4
4
  module Middleware
5
- describe ForwardProxies do
5
+ describe Routes do
6
6
  let(:base_url) { 'http://google.com' }
7
- let(:application_root) { '/application_url' }
7
+ let(:mapped_path) { '/app' }
8
+ let(:mapped_path) { '/application_url' }
8
9
 
9
10
  let(:forward_proxy_builder) do
10
11
  subject.values.first
@@ -12,20 +13,73 @@ class SiteHub
12
13
 
13
14
  subject do
14
15
  base_url = base_url()
15
- described_class.new(:cookie_name).tap do |route_set|
16
- route_set.add_proxy(mapped_path: application_root) do |builder|
16
+ described_class.new.tap do |route_set|
17
+ route_set.add_route(mapped_path: mapped_path) do |builder|
17
18
  builder.split url: base_url, label: :current, percentage: 100
18
19
  end
19
- end
20
+ end.init
20
21
  end
21
22
 
22
23
  before do
23
24
  subject.init
24
25
  end
25
26
 
27
+ describe '#add_proxy' do
28
+ def route(app, id:)
29
+ Route.new(app,
30
+ id: id,
31
+ sitehub_cookie_name: RECORDED_ROUTES_COOKIE,
32
+ sitehub_cookie_path: nil)
33
+ end
34
+
35
+ context 'RouteBuilder as parameter' do
36
+ it 'sets it' do
37
+ another_mapping = '/mapping'
38
+ route = RouteBuilder.new(sitehub_cookie_name: :sitehub_cookie_name, mapped_path: another_mapping)
39
+ subject.add_route route_builder: route
40
+ expect(subject[another_mapping]).to be(route)
41
+ end
42
+ end
43
+
44
+ context 'no version explicitly defined' do
45
+ let(:expected_route) do
46
+ proxy = ForwardProxy.new(mapped_path: mapped_path, mapped_url: :url)
47
+ route(proxy, id: :default)
48
+ end
49
+
50
+ it 'adds a default proxy for the given mapping' do
51
+ subject.add_route(url: :url, mapped_path: mapped_path)
52
+ route = subject[mapped_path]
53
+ expect(route.default_proxy).to eq(expected_route)
54
+ end
55
+ end
56
+ end
57
+
58
+ describe '#call' do
59
+ let(:app) do
60
+ subject
61
+ end
62
+
63
+ context 'mapped_route not found' do
64
+ it 'returns a 404' do
65
+ expect(get('/missing').status).to eq(404)
66
+ end
67
+ end
68
+
69
+ context 'mapped_route found' do
70
+ it 'uses the forward proxy' do
71
+ subject
72
+ expect(forward_proxy_builder.endpoints[:current]).to receive(:call) do
73
+ [200, {}, []]
74
+ end
75
+ expect(get(mapped_path).status).to eq(200)
76
+ end
77
+ end
78
+ end
79
+
26
80
  describe '#init' do
27
81
  it 'builds all of the forward_proxies' do
28
- expect(subject[application_root]).to receive(:build).and_call_original
82
+ expect(subject[mapped_path]).to receive(:build).and_call_original
29
83
  subject.init
30
84
  end
31
85
  end
@@ -34,9 +88,10 @@ class SiteHub
34
88
  let(:request) { Rack::Request.new({}) }
35
89
 
36
90
  it 'uses the id in the sitehub_cookie to resolve the correct route' do
91
+ subject.sitehub_cookie_name :cookie_name
37
92
  request.cookies[:cookie_name] = :preset_id
38
93
  expect(forward_proxy_builder).to receive(:resolve).with(id: :preset_id, env: request.env).and_call_original
39
- subject.mapped_proxy(path: application_root, request: request)
94
+ subject.mapped_proxy(path: mapped_path, request: request)
40
95
  end
41
96
 
42
97
  context 'regex match on path' do
@@ -45,13 +100,13 @@ class SiteHub
45
100
  end
46
101
 
47
102
  subject do
48
- described_class.new(:cookie_name).tap do |route_set|
49
- route_set.add_proxy url: "#{base_url}/$1/view", mapped_path: %r{#{application_root}/(.*)/view}
103
+ described_class.new.tap do |route_set|
104
+ route_set.add_route url: "#{base_url}/$1/view", mapped_path: %r{#{mapped_path}/(.*)/view}
50
105
  end
51
106
  end
52
107
 
53
108
  it 'matches and subsitutes the captured group' do
54
- mapped_endpoint = subject.mapped_proxy(path: "#{application_root}/123/view", request: request)
109
+ mapped_endpoint = subject.mapped_proxy(path: "#{mapped_path}/123/view", request: request)
55
110
  expected_endpoint = fuzzy_matcher.resolve(env: {})
56
111
  expect(mapped_endpoint).to eq(expected_endpoint)
57
112
  end
@@ -59,7 +114,7 @@ class SiteHub
59
114
 
60
115
  context 'exact match on path' do
61
116
  it 'proxies to the requested path' do
62
- mapped_endpoint = subject.mapped_proxy(path: application_root, request: request)
117
+ mapped_endpoint = subject.mapped_proxy(path: mapped_path, request: request)
63
118
  expected_endpoint = forward_proxy_builder.resolve(env: {})
64
119
  expect(mapped_endpoint).to eq(expected_endpoint)
65
120
  end
@@ -71,45 +126,19 @@ class SiteHub
71
126
  end
72
127
 
73
128
  subject do
74
- described_class.new(:cookie_name).tap do |route_set|
75
- route_set.add_proxy(url: "#{base_url}/sub_url", mapped_path: "#{application_root}/sub_url")
76
- route_set.add_proxy(mapped_path: application_root, url: base_url)
129
+ described_class.new.tap do |route_set|
130
+ route_set.add_route(url: "#{base_url}/sub_url", mapped_path: "#{mapped_path}/sub_url")
131
+ route_set.add_route(mapped_path: mapped_path, url: base_url)
77
132
  end
78
133
  end
79
134
 
80
135
  it 'matches the first endpoint' do
81
136
  expected_endpoint = more_specific_proxy_builder.resolve(env: {})
82
- mapped_endpoint = subject.mapped_proxy(path: "#{application_root}/sub_url", request: request)
137
+ mapped_endpoint = subject.mapped_proxy(path: "#{mapped_path}/sub_url", request: request)
83
138
  expect(mapped_endpoint).to eq(expected_endpoint)
84
139
  end
85
140
  end
86
141
  end
87
-
88
- describe '#call' do
89
- context 'mapped_route not found' do
90
- let(:app) do
91
- subject
92
- end
93
-
94
- it 'returns a 404' do
95
- expect(get('/missing').status).to eq(404)
96
- end
97
- end
98
-
99
- context 'mapped_route found' do
100
- let(:app) do
101
- subject
102
- end
103
-
104
- it 'uses the forward proxy' do
105
- subject
106
- expect(forward_proxy_builder.endpoints[:current]).to receive(:call) do
107
- [200, {}, []]
108
- end
109
- expect(get(application_root).status).to eq(200)
110
- end
111
- end
112
- end
113
142
  end
114
143
  end
115
144
  end
@@ -1,6 +1,6 @@
1
- require 'sitehub/nil_proxy'
1
+ require 'sitehub/nil_route'
2
2
  class SiteHub
3
- describe NilProxy do
3
+ describe NilRoute do
4
4
  describe '#call' do
5
5
  let(:app) do
6
6
  described_class.new
@@ -1,12 +1,52 @@
1
1
  # rubocop:disable Metrics/ClassLength
2
- require 'sitehub/forward_proxy_builder'
2
+ require 'sitehub/route_builder'
3
3
 
4
4
  class SiteHub
5
- describe ForwardProxyBuilder do
5
+ describe RouteBuilder do
6
6
  include_context :middleware_test
7
7
 
8
+ describe '::from_hash' do
9
+ include_context :sitehub_json
10
+
11
+ subject do
12
+ described_class.from_hash(proxy_1, :expected).endpoints[route_1[:label]]
13
+ end
14
+
15
+ context 'splits' do
16
+ context 'sitehub_cookie_name' do
17
+ it 'sets it' do
18
+ expect(subject.sitehub_cookie_name).to eq(:expected)
19
+ end
20
+ end
21
+
22
+ context 'sitehub_cookie_path' do
23
+ it 'sets it' do
24
+ expect(subject.sitehub_cookie_path).to eq(proxy_1[:sitehub_cookie_path])
25
+ end
26
+ end
27
+
28
+ pending 'returns core with splits'
29
+ end
30
+
31
+ context 'routes' do
32
+ context 'sitehub_cookie_name' do
33
+ pending 'sets it'
34
+ end
35
+
36
+ context 'sitehub_cookie_path' do
37
+ pending 'sets it'
38
+ end
39
+ pending 'returns core with routes'
40
+ end
41
+
42
+ context 'default' do
43
+ it 'sets the default'
44
+ end
45
+ end
46
+
8
47
  subject do
9
- described_class.new(mapped_path: '/path', sitehub_cookie_name: :cookie_name)
48
+ described_class.new(sitehub_cookie_name: :cookie_name,
49
+ mapped_path: '/path')
10
50
  end
11
51
 
12
52
  it 'supports middleware' do
@@ -17,7 +57,11 @@ class SiteHub
17
57
  context 'with a block' do
18
58
  it 'evaluates the block in the context of the instance' do
19
59
  self_inside_block = nil
20
- instance = described_class.new(url: '/app1', mapped_path: '/path') { self_inside_block = self }
60
+ instance = described_class.new(sitehub_cookie_name: :name,
61
+ mapped_path: '/path') do
62
+ self_inside_block = self
63
+ default(url: :url)
64
+ end
21
65
  expect(self_inside_block).to eq(instance)
22
66
  end
23
67
  end
@@ -94,7 +138,8 @@ class SiteHub
94
138
  it 'stores a forward proxy builder' do
95
139
  subject.split(percentage: 50, &block)
96
140
 
97
- expected_builder = described_class.new(mapped_path: subject.mapped_path, &block).build
141
+ expected_builder = described_class.new(sitehub_cookie_name: :cookie_name,
142
+ mapped_path: subject.mapped_path, &block).build # sitehub_cookie_name: subject.sitehub_cookie_name
98
143
  expected_split = SiteHub::Collection::SplitRouteCollection::Split.new(0, 50, expected_builder)
99
144
  expect(subject.endpoints.values).to eq([expected_split])
100
145
  end
@@ -105,12 +150,15 @@ class SiteHub
105
150
  it 'stores a split for the version' do
106
151
  subject.split url: :url, label: :label, percentage: 50
107
152
 
108
- expected_proxy = ForwardProxy.new(id: :label,
109
- sitehub_cookie_name: :cookie_name,
110
- mapped_url: :url,
111
- mapped_path: subject.mapped_path)
153
+ proxy = ForwardProxy.new(mapped_url: :url,
154
+ mapped_path: subject.mapped_path)
155
+
156
+ expected_route = Route.new(proxy,
157
+ id: :label,
158
+ sitehub_cookie_name: :cookie_name,
159
+ sitehub_cookie_path: nil)
112
160
 
113
- expected = Collection::SplitRouteCollection.new(expected_proxy => 50)
161
+ expected = Collection::SplitRouteCollection.new(expected_route => 50)
114
162
 
115
163
  expect(subject.endpoints).to eq(expected)
116
164
  end
@@ -118,7 +166,7 @@ class SiteHub
118
166
  context 'url not supplied' do
119
167
  it 'raises an error' do
120
168
  expect { subject.split(label: :label, percentage: 50) }
121
- .to raise_error(ForwardProxyBuilder::InvalidDefinitionException)
169
+ .to raise_error(RouteBuilder::InvalidDefinitionException)
122
170
  end
123
171
  end
124
172
  end
@@ -128,7 +176,7 @@ class SiteHub
128
176
  subject.route url: :url, label: :label
129
177
 
130
178
  expect { subject.split(url: :url, label: :label, percentage: 50) }
131
- .to raise_error(ForwardProxyBuilder::InvalidDefinitionException)
179
+ .to raise_error(RouteBuilder::InvalidDefinitionException)
132
180
  end
133
181
  end
134
182
  end
@@ -136,11 +184,16 @@ class SiteHub
136
184
  describe '#route' do
137
185
  it 'accepts a rule' do
138
186
  subject.route url: :url, label: :current, rule: :rule
139
- expected_route = ForwardProxy.new(sitehub_cookie_name: :cookie_name,
140
- id: :current,
141
- rule: :rule,
142
- mapped_url: :url,
143
- mapped_path: subject.mapped_path)
187
+
188
+ proxy = ForwardProxy.new(mapped_url: :url,
189
+ mapped_path: subject.mapped_path)
190
+
191
+ expected_route = Route.new(proxy,
192
+ id: :current,
193
+ sitehub_cookie_name: :cookie_name,
194
+ sitehub_cookie_path: nil).tap do |route|
195
+ route.rule(:rule)
196
+ end
144
197
  expect(subject.endpoints).to eq(current: expected_route)
145
198
  end
146
199
 
@@ -179,7 +232,11 @@ class SiteHub
179
232
  rule = proc { true }
180
233
  subject.route(rule: rule, &block)
181
234
 
182
- expected_builder = described_class.new(rule: rule, mapped_path: subject.mapped_path, &block).build
235
+ expected_builder = described_class.new(sitehub_cookie_name: :cookie_name,
236
+ rule: rule, mapped_path: subject.mapped_path, &block).tap do |builder|
237
+ builder.sitehub_cookie_name subject.sitehub_cookie_name
238
+ end.build
239
+
183
240
  expect(subject.endpoints.values).to eq([expected_builder])
184
241
  end
185
242
 
@@ -201,67 +258,37 @@ class SiteHub
201
258
 
202
259
  context 'middleware not specified' do
203
260
  it 'leaves it the proxies alone' do
204
- subject.route url: :url, label: :current, rule: rule
205
- proxy_before_build = subject.endpoints[:current]
206
- subject.build
207
- proxy_after_build = subject.endpoints[:current]
208
- expect(proxy_after_build).to be(proxy_before_build)
209
- expect(proxy_after_build.rule).to be(rule)
210
- end
211
-
212
- it 'does not extend the endpoint with Resolver' do
213
- subject.route url: :url, label: :current, rule: rule
214
- expect(subject.endpoints[:current]).to_not receive(:extend).with(Resolver)
261
+ subject.route url: :url, label: :current
262
+ expect(subject.endpoints[:current]).to be_using_rack_stack(ForwardProxy)
215
263
  subject.build
264
+ expect(subject.endpoints[:current]).to be_using_rack_stack(ForwardProxy)
216
265
  end
217
266
  end
218
267
 
219
268
  context 'middleware specified' do
220
269
  before do
221
270
  subject.use middleware
222
- subject.route url: :url, label: :current, rule: rule
223
271
  end
224
272
 
225
273
  it 'wraps the forward proxies in the middleware' do
226
- proxy_before_build = subject.endpoints[:current]
227
-
228
- subject.build
229
- proxy_after_build = subject.endpoints[:current]
230
- expect(proxy_after_build).to be_a(middleware)
231
- expect(proxy_after_build.app).to be(proxy_before_build)
232
- end
233
-
234
- it 'extends the middleware with Resolver' do
235
- subject.build
236
- expect(subject.endpoints[:current]).to be_a(Resolver)
237
- end
238
-
239
- it 'extends the middleware with Rules' do
274
+ subject.route url: :url, label: :current
240
275
  subject.build
241
- expect(subject.endpoints[:current]).to be_a(Rules)
242
- end
243
-
244
- context 'rule on route' do
245
- it 'adds it to the rule to the middleware object' do
246
- subject.build
247
- expect(subject.endpoints[:current].rule).to eq(rule)
248
- end
276
+ expect(subject.endpoints[:current]).to be_using_rack_stack(middleware, ForwardProxy)
249
277
  end
250
278
 
251
279
  it 'wraps the default in the middleware' do
252
280
  subject.default url: :url
253
- default_proxy_before_build = subject.default_proxy
254
-
255
281
  subject.build
256
- default_proxy_after_build = subject.default_proxy
257
- expect(default_proxy_after_build).to be_a(middleware)
258
- expect(default_proxy_after_build.app).to be(default_proxy_before_build)
282
+ expect(subject.default_proxy).to be_using_rack_stack(middleware, ForwardProxy)
259
283
  end
260
284
  end
261
285
  end
262
286
 
263
287
  describe '#resolve' do
264
- subject { described_class.new(mapped_path: '/') }
288
+ subject do
289
+ described_class.new(sitehub_cookie_name: :cookie_name,
290
+ mapped_path: '/')
291
+ end
265
292
 
266
293
  context 'routes defined' do
267
294
  it 'returns that route' do
@@ -337,7 +364,8 @@ class SiteHub
337
364
 
338
365
  describe '#forward_proxy' do
339
366
  subject do
340
- described_class.new(mapped_path: '/path', sitehub_cookie_name: :expected_cookie_name)
367
+ described_class.new(sitehub_cookie_name: :cookie_name,
368
+ mapped_path: '/path')
341
369
  end
342
370
 
343
371
  it 'sets the sitehub_cookie_path' do
@@ -347,6 +375,7 @@ class SiteHub
347
375
  end
348
376
 
349
377
  it 'sets the sitehub_cookie_name' do
378
+ subject.sitehub_cookie_name :expected_cookie_name
350
379
  proxy = subject.forward_proxy(label: :label, url: :url)
351
380
  expect(proxy.sitehub_cookie_name).to eq(:expected_cookie_name)
352
381
  end