sitehub 0.4.8 → 0.4.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0d9f33c7bd5d70f6b2b82af2a323ad52001ab72a
4
- data.tar.gz: 1b7e24903e21456c9ce94f2c6ae4b9e15574e257
3
+ metadata.gz: dc1b1f945c39176d1b996c7e1d874132686b2d11
4
+ data.tar.gz: 65e4926ceab0628a35a257a31c8a6f320501623c
5
5
  SHA512:
6
- metadata.gz: ac6d54ab0e88de48ecd18ccd84adbe946eff83bfdcb5a4e4b0ed5ebe64ace38c53f3ce022a6baab46f63dd6b145d40c6388b64ef917b3e50b6cfee32d86d369b
7
- data.tar.gz: 4fd1f12ee1c746b9539bbfbd704d8b83b31805ce1483d0e1508595278a28264fe23de9dabba020f81f9932d1236d6a20b2c6d1876be24ee038309a5e65678e3b
6
+ metadata.gz: 87eb4057e4afb5766c060f8b976700d1764f6230412da1fcb0ef022deeefa816e4af4bf434c73ee82c232ae7fa0ef7f93975d965cc7dc7e727ec0c537353114e
7
+ data.tar.gz: e548bc84ed6d7f341f0909978bcc7d1e694115c421d24c8bff74fa5cdd254d83caab55c1d295eb389b4befaf02423e97f4b152bd05d7172b39fc4410a6be27fd
data/.reek CHANGED
@@ -23,6 +23,7 @@ FeatureEnvy:
23
23
  exclude:
24
24
  - "SiteHub::DownstreamClient#call"
25
25
  - "SiteHub::LocationRewriters"
26
+ - "SiteHub::Builder#proxy"
26
27
  - 'Rack::Response#cookies'
27
28
  - 'format_http_to_rack_headers'
28
29
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sitehub (0.4.8)
4
+ sitehub (0.4.9)
5
5
  em-http-request
6
6
  em-synchrony
7
7
  faraday
@@ -22,11 +22,14 @@ class SiteHub
22
22
  end
23
23
 
24
24
  def initialize(&block)
25
- @forward_proxies = ForwardProxies.new
26
25
  @reverse_proxies = {}
27
26
  instance_eval(&block) if block
28
27
  end
29
28
 
29
+ def forward_proxies
30
+ @forward_proxies ||= ForwardProxies.new(sitehub_cookie_name)
31
+ end
32
+
30
33
  def build
31
34
  forward_proxies.init
32
35
  add_default_middleware
@@ -45,17 +48,11 @@ class SiteHub
45
48
  end
46
49
 
47
50
  def proxy(opts = {}, &block)
48
- if opts.is_a?(Hash)
49
- mapped_path, url = *opts.to_a.flatten
50
- else
51
- mapped_path = opts
52
- url = nil
53
- end
51
+ mapped_path, url = *(opts.respond_to?(:to_a) ? opts.to_a : [opts]).flatten
54
52
 
55
- forward_proxies << ForwardProxyBuilder.new(sitehub_cookie_name: sitehub_cookie_name,
56
- url: url,
57
- mapped_path: mapped_path,
58
- &block)
53
+ forward_proxies.add_proxy(url: url,
54
+ mapped_path: mapped_path,
55
+ &block)
59
56
  end
60
57
 
61
58
  def reverse_proxy(hash)
@@ -1,9 +1,11 @@
1
1
  # rubocop:disable Metrics/ParameterLists
2
2
  class SiteHub
3
3
  class ForwardProxy
4
- include Rules, Resolver
4
+ include Rules, Resolver, Equality
5
5
 
6
- attr_reader :downstream_client, :sitehub_cookie_name, :sitehub_cookie_path, :id, :mapped_path, :mapped_url
6
+ attr_reader :downstream_client, :sitehub_cookie_name, :id, :sitehub_cookie_path, :mapped_path, :mapped_url
7
+
8
+ transient :downstream_client
7
9
 
8
10
  def initialize(sitehub_cookie_path: nil, sitehub_cookie_name:, id:, rule: nil, mapped_path:, mapped_url:)
9
11
  @downstream_client = DownstreamClient.new
@@ -19,25 +21,15 @@ class SiteHub
19
21
  request = env[REQUEST]
20
22
  request.map(mapped_path, mapped_url)
21
23
 
22
- response = downstream_client.call(request)
23
- value = request.cookies[sitehub_cookie_name.to_s] || id
24
- response.set_cookie(sitehub_cookie_name, path: resolve_sitehub_cookie_path(request), value: value)
25
- response
24
+ downstream_client.call(request).tap do |response|
25
+ response.set_cookie(sitehub_cookie_name,
26
+ path: resolve_sitehub_cookie_path(request),
27
+ value: id)
28
+ end
26
29
  end
