goliath-reverse-proxy 0.1.2 → 0.2.0

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.
data/Gemfile.lock CHANGED
@@ -1,63 +1,61 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- goliath-reverse-proxy (0.1.0)
5
- em-http-request
4
+ goliath-reverse-proxy (0.2.0)
5
+ em-http-request (~> 1.0.3)
6
6
  em-synchrony
7
7
  goliath
8
8
 
9
9
  GEM
10
10
  remote: http://rubygems.org/
11
11
  specs:
12
- addressable (2.2.8)
12
+ addressable (2.3.2)
13
13
  async-rack (0.5.1)
14
14
  rack (~> 1.1)
15
15
  cookiejar (0.3.0)
16
16
  diff-lcs (1.1.3)
17
- em-http-request (1.0.2)
17
+ em-http-request (1.0.3)
18
18
  addressable (>= 2.2.3)
19
19
  cookiejar
20
20
  em-socksify
21
21
  eventmachine (>= 1.0.0.beta.4)
22
22
  http_parser.rb (>= 0.5.3)
23
- em-socksify (0.2.0)
23
+ em-socksify (0.2.1)
24
24
  eventmachine (>= 1.0.0.beta.4)
25
- em-synchrony (1.0.1)
25
+ em-synchrony (1.0.2)
26
26
  eventmachine (>= 1.0.0.beta.1)
27
- eventmachine (1.0.0.beta.4)
28
- goliath (0.9.4)
27
+ em-websocket (0.3.8)
28
+ addressable (>= 2.1.1)
29
+ eventmachine (>= 0.12.9)
30
+ eventmachine (1.0.0.rc.4)
31
+ goliath (1.0.0)
29
32
  async-rack
30
33
  em-synchrony (>= 1.0.0)
31
- eventmachine (>= 1.0.0.beta.3)
32
- http_parser.rb
33
- http_router (~> 0.9.0)
34
+ em-websocket
35
+ eventmachine (>= 1.0.0.beta.4)
36
+ http_parser.rb (= 0.5.3)
34
37
  log4r
35
38
  multi_json
36
39
  rack (>= 1.2.2)
37
40
  rack-contrib
38
41
  rack-respond_to
39
42
  http_parser.rb (0.5.3)
40
- http_router (0.9.7)
41
- rack (>= 1.0.0)
42
- url_mount (~> 0.2.1)
43
43
  log4r (1.1.10)
44
- multi_json (1.3.5)
44
+ multi_json (1.3.6)
45
45
  rack (1.4.1)
46
46
  rack-accept-media-types (0.9)
47
47
  rack-contrib (1.1.0)
48
48
  rack (>= 0.9.1)
49
49
  rack-respond_to (0.9.8)
50
50
  rack-accept-media-types (>= 0.6)
51
- rspec (2.10.0)
52
- rspec-core (~> 2.10.0)
53
- rspec-expectations (~> 2.10.0)
54
- rspec-mocks (~> 2.10.0)
55
- rspec-core (2.10.1)
56
- rspec-expectations (2.10.0)
51
+ rspec (2.11.0)
52
+ rspec-core (~> 2.11.0)
53
+ rspec-expectations (~> 2.11.0)
54
+ rspec-mocks (~> 2.11.0)
55
+ rspec-core (2.11.1)
56
+ rspec-expectations (2.11.2)
57
57
  diff-lcs (~> 1.1.3)
58
- rspec-mocks (2.10.1)
59
- url_mount (0.2.1)
60
- rack
58
+ rspec-mocks (2.11.2)
61
59
 
62
60
  PLATFORMS
63
61
  ruby
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = 'goliath-reverse-proxy'
5
- gem.version = '0.1.2'
5
+ gem.version = '0.2.0'
6
6
  gem.authors = ["Daniel Farrell"]
7
7
  gem.email = 'danielfarrell76@gmail.com'
8
8
  gem.homepage = 'https://github.com/danielfarrell/goliath-reverse-proxy'
@@ -12,7 +12,7 @@ Gem::Specification.new do |gem|
12
12
 
13
13
  gem.add_dependency "goliath"
14
14
  gem.add_dependency "em-synchrony"
15
- gem.add_dependency "em-http-request"
15
+ gem.add_dependency "em-http-request", "~> 1.0.3"
16
16
 
