sitehub 0.4.2 → 0.4.3
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 +4 -4
- data/.rubocop.yml +17 -0
- data/Gemfile +0 -2
- data/Gemfile.lock +15 -1
- data/README.md +23 -0
- data/Rakefile +3 -9
- data/circle.yml +6 -0
- data/lib/sitehub/builder.rb +19 -19
- data/lib/sitehub/collection/route_collection.rb +7 -9
- data/lib/sitehub/collection/split_route_collection/split.rb +7 -5
- data/lib/sitehub/collection/split_route_collection.rb +13 -10
- data/lib/sitehub/collection.rb +26 -21
- data/lib/sitehub/constants/http_header_keys.rb +2 -6
- data/lib/sitehub/constants/rack_http_header_keys.rb +2 -2
- data/lib/sitehub/constants.rb +3 -3
- data/lib/sitehub/cookie/attribute.rb +4 -4
- data/lib/sitehub/cookie/flag.rb +4 -5
- data/lib/sitehub/cookie.rb +10 -14
- data/lib/sitehub/cookie_rewriting.rb +11 -13
- data/lib/sitehub/forward_proxies.rb +12 -13
- data/lib/sitehub/forward_proxy.rb +38 -24
- data/lib/sitehub/forward_proxy_builder.rb +38 -21
- data/lib/sitehub/http_headers.rb +39 -26
- data/lib/sitehub/logging/access_logger.rb +39 -35
- data/lib/sitehub/logging/error_logger.rb +7 -7
- data/lib/sitehub/logging/log_entry.rb +6 -5
- data/lib/sitehub/logging/log_stash.rb +2 -2
- data/lib/sitehub/logging/log_wrapper.rb +5 -5
- data/lib/sitehub/logging.rb +1 -1
- data/lib/sitehub/middleware.rb +2 -2
- data/lib/sitehub/path_directive.rb +10 -10
- data/lib/sitehub/path_directives.rb +5 -4
- data/lib/sitehub/request_mapping.rb +13 -11
- data/lib/sitehub/resolver.rb +5 -6
- data/lib/sitehub/reverse_proxy.rb +16 -12
- data/lib/sitehub/rules.rb +2 -2
- data/lib/sitehub/string_sanitiser.rb +1 -1
- data/lib/sitehub/transaction_id.rb +3 -3
- data/lib/sitehub/version.rb +1 -1
- data/lib/sitehub.rb +1 -1
- data/sitehub.gemspec +27 -29
- data/spec/sitehub/builder_spec.rb +18 -20
- data/spec/sitehub/collection/route_collection_spec.rb +16 -14
- data/spec/sitehub/collection/split_route_collection_spec.rb +8 -10
- data/spec/sitehub/collection_spec.rb +7 -7
- data/spec/sitehub/cookie/attribute_spec.rb +3 -3
- data/spec/sitehub/cookie/flag_spec.rb +2 -2
- data/spec/sitehub/cookie_rewriting_spec.rb +15 -12
- data/spec/sitehub/cookie_spec.rb +7 -18
- data/spec/sitehub/error_handling_spec.rb +2 -3
- data/spec/sitehub/forward_proxies_spec.rb +16 -12
- data/spec/sitehub/forward_proxy_builder_spec.rb +53 -30
- data/spec/sitehub/forward_proxy_spec.rb +26 -22
- data/spec/sitehub/http_headers_spec.rb +17 -18
- data/spec/sitehub/integration_spec.rb +4 -5
- data/spec/sitehub/logging/access_logger_spec.rb +25 -24
- data/spec/sitehub/logging/error_logger_spec.rb +5 -7
- data/spec/sitehub/logging/log_entry_spec.rb +2 -5
- data/spec/sitehub/logging/log_stash_spec.rb +1 -3
- data/spec/sitehub/logging/log_wrapper_spec.rb +0 -4
- data/spec/sitehub/middleware_spec.rb +1 -5
- data/spec/sitehub/path_directive_spec.rb +4 -7
- data/spec/sitehub/path_directives_spec.rb +6 -7
- data/spec/sitehub/request_mapping_spec.rb +2 -5
- data/spec/sitehub/resolver_spec.rb +1 -1
- data/spec/sitehub/reverse_proxy_spec.rb +37 -31
- data/spec/sitehub/transaction_id_spec.rb +3 -3
- data/spec/sitehub_spec.rb +2 -4
- data/spec/support/async/callback.rb +11 -0
- data/spec/support/async/middleware.rb +25 -0
- data/spec/support/async/response_handler.rb +16 -0
- data/spec/support/async.rb +4 -0
- data/spec/support/patch/rack/response.rb +13 -21
- data/spec/support/shared_contexts/async_context.rb +3 -58
- data/spec/support/shared_contexts/middleware_context.rb +15 -17
- data/spec/support/shared_contexts/rack_test_context.rb +3 -3
- data/spec/support/shared_contexts/sitehub_context.rb +9 -4
- data/spec/support/silent_warnings.rb +2 -3
- data/tasks/code_quality.rake +15 -0
- data/tasks/gem_tasks.rake +1 -0
- data/tasks/support/console.rb +7 -0
- data/tasks/testing.rake +4 -0
- data/tasks/util_tasks.rake +7 -0
- metadata +27 -3
- data/spec/basket_spec.rb +0 -30
@@ -4,40 +4,42 @@ class SiteHub
|
|
4
4
|
attr_reader :source_url, :mapped_url, :mapped_path
|
5
5
|
|
6
6
|
BASE_URL_MATCHER = %r{^\w+://[\w+\.-]+(:\d+)?}
|
7
|
-
CAPTURE_GROUP_REFERENCE='$%s'
|
7
|
+
CAPTURE_GROUP_REFERENCE = '$%s'.freeze
|
8
8
|
USER_SUPPLIED_CAPTURE = 1..-1
|
9
9
|
|
10
10
|
def initialize(source_url:, downstream_url: EMPTY_STRING, mapped_url: EMPTY_STRING, mapped_path:)
|
11
|
-
@source_url
|
11
|
+
@source_url = source_url
|
12
|
+
@mapped_url = mapped_url.dup
|
12
13
|
@mapped_path = mapped_path.is_a?(Regexp) ? mapped_path : Regexp.new(mapped_path)
|
13
14
|
@downstream_url = downstream_url
|
14
15
|
end
|
15
16
|
|
16
17
|
def cookie_path
|
17
|
-
if mapped_path.is_a?(Regexp)
|
18
|
-
mapped_path.source[/^(.*)?\(/,1].gsub(/\/$/, '')
|
19
|
-
end
|
18
|
+
mapped_path.source[/^(.*)?\(/, 1].gsub(%r{/$}, '') if mapped_path.is_a?(Regexp)
|
20
19
|
end
|
21
20
|
|
22
|
-
|
23
21
|
def computed_uri
|
24
22
|
@computed_uri ||= begin
|
25
23
|
url_components = url_scanner_regex.match(source_url).captures[USER_SUPPLIED_CAPTURE]
|
26
24
|
mapped_url.tap do |url|
|
27
25
|
url_components.each_with_index do |match, index|
|
28
|
-
url.gsub!(CAPTURE_GROUP_REFERENCE % (index+1), match)
|
26
|
+
url.gsub!(CAPTURE_GROUP_REFERENCE % (index + 1), match)
|
29
27
|
end
|
30
28
|
end
|
31
29
|
end
|
32
30
|
end
|
33
31
|
|
34
|
-
def ==
|
35
|
-
other.is_a?(RequestMapping) &&
|
32
|
+
def ==(other)
|
33
|
+
other.is_a?(RequestMapping) &&
|
34
|
+
source_url == other.source_url &&
|
35
|
+
mapped_url == other.mapped_url &&
|
36
|
+
mapped_path == other.mapped_path
|
36
37
|
end
|
37
38
|
|
38
39
|
private
|
40
|
+
|
39
41
|
def url_scanner_regex
|
40
|
-
|
42
|
+
/#{BASE_URL_MATCHER.source}#{mapped_path.source}/
|
41
43
|
end
|
42
44
|
end
|
43
|
-
end
|
45
|
+
end
|
data/lib/sitehub/resolver.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
=end
|
1
|
+
# TODO: - this helper module is for the benefit of middleware that may not otherwse respond to resolve.
|
2
|
+
# THis is to ensure that middleware wrapping the core forward proxy apps had the resolve method also.
|
3
|
+
#
|
5
4
|
class SiteHub
|
6
5
|
module Resolver
|
7
|
-
def resolve(*
|
6
|
+
def resolve(*_args)
|
8
7
|
self
|
9
8
|
end
|
10
9
|
end
|
11
|
-
end
|
10
|
+
end
|
@@ -9,14 +9,12 @@ class SiteHub
|
|
9
9
|
|
10
10
|
attr_reader :path_directives
|
11
11
|
|
12
|
-
def initialize
|
12
|
+
def initialize(app, directives)
|
13
13
|
@app = app
|
14
14
|
@path_directives = PathDirectives.new(directives)
|
15
15
|
end
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
def call env
|
17
|
+
def call(env)
|
20
18
|
downstream_response = @app.call(env)
|
21
19
|
|
22
20
|
request_mapping = env[REQUEST_MAPPING]
|
@@ -24,19 +22,25 @@ class SiteHub
|
|
24
22
|
begin
|
25
23
|
downstream_status, downstream_headers, downstream_body = downstream_response.to_a
|
26
24
|
|
27
|
-
|
28
|
-
downstream_uri = URI(downstream_headers[HttpHeaders::LOCATION_HEADER])
|
29
|
-
mapped_downstream_uri = URI(request_mapping.mapped_url)
|
30
|
-
if downstream_uri.host == mapped_downstream_uri.host && downstream_uri.port == mapped_downstream_uri.port
|
31
|
-
downstream_headers[HttpHeaders::LOCATION_HEADER] = interpolate_location(downstream_headers[HttpHeaders::LOCATION_HEADER], request_mapping.source_url)
|
32
|
-
end
|
33
|
-
end
|
25
|
+
transform_headers(downstream_headers, request_mapping)
|
34
26
|
|
35
|
-
|
27
|
+
if downstream_headers[HttpHeaders::SET_COOKIE]
|
28
|
+
rewrite_cookies(downstream_headers, substitute_domain: URI(request_mapping.source_url).host)
|
29
|
+
end
|
36
30
|
|
37
31
|
Rack::Response.new(downstream_body, downstream_status, downstream_headers)
|
38
32
|
end
|
33
|
+
end
|
39
34
|
|
35
|
+
def transform_headers(downstream_headers, request_mapping)
|
36
|
+
if downstream_headers[HttpHeaders::LOCATION_HEADER]
|
37
|
+
downstream_uri = URI(downstream_headers[HttpHeaders::LOCATION_HEADER])
|
38
|
+
mapped_downstream_uri = URI(request_mapping.mapped_url)
|
39
|
+
if downstream_uri.host == mapped_downstream_uri.host && downstream_uri.port == mapped_downstream_uri.port
|
40
|
+
location = interpolate_location(downstream_headers[HttpHeaders::LOCATION_HEADER], request_mapping.source_url)
|
41
|
+
downstream_headers[HttpHeaders::LOCATION_HEADER] = location
|
42
|
+
end
|
43
|
+
end
|
40
44
|
end
|
41
45
|
|
42
46
|
def interpolate_location(old_location, source_url)
|
data/lib/sitehub/rules.rb
CHANGED
@@ -3,12 +3,12 @@ require 'sitehub/constants'
|
|
3
3
|
class SiteHub
|
4
4
|
class TransactionId
|
5
5
|
include Constants
|
6
|
-
def initialize
|
6
|
+
def initialize(app)
|
7
7
|
@app = app
|
8
8
|
end
|
9
9
|
|
10
|
-
#TODO - don't overwrite
|
11
|
-
def call
|
10
|
+
# TODO: - don't overwrite
|
11
|
+
def call(env)
|
12
12
|
env[RackHttpHeaderKeys::TRANSACTION_ID] ||= UUID.generate(:compact)
|
13
13
|
@app.call env
|
14
14
|
end
|
data/lib/sitehub/version.rb
CHANGED
data/lib/sitehub.rb
CHANGED
data/sitehub.gemspec
CHANGED
@@ -4,40 +4,38 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'sitehub/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'sitehub'
|
8
8
|
spec.version = SiteHub::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
9
|
+
spec.authors = ['Ladtech']
|
10
|
+
spec.email = ['team@lad-tech.com']
|
11
|
+
spec.summary = 'A/B testing enabled HTTP proxy'
|
12
|
+
spec.description = 'A/B testing enabled HTTP proxy'
|
13
|
+
spec.homepage = ''
|
14
|
+
spec.license = 'MIT'
|
15
15
|
|
16
|
-
spec.files = `git ls-files`.split(
|
16
|
+
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(spec)/*.rb})
|
19
|
-
spec.require_paths = [
|
19
|
+
spec.require_paths = ['lib']
|
20
20
|
|
21
|
+
spec.add_dependency 'rack'
|
22
|
+
spec.add_dependency 'uuid'
|
23
|
+
spec.add_dependency 'em-http-request'
|
24
|
+
spec.add_dependency 'rack-ssl-enforcer'
|
25
|
+
spec.add_dependency 'rack-fiber_pool'
|
26
|
+
spec.add_dependency 'faraday'
|
27
|
+
spec.add_dependency 'em-synchrony'
|
28
|
+
spec.add_dependency 'thin'
|
21
29
|
|
22
|
-
spec.
|
23
|
-
spec.
|
24
|
-
spec.
|
25
|
-
spec.
|
26
|
-
spec.
|
27
|
-
spec.
|
28
|
-
spec.
|
29
|
-
spec.
|
30
|
-
|
31
|
-
spec.add_development_dependency "bundler", "~> 1.5"
|
32
|
-
spec.add_development_dependency "rake"
|
33
|
-
spec.add_development_dependency "rspec", "~> 3.2.0"
|
34
|
-
spec.add_development_dependency "webmock"
|
35
|
-
spec.add_development_dependency "rack-test"
|
36
|
-
spec.add_development_dependency "geminabox"
|
37
|
-
spec.add_development_dependency "simplecov"
|
38
|
-
#spec.add_development_dependency "rubocop"
|
39
|
-
spec.add_development_dependency "memory_profiler"
|
40
|
-
|
41
|
-
spec.add_development_dependency "ruby-prof"
|
30
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
31
|
+
spec.add_development_dependency 'rake'
|
32
|
+
spec.add_development_dependency 'rspec', '~> 3.2.0'
|
33
|
+
spec.add_development_dependency 'webmock'
|
34
|
+
spec.add_development_dependency 'rack-test'
|
35
|
+
spec.add_development_dependency 'geminabox'
|
36
|
+
spec.add_development_dependency 'simplecov'
|
37
|
+
spec.add_development_dependency 'rubocop'
|
38
|
+
spec.add_development_dependency 'memory_profiler'
|
42
39
|
|
40
|
+
spec.add_development_dependency 'ruby-prof'
|
43
41
|
end
|
@@ -1,10 +1,8 @@
|
|
1
|
+
# rubocop:disable Metrics/ClassLength
|
1
2
|
require 'sitehub/builder'
|
2
3
|
|
3
4
|
class SiteHub
|
4
|
-
|
5
|
-
|
6
5
|
describe Builder do
|
7
|
-
|
8
6
|
include_context :middleware_test
|
9
7
|
|
10
8
|
subject do
|
@@ -19,14 +17,12 @@ class SiteHub
|
|
19
17
|
|
20
18
|
describe '#proxy' do
|
21
19
|
context 'no version explicitly defined' do
|
22
|
-
|
23
20
|
subject do
|
24
21
|
described_class.new do
|
25
22
|
proxy '/app1' => :endpoint
|
26
23
|
end
|
27
24
|
end
|
28
25
|
|
29
|
-
|
30
26
|
it 'the defined route is used 100% of the time' do
|
31
27
|
expected_proxy = ForwardProxyBuilder.new(mapped_path: '/path').tap do |route|
|
32
28
|
route.default(url: :endpoint)
|
@@ -54,7 +50,7 @@ class SiteHub
|
|
54
50
|
end
|
55
51
|
end
|
56
52
|
|
57
|
-
describe
|
53
|
+
describe '#access_logger' do
|
58
54
|
it ' sets the logger' do
|
59
55
|
subject.access_logger :access_logger
|
60
56
|
sitehub = subject.build
|
@@ -69,13 +65,12 @@ class SiteHub
|
|
69
65
|
logger_middleware = find_middleware(sitehub, Logging::AccessLogger)
|
70
66
|
expect(logger_middleware.logger).to eq(Logging::LogWrapper.new(:stdout_logger))
|
71
67
|
end
|
72
|
-
|
73
68
|
end
|
74
69
|
|
75
70
|
describe '#reverse_proxy' do
|
76
71
|
it 'registers a reverse proxy' do
|
77
|
-
subject.reverse_proxy(:
|
78
|
-
expect(subject.reverse_proxies).to eq(
|
72
|
+
subject.reverse_proxy(downstream_url: :upstream_path)
|
73
|
+
expect(subject.reverse_proxies).to eq(downstream_url: :upstream_path)
|
79
74
|
end
|
80
75
|
end
|
81
76
|
|
@@ -91,7 +86,7 @@ class SiteHub
|
|
91
86
|
end
|
92
87
|
end
|
93
88
|
|
94
|
-
describe
|
89
|
+
describe '#error_logger' do
|
95
90
|
it 'sets the logger' do
|
96
91
|
subject.error_logger :error_logger
|
97
92
|
sitehub = subject.build
|
@@ -106,11 +101,9 @@ class SiteHub
|
|
106
101
|
logger_middleware = find_middleware(sitehub, SiteHub::Logging::ErrorLogger)
|
107
102
|
expect(logger_middleware.logger).to eq(Logging::LogWrapper.new(:stderr_logger))
|
108
103
|
end
|
109
|
-
|
110
104
|
end
|
111
105
|
|
112
106
|
describe '#build' do
|
113
|
-
|
114
107
|
it 'initializes the forward_proxies' do
|
115
108
|
expect(subject.forward_proxies).to receive(:init).and_call_original
|
116
109
|
subject.build
|
@@ -142,17 +135,24 @@ class SiteHub
|
|
142
135
|
end
|
143
136
|
|
144
137
|
it 'uses configured reverse proxy directives' do
|
145
|
-
subject.reverse_proxy(
|
138
|
+
subject.reverse_proxy(downstream_url: :upstream_path.to_s)
|
146
139
|
reverse_proxy = find_middleware(subject.build, ReverseProxy)
|
147
140
|
|
148
|
-
expect(reverse_proxy.path_directives).to eq(PathDirectives.new(:
|
141
|
+
expect(reverse_proxy.path_directives).to eq(PathDirectives.new(downstream_url: :upstream_path.to_s))
|
149
142
|
end
|
150
143
|
end
|
151
144
|
|
152
|
-
|
153
145
|
it 'adds them in the right order' do
|
154
|
-
middleware_stack = collect_middleware(subject.build).collect
|
155
|
-
|
146
|
+
middleware_stack = collect_middleware(subject.build).collect(&:class)
|
147
|
+
|
148
|
+
expected_middleware = [Rack::FiberPool,
|
149
|
+
Logging::ErrorLogger,
|
150
|
+
Logging::AccessLogger,
|
151
|
+
TransactionId,
|
152
|
+
ReverseProxy,
|
153
|
+
ForwardProxies]
|
154
|
+
|
155
|
+
expect(middleware_stack).to eq(expected_middleware)
|
156
156
|
end
|
157
157
|
end
|
158
158
|
|
@@ -195,9 +195,7 @@ class SiteHub
|
|
195
195
|
expect(sitehub).to_not be_using(Rack::SslEnforcer)
|
196
196
|
end
|
197
197
|
end
|
198
|
-
|
199
198
|
end
|
200
199
|
end
|
201
|
-
|
202
200
|
end
|
203
|
-
end
|
201
|
+
end
|
@@ -2,10 +2,8 @@ require 'sitehub/collection/route_collection'
|
|
2
2
|
require 'sitehub/forward_proxy'
|
3
3
|
|
4
4
|
class SiteHub
|
5
|
-
|
6
5
|
describe Collection::RouteCollection do
|
7
|
-
|
8
|
-
let(:route_without_rule) { ForwardProxy.new(url: :url, id: :id,sitehub_cookie_name: :cookie_name) }
|
6
|
+
let(:route_without_rule) { ForwardProxy.new(url: :url, id: :id, sitehub_cookie_name: :cookie_name) }
|
9
7
|
|
10
8
|
it 'is a collection' do
|
11
9
|
expect(subject).to be_a(Collection)
|
@@ -34,7 +32,7 @@ class SiteHub
|
|
34
32
|
describe '#valid?' do
|
35
33
|
context 'route added' do
|
36
34
|
it 'returns true' do
|
37
|
-
subject.add :id,route_without_rule
|
35
|
+
subject.add :id, route_without_rule
|
38
36
|
expect(subject).to be_valid
|
39
37
|
end
|
40
38
|
end
|
@@ -47,21 +45,21 @@ class SiteHub
|
|
47
45
|
end
|
48
46
|
|
49
47
|
describe '#resolve' do
|
50
|
-
|
51
48
|
context 'no rule on route' do
|
52
49
|
it 'returns the route' do
|
53
|
-
route_without_rule = ForwardProxy.new(url: :url, id: :id,sitehub_cookie_name: :cookie_name)
|
50
|
+
route_without_rule = ForwardProxy.new(url: :url, id: :id, sitehub_cookie_name: :cookie_name)
|
54
51
|
subject.add(:id, route_without_rule)
|
55
52
|
expect(subject.resolve({})).to be(route_without_rule)
|
56
53
|
end
|
57
54
|
end
|
58
55
|
context 'rule on route' do
|
59
|
-
|
60
56
|
it 'passes the environment to the rule' do
|
61
57
|
request_env = {}
|
62
|
-
rule = proc {|env| env[:env_passed_in] = true}
|
58
|
+
rule = proc { |env| env[:env_passed_in] = true }
|
63
59
|
|
64
|
-
proxy = ForwardProxy.new(url: :url,
|
60
|
+
proxy = ForwardProxy.new(url: :url,
|
61
|
+
id: :id,
|
62
|
+
sitehub_cookie_name: :cookie_name)
|
65
63
|
proxy.rule(rule)
|
66
64
|
subject.add(:id, proxy)
|
67
65
|
subject.resolve(env: request_env)
|
@@ -70,16 +68,21 @@ class SiteHub
|
|
70
68
|
|
71
69
|
context 'rule applies' do
|
72
70
|
it 'returns the route' do
|
73
|
-
route_with_rule = ForwardProxy.new(url: :url,
|
71
|
+
route_with_rule = ForwardProxy.new(url: :url,
|
72
|
+
id: :id,
|
73
|
+
rule: proc { true },
|
74
|
+
sitehub_cookie_name: :cookie_name)
|
74
75
|
subject.add(:id, route_with_rule)
|
75
76
|
expect(subject.resolve({})).to be(route_with_rule)
|
76
77
|
end
|
77
|
-
|
78
78
|
end
|
79
79
|
|
80
80
|
context 'rule does not apply' do
|
81
81
|
it 'returns nil' do
|
82
|
-
route_with_rule = ForwardProxy.new(url: :url,
|
82
|
+
route_with_rule = ForwardProxy.new(url: :url,
|
83
|
+
id: :id,
|
84
|
+
sitehub_cookie_name: :cookie_name,
|
85
|
+
rule: proc { false })
|
83
86
|
subject.add(:id, route_with_rule)
|
84
87
|
expect(subject.resolve({})).to eq(nil)
|
85
88
|
end
|
@@ -87,5 +90,4 @@ class SiteHub
|
|
87
90
|
end
|
88
91
|
end
|
89
92
|
end
|
90
|
-
|
91
|
-
end
|
93
|
+
end
|
@@ -2,11 +2,9 @@ require 'sitehub/collection/split_route_collection'
|
|
2
2
|
require 'sitehub/forward_proxy'
|
3
3
|
|
4
4
|
class SiteHub
|
5
|
-
|
6
5
|
describe Collection::SplitRouteCollection do
|
7
|
-
|
8
|
-
let(:
|
9
|
-
let(:route_2) { ForwardProxy.new(url: :url, id: :id2,sitehub_cookie_name: :cookie_name) }
|
6
|
+
let(:route_1) { ForwardProxy.new(url: :url, id: :id1, sitehub_cookie_name: :cookie_name) }
|
7
|
+
let(:route_2) { ForwardProxy.new(url: :url, id: :id2, sitehub_cookie_name: :cookie_name) }
|
10
8
|
|
11
9
|
it 'is a collection' do
|
12
10
|
expect(subject).to be_a(Collection)
|
@@ -34,13 +32,15 @@ class SiteHub
|
|
34
32
|
|
35
33
|
context 'entry is added which takes splits total over 100%' do
|
36
34
|
it 'raises an error' do
|
37
|
-
expect{subject.add route_2.id, route_2, 101}
|
35
|
+
expect { subject.add route_2.id, route_2, 101 }
|
36
|
+
.to raise_exception described_class::InvalidSplitException, described_class::SPLIT_ERR_MSG
|
38
37
|
end
|
39
38
|
end
|
40
39
|
|
41
40
|
context 'non fixnum passed in' do
|
42
41
|
it 'raises and error' do
|
43
|
-
expect{subject.add route_2.id, route_2, 1.1}
|
42
|
+
expect { subject.add route_2.id, route_2, 1.1 }
|
43
|
+
.to raise_exception described_class::InvalidSplitException, described_class::FIXNUM_ERR_MSG
|
44
44
|
end
|
45
45
|
end
|
46
46
|
end
|
@@ -79,8 +79,7 @@ class SiteHub
|
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
82
|
-
describe
|
83
|
-
|
82
|
+
describe '#valid?' do
|
84
83
|
context 'splits == to 100' do
|
85
84
|
it 'returns true' do
|
86
85
|
subject.add route_1.id, route_1, 50
|
@@ -107,5 +106,4 @@ class SiteHub
|
|
107
106
|
end
|
108
107
|
end
|
109
108
|
end
|
110
|
-
|
111
|
-
end
|
109
|
+
end
|
@@ -4,13 +4,13 @@ class SiteHub
|
|
4
4
|
describe Collection do
|
5
5
|
describe '#valid?' do
|
6
6
|
it 'must be overiden and raises an exception by default' do
|
7
|
-
expect { subject.valid? }.to raise_exception
|
7
|
+
expect { subject.valid? }.to raise_exception 'implement me'
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
11
|
describe '#resolve' do
|
12
12
|
it 'must be overiden and raises an exception by default' do
|
13
|
-
expect { subject.resolve }.to raise_exception
|
13
|
+
expect { subject.resolve }.to raise_exception 'implement me'
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -19,7 +19,7 @@ class SiteHub
|
|
19
19
|
context 'duplicate ids added' do
|
20
20
|
subject do
|
21
21
|
inheritor = Class.new(described_class) do
|
22
|
-
def add
|
22
|
+
def add(id, value, *_args)
|
23
23
|
self[id] = value
|
24
24
|
end
|
25
25
|
end
|
@@ -29,12 +29,12 @@ class SiteHub
|
|
29
29
|
it 'raises an error' do
|
30
30
|
duplicate = Struct.new(:id).new(1)
|
31
31
|
subject.add(duplicate.id, duplicate)
|
32
|
-
|
32
|
+
expected_message = described_class::ClassMethods::UNIQUE_LABELS_MSG
|
33
|
+
expect { subject.add(duplicate.id, duplicate) }
|
34
|
+
.to raise_exception described_class::DuplicateVersionException, expected_message
|
33
35
|
end
|
34
36
|
end
|
35
37
|
end
|
36
|
-
|
37
38
|
end
|
38
|
-
|
39
39
|
end
|
40
|
-
end
|
40
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
class SiteHub
|
2
2
|
class Cookie
|
3
3
|
describe Attribute do
|
4
|
-
let(:attribute_name){'domain'}
|
5
|
-
let(:attribute_value){'value'}
|
4
|
+
let(:attribute_name) { 'domain' }
|
5
|
+
let(:attribute_value) { 'value' }
|
6
6
|
subject do
|
7
7
|
described_class.new(attribute_name, attribute_value)
|
8
8
|
end
|
@@ -34,4 +34,4 @@ class SiteHub
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
37
|
-
end
|
37
|
+
end
|
@@ -2,7 +2,7 @@ class SiteHub
|
|
2
2
|
class Cookie
|
3
3
|
describe Flag do
|
4
4
|
let(:name) { 'flag' }
|
5
|
-
let(:string_sanitiser){Object.new.tap{|o|o.extend(StringSanitiser)}}
|
5
|
+
let(:string_sanitiser) { Object.new.tap { |o| o.extend(StringSanitiser) } }
|
6
6
|
subject do
|
7
7
|
described_class.new(name)
|
8
8
|
end
|
@@ -24,4 +24,4 @@ class SiteHub
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
27
|
-
end
|
27
|
+
end
|
@@ -2,12 +2,18 @@ require 'sitehub/cookie_rewriting'
|
|
2
2
|
|
3
3
|
class SiteHub
|
4
4
|
describe CookieRewriting do
|
5
|
-
let(:downstream_domain){'.downstream_domain.com'}
|
6
|
-
|
7
|
-
let(:
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
let(:downstream_domain) { '.downstream_domain.com' }
|
6
|
+
|
7
|
+
let(:request_mapping) do
|
8
|
+
RequestMapping.new(source_url: 'http://example.org',
|
9
|
+
mapped_url: "http://#{downstream_domain}",
|
10
|
+
mapped_path: '/map')
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:substitute_domain) { URI(request_mapping.source_url).host }
|
14
|
+
let(:substitute_path) { '/path' }
|
15
|
+
let(:downstream_response) { Rack::Response.new }
|
16
|
+
let(:downstream_domain_cookie_name) { 'downstream.cookie' }
|
11
17
|
|
12
18
|
subject do
|
13
19
|
Object.new.tap do |o|
|
@@ -28,8 +34,8 @@ class SiteHub
|
|
28
34
|
cookie1 = SiteHub::Cookie.new(cookie_strings[0])
|
29
35
|
cookie2 = SiteHub::Cookie.new(cookie_strings[1])
|
30
36
|
expected = {
|
31
|
-
|
32
|
-
|
37
|
+
cookie1.name => cookie1,
|
38
|
+
cookie2.name => cookie2
|
33
39
|
}
|
34
40
|
result = subject.cookies_string_as_hash(cookie_header)
|
35
41
|
expect(result).to eq(expected)
|
@@ -44,7 +50,6 @@ class SiteHub
|
|
44
50
|
end
|
45
51
|
|
46
52
|
describe '#rewrite_cookies' do
|
47
|
-
|
48
53
|
context 'subdomain character present' do
|
49
54
|
it 'substitues the domain for the mapped domain' do
|
50
55
|
downstream_response.set_cookie(downstream_domain_cookie_name, domain: downstream_domain, value: 'value')
|
@@ -61,7 +66,5 @@ class SiteHub
|
|
61
66
|
end
|
62
67
|
end
|
63
68
|
end
|
64
|
-
|
65
69
|
end
|
66
|
-
|
67
|
-
end
|
70
|
+
end
|
data/spec/sitehub/cookie_spec.rb
CHANGED
@@ -2,12 +2,12 @@ require 'sitehub/cookie'
|
|
2
2
|
|
3
3
|
class SiteHub
|
4
4
|
describe Cookie do
|
5
|
-
let(:attribute_class){described_class::Attribute}
|
6
|
-
let(:flag_class){described_class::Flag}
|
7
|
-
let(:domain_attribute){attribute_class.new(:domain.to_s, 'example.com')}
|
8
|
-
let(:name){'sitehub.recorded_route'}
|
9
|
-
let(:value){'new'}
|
10
|
-
let(:cookie_string){ "#{name}=#{value}; HttpOnly; #{domain_attribute}"}
|
5
|
+
let(:attribute_class) { described_class::Attribute }
|
6
|
+
let(:flag_class) { described_class::Flag }
|
7
|
+
let(:domain_attribute) { attribute_class.new(:domain.to_s, 'example.com') }
|
8
|
+
let(:name) { 'sitehub.recorded_route' }
|
9
|
+
let(:value) { 'new' }
|
10
|
+
let(:cookie_string) { "#{name}=#{value}; HttpOnly; #{domain_attribute}" }
|
11
11
|
|
12
12
|
subject do
|
13
13
|
described_class.new cookie_string
|
@@ -27,9 +27,7 @@ class SiteHub
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
|
31
30
|
describe '#find' do
|
32
|
-
|
33
31
|
context 'entry found' do
|
34
32
|
it 'returns the entry with the given name' do
|
35
33
|
expect(subject.find(:domain)).to eq(domain_attribute)
|
@@ -41,14 +39,6 @@ class SiteHub
|
|
41
39
|
expect(subject.find(:missing)).to eq(nil)
|
42
40
|
end
|
43
41
|
end
|
44
|
-
|
45
|
-
end
|
46
|
-
|
47
|
-
describe '#delete' do
|
48
|
-
it 'removes the entry' do
|
49
|
-
subject.delete(:domain)
|
50
|
-
expect(subject.find(:domain)).to eq(nil)
|
51
|
-
end
|
52
42
|
end
|
53
43
|
|
54
44
|
describe '#to_s' do
|
@@ -56,6 +46,5 @@ class SiteHub
|
|
56
46
|
expect(subject.to_s).to eq(cookie_string)
|
57
47
|
end
|
58
48
|
end
|
59
|
-
|
60
49
|
end
|
61
|
-
end
|
50
|
+
end
|