27
30
 
28
31
  def resolve_sitehub_cookie_path(request)
29
32
  sitehub_cookie_path || request.path
30
33
  end
31
-
32
- def ==(other)
33
- expected = [other.mapped_path,
34
- other.mapped_url,
35
- other.rule,
36
- other.id,
37
- other.sitehub_cookie_name,
38
- other.sitehub_cookie_path]
39
-
40
- expected == [mapped_path, mapped_url, rule, id, sitehub_cookie_name, sitehub_cookie_path]
41
- end
42
34
  end
43
35
  end
@@ -4,12 +4,20 @@ require 'rack/request'
4
4
  require 'rack/response'
5
5
  require 'rack/utils'
6
6
  require 'em-http'
7
+ require 'forwardable'
7
8
 
8
9
  class SiteHub
9
10
  module Middleware
10
- class ForwardProxies
11
+ class ForwardProxies < Hash
11
12
  NIL_PROXY = NilProxy.new
12
13
 
14
+ attr_reader :sitehub_cookie_name
15
+
16
+ def initialize(sitehub_cookie_name)
17
+ @sitehub_cookie_name = sitehub_cookie_name
18
+ self.default = NIL_PROXY
19
+ end
20
+
13
21
  def call(env)
14
22
  source_request = Rack::Request.new(env)
15
23
 
@@ -19,20 +27,23 @@ class SiteHub
19
27
  end
20
28
 
21
29
  def init
22
- forward_proxies.values.each(&:build)
30
+ values.each(&:build)
23
31
  self
24
32
  end
25
33
 
26
- def <<(route)
27
- forward_proxies[route.mapped_path] = route
34
+ def add_proxy(url: nil, mapped_path:, &block)
35
+ self[mapped_path] = ForwardProxyBuilder.new(sitehub_cookie_name: sitehub_cookie_name,
36
+ url: url,
37
+ mapped_path: mapped_path,
38
+ &block)
28
39
  end
29
40
 
30
41
  def mapped_proxy(path:, request:)
31
- forward_proxies[mapping(path)].resolve(id: request.cookies[RECORDED_ROUTES_COOKIE], env: request.env)
42
+ self[mapping(path)].resolve(id: request.cookies[sitehub_cookie_name], env: request.env)
32
43
  end
33
44
 
34
45
  def mapping(path)
35
- forward_proxies.keys.find do |key|
46
+ keys.find do |key|
36
47
  case key
37
48
  when Regexp
38
49
  key.match(path)
@@ -41,14 +52,6 @@ class SiteHub
41
52
  end
42
53
  end
43
54
  end
44
-
45
- def forward_proxies
46
- @forward_proxies ||= begin
47
- {}.tap do |hash|
48
- hash.default = NIL_PROXY
49
- end
50
- end
51
- end
52
55
  end
53
56
  end
54
57
  end
@@ -1,3 +1,3 @@
1
1
  class SiteHub
2
- VERSION = '0.4.8'.freeze
2
+ VERSION = '0.4.9'.freeze
3
3
  end
@@ -28,7 +28,7 @@ class SiteHub
28
28
  sitehub_cookie_name: RECORDED_ROUTES_COOKIE).tap do |route|
29
29
  route.default(url: :endpoint)
30
30
  end
31
- expect(subject.forward_proxies.forward_proxies['/app1']).to eq(expected_proxy)
31
+ expect(subject.forward_proxies['/app1']).to eq(expected_proxy)
32
32
  end
33
33
  end
34
34
 
@@ -47,7 +47,7 @@ class SiteHub
47
47
  route.split url: :endpoint, percentage: 100, label: :label
48
48
  end
49
49
 
50
- expect(subject.forward_proxies.forward_proxies['/app'].endpoints).to eq(expected_route.endpoints)
50
+ expect(subject.forward_proxies['/app'].endpoints).to eq(expected_route.endpoints)
51
51
  end
52
52
  end
53
53
  end
@@ -81,10 +81,18 @@ class SiteHub
81
81
  expect(subject.sitehub_cookie_name).to eq(RECORDED_ROUTES_COOKIE)
82
82
  end
83
83
 