17
17
  gem.add_development_dependency "rspec", "~> 2.10"
18
18
 
@@ -7,26 +7,32 @@ module Goliath
7
7
 
8
8
  def initialize(app, options)
9
9
  @app = app
10
- @options = options
10
+ @connection = EM::HttpRequest.new(options[:base_url])
11
11
  end
12
12
 
13
- def post_process(env, status, headers, body)
13
+ def call(env)
14
+ connection = @connection.dup
15
+ super(env, connection)
16
+ end
17
+
18
+ def post_process(env, status, headers, body, connection)
14
19
  method = env['REQUEST_METHOD'].downcase.to_sym
15
20
 
16
- url = "#{@options[:base_url]}#{env['REQUEST_URI']}"
21
+ options = {:head => request_headers(env, headers), :path => env['REQUEST_URI']}
22
+ options[:body] = env['params'] if [:put, :post, :patch].include? method
17
23
 
24
+ http = connection.send(method, options)
25
+
26
+ [http.response_header.status, http.response_header.raw, [http.response]]
27
+ end
28
+
29
+ def request_headers(env, headers)
18
30
  env.each do |key, value|
19
31
  headers[$1] = value if key =~ /HTTP_(.*)/
20
32
  end
21
33
  headers['X-Forwarded-Host'] = env['HTTP_HOST']
22
34
  headers['X-Forwarded-User'] = env['REMOTE_USER'] if env['REMOTE_USER']
23
-
24
- params = {:head => headers}
25
- params[:body] = env['params'] if [:put, :post, :patch].include? method
26
-
27
- http = EM::HttpRequest.new(url).send(method, params)
28
-
29
- [http.response_header.status, http.response_header, [http.response]]
35
+ headers
30
36
  end
31
37
 
32
38
  end
@@ -2,6 +2,11 @@ require 'spec_helper'
2
2
  require 'goliath/rack/reverse_proxy'
3
3
 
4
4
  describe Goliath::Rack::ReverseProxy do
5
+
6
+ def failed
7
+ EM.stop
8
+ end
9
+
5
10
  it 'should load without errors' do
6
11
  lambda { Goliath::Rack::ReverseProxy.new('app', {:base_url => 'http://localhost'}) }.should_not raise_error
7
12
  end
@@ -11,58 +16,66 @@ describe Goliath::Rack::ReverseProxy do
11
16
  @app = double('app').as_null_object
12
17
  @app.should_receive(:call).and_return(initial_response)
13
18
  @env = Goliath::Env.new
19
+ @stub_connection = EventMachine::HttpConnection.new
20
+ @stub_connection.stub(:dup => @stub_connection)
21
+ EM::HttpRequest.should_receive(:new).with('http://localhost').and_return(@stub_connection)
14
22
  @proxy = Goliath::Rack::ReverseProxy.new(@app, {:base_url => 'http://localhost'})
15
23
  end
16
24
 
17
25
  it 'should create url to proxy to' do
18
- @env['REQUEST_METHOD'] = 'GET'
19
- @env['REQUEST_URI'] = '/a/test/file.html'
20
- action = double('http action').as_null_object
21
- action.should_receive(:get).and_return(response_object)
22
- EM::HttpRequest.should_receive(:new).with('http://localhost/a/test/file.html').and_return(action)
23
- @proxy.call(@env)
26
+ EM.synchrony do
27
+ @env['REQUEST_METHOD'] = 'GET'
28
+ @env['REQUEST_URI'] = '/a/test/file.html'
29
+ @stub_connection.should_receive(:get).and_return(response_object)
30
+ @proxy.call(@env)
31
+ EM.stop
32
+ end
24
33
  end
25
34
 
26
35
  it 'should build the needed headers' do
27
- @env['REQUEST_METHOD'] = 'GET'
28
- @env['REQUEST_URI'] = '/'
29
- @env['SERVER_NAME'] = 'test.com'
30
- @env['HTTP_HOST'] = 'testing.com'
31
- @env['REMOTE_USER'] = 'bob'
32
- action = double('http action').as_null_object
33
- headers = {"HOST"=>"testing.com", "CONTENT_TYPE"=>nil, "HTTP_HOST"=>"test.com", "X-Forwarded-Host"=>"testing.com", "REMOTE_USER"=>"bob"}
34
- action.should_receive(:get).with(:head => headers).and_return(response_object)
35
- EM::HttpRequest.should_receive(:new).with('http://localhost/').and_return(action)
36
- @proxy.call(@env)
36
+ EM.synchrony do
37
+ @env['REQUEST_METHOD'] = 'GET'
38
+ @env['REQUEST_URI'] = '/'
39
+ @env['SERVER_NAME'] = 'test.com'
40
+ @env['HTTP_HOST'] = 'testing.com'
41
+ @env['REMOTE_USER'] = 'bob'
42
+ headers = {"HOST"=>"testing.com", "X-Forwarded-Host"=>"testing.com", "X-Forwarded-User"=>"bob"}
43
+ @stub_connection.should_receive(:get).with(:head => headers, :path => @env['REQUEST_URI']).and_return(response_object)
44
+ @proxy.call(@env)
45
+ EM.stop
46
+ end
37
47
  end
38
48
 
39
49
  it 'should do a delete if a get was sent' do
40
- @env['REQUEST_METHOD'] = 'DELETE'
41
- @env['REQUEST_URI'] = '/some/item/23'
42
- action = double('http action').as_null_object
43
- action.should_receive(:delete).and_return(response_object)
44
- EM::HttpRequest.should_receive(:new).with('http://localhost/some/item/23').and_return(action)
45
- @proxy.call(@env)
50
+ EM.synchrony do
51
+ @env['REQUEST_METHOD'] = 'DELETE'
52
+ @env['REQUEST_URI'] = '/some/item/23'
53
+ @stub_connection.should_receive(:delete).and_return(response_object)
54
+ @proxy.call(@env)
55
+ EM.stop
56
+ end
46
57
  end
47
58
 
48
59
  it 'should do a post with body if a post was sent' do
49
- @env['REQUEST_METHOD'] = 'POST'
50
- @env['REQUEST_URI'] = '/some/item'
51
- action = double('http action').as_null_object
52
- action.should_receive(:post).and_return(response_object)
53
- EM::HttpRequest.should_receive(:new).with('http://localhost/some/item').and_return(action)
54
- @proxy.call(@env)
60
+ EM.synchrony do
61
+ @env['REQUEST_METHOD'] = 'POST'
62
+ @env['REQUEST_URI'] = '/some/item'
63
+ @stub_connection.should_receive(:post).and_return(response_object)
64
+ @proxy.call(@env)
65
+ EM.stop
66
+ end
55
67
  end
56
68
 
57
69
  it 'should return the response from the proxied request' do
58
- @env['REQUEST_METHOD'] = 'GET'
59
- @env['REQUEST_URI'] = '/a/test/file.html'
60
- results = response_object(401, {}, ['testing'])
61
- action = double('http action').as_null_object
62
- action.should_receive(:get).and_return(results)
63
- EM::HttpRequest.should_receive(:new).with('http://localhost/a/test/file.html').and_return(action)
64
- response = @proxy.call(@env)
65
- response.should == [401, {}, [["testing"]]]
70
+ EM.synchrony do
71
+ @env['REQUEST_METHOD'] = 'GET'
72
+ @env['REQUEST_URI'] = '/a/test/file.html'
73
+ results = response_object(401, {}, ['testing'])
74
+ @stub_connection.should_receive(:get).and_return(results)
75
+ response = @proxy.call(@env)
76
+ response.should == [401, {}, [["testing"]]]
77
+ EM.stop
78
+ end
66
79
  end
67
80
 
68
81
  end
data/spec/spec_helper.rb CHANGED
@@ -13,6 +13,10 @@ class ResponseHeader < Hash
13
13
  def status=(status)
14
14
  @status = status
15
15
  end
16
+
17
+ def raw
18
+ self
19
+ end
16
20
  end
17
21
 
18
22
  class Response
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: goliath-reverse-proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-29 00:00:00.000000000 Z
12
+ date: 2012-08-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: goliath
@@ -48,17 +48,17 @@ dependencies:
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
- - - ! '>='
51
+ - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: '0'
53
+ version: 1.0.3
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements:
59
- - - ! '>='
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 1.0.3
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: rspec
64
64
  requirement: !ruby/object:Gem::Requirement