sitehub 0.5.0.alpha8 → 0.5.0.alpha10

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2389132d290dc9bcff1fca5999cc75802e31af1a
4
- data.tar.gz: dfca66250eac7e57b5ec53002d2c662ede650688
3
+ metadata.gz: b127101beee3301abb96d447bcc5a89079edbc61
4
+ data.tar.gz: bdb07a1212a2ddc317e90bb0f357aa710707b3d9
5
5
  SHA512:
6
- metadata.gz: 27a39c2ac4aea35dcf4ae1b73a7ad8fd77c23f3f0fc1e22c4829a0052eec38cec2d1d4eaac32e84906e442297855eefa7afce5e6eccf1a33605a04ca84fcd001
7
- data.tar.gz: fb3e3f8a9d9eb65ef2ad3b714bec96a716cbaf5d8b0fde70dd5e5418a887901e7f07403f4e63249e4f736ce4e10a7f723afe90d21b2312803e0c22de771c4341
6
+ metadata.gz: 56bdcc1643bc86eb0a1f16aa06d6e0da5b21cfe7fc8edaab4801b96ae16fc859ffb986f4286c3e05e01c57ae4c608b94a1f94d8627d53de897516e5d7ded34ab
7
+ data.tar.gz: a9e5fbe7fc778646af3de85e2061df64e2704292f7b7dd15f7ad43fe3bde2adf59cd4a8fc75d2bd10132e7ad4cc872fb3c48c21b9a4bae72280653589bfb68f1
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sitehub (0.5.0.alpha8)
4
+ sitehub (0.5.0.alpha10)
5
5
  activesupport
6
6
  em-http-request
7
7
  em-synchrony
@@ -18,6 +18,10 @@ class SiteHub
18
18
  class InvalidDefinitionError < StandardError
19
19
  end
20
20
 
21
+ INVALID_PATH_MATCHER = 'Matcher for path (%s) was not a valid regexp: %s'.freeze
22
+ class InvalidPathMatcherError < StandardError
23
+ end
24
+
21
25
  ROUTES_WITH_SPLITS_MSG = 'you cant register routes and splits at the same level'.freeze
22
26
  INVALID_SPLIT_MSG = 'url must be defined if not supplying a block'.freeze
23
27
  RULE_NOT_SPECIFIED_MSG = 'rule must be supplied'.freeze
@@ -29,7 +33,8 @@ class SiteHub
29
33
  include Rules, Equality, Middleware
30
34
 
31
35
  getter_setters :sitehub_cookie_path, :sitehub_cookie_name
32
- attr_reader :mapped_path, :id, :calling_scope
36
+ attr_reader :id, :calling_scope
37
+ attr_accessor :mapped_path
33
38
 
34
39
  transient :calling_scope
35
40
 
@@ -86,6 +91,8 @@ class SiteHub
86
91
  def initialize(id: nil, sitehub_cookie_name:, sitehub_cookie_path: nil, mapped_path:, rule: nil, calling_scope: nil, &block)
87
92
  @id = Identifier.new(id)
88
93
  @calling_scope = calling_scope
94
+
95
+ mapped_path = string_to_regexp(mapped_path) if string_containing_regexp?(mapped_path)
89
96
  @mapped_path = mapped_path
90
97
  @sitehub_cookie_name = sitehub_cookie_name
91
98
  @sitehub_cookie_path = sitehub_cookie_path
@@ -138,6 +145,18 @@ class SiteHub
138
145
  candidates[Identifier.new(key)]
139
146
  end
140
147
 
148
+ def string_containing_regexp?(obj)
149
+ return false unless obj.is_a?(String)
150
+ obj.start_with?('%r{') && obj.end_with?('}')
151
+ end
152
+
153
+ def string_to_regexp(mapped_path)
154
+ regexp_string = mapped_path.to_s.sub(/^%r{/, '').sub(/}$/, '')
155
+ Regexp.compile(regexp_string)
156
+ rescue RegexpError => e
157
+ raise InvalidPathMatcherError, format(INVALID_PATH_MATCHER, regexp_string, e.message)
158
+ end
159
+
141
160
  private
142
161
 
143
162
  def add_middleware_to_proxy(proxy)
@@ -9,10 +9,6 @@ require 'em-http'
9
9
  class SiteHub
10
10
  module Middleware
11
11
  class CandidateRouteMappings < Hash
12
- INVALID_PATH_MATCHER = 'Matcher for path (%s) was not a valid regexp: %s'.freeze
13
- class InvalidPathMatcherError < StandardError
14
- end
15
-
16
12
  NIL_ROUTE = NilRoute.new
17
13
 
18
14
  include Equality
@@ -38,18 +34,15 @@ class SiteHub
38
34
  end
39
35
 
40
36
  def add_route(url: nil, mapped_path: nil, route_builder: nil, &block)
41
- if route_builder
42
- self[route_builder.mapped_path] = route_builder
43
- return
37
+ unless route_builder
38
+ route_builder = CandidateRoutes.new(sitehub_cookie_name: sitehub_cookie_name,
39
+ mapped_path: mapped_path,
40
+ &block).tap do |builder|
41
+ builder.default(url: url) if url
42
+ end
44
43
  end
45
44
 
46
- mapped_path = string_to_regexp(mapped_path) if string_containing_regexp?(mapped_path)
47
-
48
- self[mapped_path] = CandidateRoutes.new(sitehub_cookie_name: sitehub_cookie_name,
49
- mapped_path: mapped_path,
50
- &block).tap do |builder|
51
- builder.default(url: url) if url
52
- end
45
+ self[route_builder.mapped_path] = route_builder
53
46
  end
54
47
 
55
48
  def mapped_route(path:, request:)
@@ -66,20 +59,6 @@ class SiteHub
66
59
  end
67
60
  end
68
61
  end
69
-
70
- def string_containing_regexp?(obj)
71
- return false unless obj.is_a?(String)
72
- obj.start_with?('%r{') && obj.end_with?('}')
73
- end
74
-
75
- private
76
-
77
- def string_to_regexp(mapped_path)
78
- regexp_string = mapped_path.to_s.sub(/^%r{/, '').sub(/}$/, '')
79
- Regexp.compile(regexp_string)
80
- rescue RegexpError => e
81
- raise InvalidPathMatcherError, format(INVALID_PATH_MATCHER, regexp_string, e.message)
82
- end
83
62
  end
84
63
  end
85
64
  end
@@ -27,11 +27,10 @@ class SiteHub
27
27
 
28
28
  # TODO: handle errors connecting to the config server
29
29
  def load_config
30
- config = cache.fetch(:sitehub_config, caching_options) do
31
- config_server.get
30
+ unless cache.exist?(:sitehub_config)
31
+ cache.write(:sitehub_config, :retrieved, caching_options)
32
+ @app = Core.from_hash(config_server.get).build
32
33
  end
33
-
34
- @app = Core.from_hash(config).build
35
34
  end
36
35
  end
37
36
  end
@@ -1,3 +1,3 @@
1
1
  class SiteHub
2
- VERSION = '0.5.0.alpha8'.freeze
2
+ VERSION = '0.5.0.alpha10'.freeze
3
3
  end
@@ -0,0 +1,34 @@
1
+ describe 'config server' do
2
+ include_context :integration
3
+ include_context :sitehub_json
4
+
5
+ let(:config) do
6
+ { proxies: [
7
+ {
8
+ path: '%r{/regex(.*)}',
9
+ default: DOWNSTREAM_URL
10
+ }
11
+ ] }
12
+ end
13
+
14
+ let(:downstream_response) { 'downstream response' }
15
+
16
+ before do
17
+ stub_request(:get, CONFIG_SERVER_URL).and_return(body: config.to_json)
18
+ stub_request(:get, DOWNSTREAM_URL).and_return(body: downstream_response)
19
+ end
20
+
21
+ let(:app) do
22
+ sitehub = sitehub do
23
+ config_server(CONFIG_SERVER_URL, caching_options: { expires_in: 1 })
24
+ end
25
+
26
+ Async::Middleware.new(sitehub)
27
+ end
28
+
29
+ it 'path as regex' do
30
+ get('/regex123')
31
+
32
+ expect(app.last_response.body).to eq([downstream_response])
33
+ end
34
+ end
@@ -1,11 +1,7 @@
1
1
  require 'stringio'
2
2
  describe 'error handling' do
3
3
  context 'using config server' do
4
- CONFIG_SERVER_URL = 'http://the.config.server'.freeze
5
- DOWNSTREAM_URL = 'http://downstream.url'.freeze
6
- ERROR_LOGGER = StringIO.new
7
- ACCESS_LOGGER = StringIO.new
8
-
4
+ include_context :integration
9
5
  include_context :sitehub_json
10
6
 
11
7
  let(:config) do
@@ -23,10 +19,8 @@ describe 'error handling' do
23
19
  stub_request(:get, DOWNSTREAM_URL).and_return(body: downstream_response)
24
20
  end
25
21
  let(:app) do
26
- sitehub = SiteHub.build do
27
- config_server(CONFIG_SERVER_URL, caching_options: { force: true })
28
- error_logger ERROR_LOGGER
29
- access_logger ACCESS_LOGGER
22
+ sitehub = sitehub do
23
+ config_server(CONFIG_SERVER_URL, caching_options: { expires_in: 1 })
30
24
  end
31
25
  Async::Middleware.new(sitehub)
32
26
  end
@@ -63,7 +57,9 @@ describe 'error handling' do
63
57
  end
64
58
 
65
59
  it 'uses the old config' do
60
+ Timecop.travel(1)
66
61
  get('/')
62
+
67
63
  expect(app.last_response.body).to eq([downstream_response])
68
64
  end
69
65
 
@@ -47,7 +47,6 @@ class SiteHub
47
47
  subject do
48
48
  described_class.from_hash(routes_proxy, :expected)
49
49
  end
50
-
51
50
  context 'sitehub_cookie_name' do
52
51
  it 'sets it' do
53
52
  expect(subject.sitehub_cookie_name).to eq(:expected)
@@ -192,6 +191,23 @@ class SiteHub
192
191
  end
193
192
  end
194
193
  end
194
+
195
+ context 'mapped_path' do
196
+ context 'is a string containing malformed regexp' do
197
+ let(:mapped_path) { '%r{*}' }
198
+
199
+ it 'raises an error' do
200
+ expected_message = begin
201
+ Regexp.compile('*')
202
+ rescue RegexpError => e
203
+ format(described_class::INVALID_PATH_MATCHER, '*', e.message)
204
+ end
205
+
206
+ expect { described_class.new(sitehub_cookie_name: '', mapped_path: '%r{*}') }
207
+ .to raise_error(described_class::InvalidPathMatcherError, expected_message)
208
+ end
209
+ end
210
+ end
195
211
  end
196
212
 
197
213
  describe 'valid?' do
@@ -491,6 +507,28 @@ class SiteHub
491
507
  end
492
508
  end
493
509
 
510
+ describe '#string_containing_regexp?' do
511
+ context 'parameter is not a string' do
512
+ it 'it returns false' do
513
+ expect(subject.string_containing_regexp?(//)).to eq(false)
514
+ end
515
+ end
516
+
517
+ context 'parameter is a string' do
518
+ context 'contains a regexp' do
519
+ it 'it returns true' do
520
+ expect(subject.string_containing_regexp?('%r{}')).to be(true)
521
+ end
522
+ end
523
+
524
+ context 'does not contain a regexp' do
525
+ it 'returns false' do
526
+ expect(subject.string_containing_regexp?('')).to eq(false)
527
+ end
528
+ end
529
+ end
530
+ end
531
+
494
532
  describe '#method_missing' do
495
533
  context 'calling scope set' do
496
534
  subject do
@@ -70,20 +70,6 @@ class SiteHub
70
70
  expect(subject[expected_mapping][:current]).to eq(expected_route)
71
71
  end
72
72
  end
73
-
74
- context 'is a string containing malformed regexp' do
75
- let(:mapped_path) { '%r{*}' }
76
-
77
- it 'raises an error' do
78
- expected_message = begin
79
- Regexp.compile('*')
80
- rescue RegexpError => e
81
- format(described_class::INVALID_PATH_MATCHER, '*', e.message)
82
- end
83
-
84
- expect { subject }.to raise_error(described_class::InvalidPathMatcherError, expected_message)
85
- end
86
- end
87
73
  end
88
74
 
89
75
  context 'url specified' do
@@ -190,28 +176,6 @@ class SiteHub
190
176
  end
191
177
  end
192
178
  end
193
-
194
- describe '#string_containing_regexp?' do
195
- context 'parameter is not a string' do
196
- it 'it returns false' do
197
- expect(subject.string_containing_regexp?(//)).to eq(false)
198
- end
199
- end
200
-
201
- context 'parameter is a string' do
202
- context 'contains a regexp' do
203
- it 'it returns true' do
204
- expect(subject.string_containing_regexp?('%r{}')).to be(true)
205
- end
206
- end
207
-
208
- context 'does not contain a regexp' do
209
- it 'returns false' do
210
- expect(subject.string_containing_regexp?('')).to eq(false)
211
- end
212
- end
213
- end
214
- end
215
179
  end
216
180
  end
217
181
  end
@@ -1,3 +1,4 @@
1
+ require 'timecop'
1
2
  class SiteHub
2
3
  module Middleware
3
4
  describe ConfigLoader do
@@ -26,15 +27,14 @@ class SiteHub
26
27
  stub_request(:get, server_url).to_return(body: config.to_json)
27
28
  end
28
29
 
29
- let(:no_caching) { { force: true } }
30
+ let(:cache_settings) { { expires_in: 1 } }
30
31
  subject do
31
- described_class.new(:app, server_url, caching_options: no_caching)
32
+ described_class.new(:app, server_url, caching_options: cache_settings)
32
33
  end
33
34
 
34
35
  describe '#load_config' do
35
36
  subject do
36
- caching_enabled = { expires_in: 30 }
37
- described_class.new(:app, server_url, caching_options: caching_enabled)
37
+ described_class.new(:app, server_url, caching_options: cache_settings)
38
38
  end
39
39
 
40
40
  let(:expected_core) do
@@ -101,7 +101,7 @@ class SiteHub
101
101
 
102
102
  context 'config previously loaded' do
103
103
  subject do
104
- described_class.new(:app, server_url, caching_options: no_caching)
104
+ described_class.new(:app, server_url, caching_options: cache_settings)
105
105
  end
106
106
 
107
107
  let(:response) { [200, {}, []] }
@@ -113,6 +113,7 @@ class SiteHub
113
113
  expect(config_server).to receive(:get).and_return(config)
114
114
  expect(Core).to receive(:from_hash).with(config).and_return(double(build: app))
115
115
  subject.call(env)
116
+ Timecop.travel(2)
116
117
  end
117
118
 
118
119
  it 'retains the original config' do
data/spec/spec_helper.rb CHANGED
@@ -22,11 +22,16 @@ require 'lib/sitehub'
22
22
 
23
23
  RSpec.configure do |config|
24
24
  include Rack::Test::Methods
25
+
25
26
  config.before do
26
27
  Timecop.freeze
27
28
  WebMock.enable!
28
29
  end
29
30
 
31
+ config.after do
32
+ Timecop.return
33
+ end
34
+
30
35
  config.after(:suite) do
31
36
  WebMock.disable!
32
37
  end
@@ -0,0 +1,19 @@
1
+
2
+
3
+ shared_context :integration do
4
+ unless const_defined?(:CONFIG_SERVER_URL)
5
+ CONFIG_SERVER_URL = 'http://the.config.server'.freeze
6
+ DOWNSTREAM_URL = 'http://downstream.url'.freeze
7
+ ERROR_LOGGER = StringIO.new
8
+ ACCESS_LOGGER = StringIO.new
9
+ end
10
+
11
+ def sitehub(&block)
12
+ builder_block = proc do
13
+ access_logger ACCESS_LOGGER
14
+ error_logger ERROR_LOGGER
15
+ instance_eval(&block)
16
+ end
17
+ SiteHub.build(&builder_block)
18
+ end
19
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sitehub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0.alpha8
4
+ version: 0.5.0.alpha10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ladtech
@@ -391,6 +391,7 @@ files:
391
391
  - sitehub.gemspec
392
392
  - spec/equality_spec.rb
393
393
  - spec/integration/access_logs_spec.rb
394
+ - spec/integration/config_server_spec.rb
394
395
  - spec/integration/error_handling_spec.rb
395
396
  - spec/integration/middleware_spec.rb
396
397
  - spec/integration/version_affinity_spec.rb
@@ -441,6 +442,7 @@ files:
441
442
  - spec/support/patch/rack/response.rb
442
443
  - spec/support/shared_contexts.rb
443
444
  - spec/support/shared_contexts/http_proxy_rules_context.rb
445
+ - spec/support/shared_contexts/integration_context.rb
444
446
  - spec/support/shared_contexts/middleware_context.rb
445
447
  - spec/support/shared_contexts/module_spec_context.rb
446
448
  - spec/support/shared_contexts/rack_request_context.rb