84
- it 'is passed to forward_proxy_builders' do
85
- subject.sitehub_cookie_name :expected_cookie_name
86
- proxy = subject.proxy '/app1' => :endpoint
87
- expect(proxy.sitehub_cookie_name).to eq(:expected_cookie_name)
84
+ context 'forward_proxies' do
85
+ subject do
86
+ described_class.new do
87
+ sitehub_cookie_name :expected_cookie_name
88
+ proxy '/app1' => :endpoint
89
+ end
90
+ end
91
+
92
+ it 'forwards it' do
93
+ proxy = subject.proxy '/app1' => :endpoint
94
+ expect(proxy.sitehub_cookie_name).to eq(:expected_cookie_name)
95
+ end
88
96
  end
89
97
  end
90
98
 
@@ -116,8 +124,22 @@ class SiteHub
116
124
  expect(subject.build).to be_using(Middleware::TransactionId)
117
125
  end
118
126
 
119
- it 'adds a forward proxies' do
120
- expect(subject.build).to be_using(Middleware::ForwardProxies)
127
+ context 'forward proxies' do
128
+ subject do
129
+ described_class.new do
130
+ sitehub_cookie_name :custom_cookie_name
131
+ proxy '/app1' => :endpoint
132
+ end
133
+ end
134
+
135
+ it 'adds a forward proxies' do
136
+ expect(subject.build).to be_using(Middleware::ForwardProxies)
137
+ end
138
+
139
+ it 'configures it with the sitehub_cookie_name' do
140
+ forward_proxies = find_middleware(subject.build, Middleware::ForwardProxies)
141
+ expect(forward_proxies.sitehub_cookie_name).to eq(:custom_cookie_name)
142
+ end
121
143
  end
122
144
 
123
145
  it 'adds a AccessLogger' do
@@ -51,8 +51,8 @@ class SiteHub
51
51
  context 'cookie already set' do
52
52
  let(:rack_headers) { { 'HTTP_COOKIE' => 'cookie_name=existing_value' } }
53
53
 
54
- it 'leaves it alone' do
55
- expect(last_response.cookies[:cookie_name.to_s]).to eq(value: :existing_value.to_s, path: mapped_path)
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
56
  end
57
57
  end
58
58
 
@@ -5,15 +5,17 @@ class SiteHub
5
5
  describe ForwardProxies do
6
6
  let(:base_url) { 'http://google.com' }
7
7
  let(:application_root) { '/application_url' }
8
+
8
9
  let(:forward_proxy_builder) do
9
- ForwardProxyBuilder.new(mapped_path: application_root).tap do |builder|
10
- builder.split url: base_url, label: :current, percentage: 100
11
- end
10
+ subject.values.first
12
11
  end
13
12
 
14
13
  subject do
15
- described_class.new.tap do |route_set|
16
- route_set << forward_proxy_builder
14
+ 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|
17
+ builder.split url: base_url, label: :current, percentage: 100
18
+ end
17
19
  end
18
20
  end
19
21
 
@@ -23,24 +25,28 @@ class SiteHub
23
25
 
24
26
  describe '#init' do
25
27
  it 'builds all of the forward_proxies' do
26
- expect(subject.forward_proxies[application_root]).to receive(:build).and_call_original
28
+ expect(subject[application_root]).to receive(:build).and_call_original
27
29
  subject.init
28
30
  end
29
31
  end
30
32
 
31
33
  describe '#mapped_route' do
32
34
  let(:request) { Rack::Request.new({}) }
33
- let(:more_specific_proxy_builder) do
34
- ForwardProxyBuilder.new(url: "#{base_url}/sub_url", mapped_path: "#{application_root}/sub_url")
35
+
36
+ it 'uses the id in the sitehub_cookie to resolve the correct route' do
37
+ request.cookies[:cookie_name] = :preset_id
38
+ 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)
35
40
  end
36
41
 
37
42
  context 'regex match on path' do
38
43
  let(:fuzzy_matcher) do
39
- ForwardProxyBuilder.new(url: "#{base_url}/$1/view", mapped_path: %r{#{application_root}/(.*)/view})
44
+ subject.values.first
40
45
  end
46
+
41
47
  subject do
42
- described_class.new.tap do |route_set|
43
- route_set << fuzzy_matcher
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}
44
50
  end
45
51
  end
46
52
 
@@ -60,10 +66,14 @@ class SiteHub
60
66
  end
61
67
 
62
68
  context 'when more specific route is configured first' do
69
+ let(:more_specific_proxy_builder) do
70
+ subject.values.first
71
+ end
72
+
63
73
  subject do
64
- described_class.new.tap do |route_set|
65
- route_set << more_specific_proxy_builder
66
- route_set << forward_proxy_builder
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)
67
77
  end
68
78
  end
69
79
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sitehub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.8
4
+ version: 0.4.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ladtech
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-22 00:00:00.000000000 Z
11
+ date: 2016-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack