goliath 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of goliath might be problematic. Click here for more details.

@@ -18,7 +18,7 @@ class Echo < Goliath::API
18
18
  # If you are using Golaith version <=0.9.1 you need to Goliath::Rack::ValidationError
19
19
  # to prevent the request from remaining open after an error occurs
20
20
  #use Goliath::Rack::ValidationError
21
- use Goliath::Rack::Validation::RequestMethod, %w(GET POST) # allow GET and POST requests only
21
+ use Goliath::Rack::Validation::RequestMethod, %w(GET POST PATCH) # allow GET, POST and PATCH requests only
22
22
  use Goliath::Rack::Validation::RequiredParam, {:key => 'echo'} # must provide ?echo= query or body param
23
23
 
24
24
  plugin Goliath::Plugin::Latency # output reactor latency every second
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.add_dependency 'eventmachine', '>= 1.0.0.beta.4'
18
18
  s.add_dependency 'em-synchrony', '>= 1.0.0'
19
19
  s.add_dependency 'em-websocket', "0.3.8"
20
- s.add_dependency 'http_parser.rb', '0.5.3'
20
+ s.add_dependency 'http_parser.rb', '0.6.0.beta.2'
21
21
  s.add_dependency 'log4r'
22
22
 
23
23
  s.add_dependency 'rack', '>=1.2.2'
@@ -4,6 +4,7 @@ require 'goliath/response'
4
4
  require 'goliath/validation'
5
5
  require 'async_rack'
6
6
  require 'stringio'
7
+ require 'uri'
7
8
 
8
9
  module Goliath
9
10
  # Goliath::Request is responsible for processing a request and returning
@@ -86,14 +87,16 @@ module Goliath
86
87
  @env[SERVER_PORT] = port if port
87
88
  end
88
89
 
90
+ uri = URI(parser.request_url)
91
+
89
92
  @env[REQUEST_METHOD] = parser.http_method
90
93
  @env[REQUEST_URI] = parser.request_url
91
- @env[QUERY_STRING] = parser.query_string
94
+ @env[QUERY_STRING] = uri.query
92
95
  @env[HTTP_VERSION] = parser.http_version.join('.')
93
- @env[SCRIPT_NAME] = parser.request_path
94
- @env[REQUEST_PATH] = parser.request_path
95
- @env[PATH_INFO] = parser.request_path
96
- @env[FRAGMENT] = parser.fragment
96
+ @env[SCRIPT_NAME] = uri.path
97
+ @env[REQUEST_PATH] = uri.path
98
+ @env[PATH_INFO] = uri.path
99
+ @env[FRAGMENT] = uri.fragment
97
100
 
98
101
  yield if block_given?
99
102
 
@@ -86,6 +86,9 @@ module Goliath
86
86
  # @return [Array] The list of plugins to be executed by the server
87
87
  attr_accessor :plugins
88
88
 
89
+ # Allow to inject a custom logger
90
+ attr_accessor :logger
91
+
89
92
  # Any additional server options
90
93
  # @return [Hash] Any options to be passed to the server
91
94
  attr_accessor :app_options
@@ -238,6 +241,10 @@ module Goliath
238
241
  # Sets up the logging for the runner
239
242
  # @return [Logger] The logger object
240
243
  def setup_logger
244
+ if logger
245
+ warn_on_custom_logger
246
+ return logger
247
+ end
241
248
  log = Log4r::Logger.new('goliath')
242
249
 
243
250
  log_format = Log4r::PatternFormatter.new(:pattern => "[#{Process.pid}:%l] %d :: %m")
@@ -312,5 +319,11 @@ module Goliath
312
319
  def remove_pid
313
320
  File.delete(@pid_file)
314
321
  end
322
+
323
+ def warn_on_custom_logger
324
+ warn "log_file option will not take effect with a custom logger" if @log_file
325
+ warn "log_stdout option will not take effect with a custom logger" if @log_stdout
326
+ warn "verbose option will not take effect with a custom logger" if @verbose
327
+ end
315
328
  end
316
329
  end
@@ -159,7 +159,15 @@ module Goliath
159
159
  @plugins.each do |(name, args)|
160
160
  logger.info("Loading #{name.to_s}")
161
161
 
162
- plugin = name.new(port, config, status, logger)
162
+ if name.instance_method(:initialize).arity != 5 then
163
+ logger.warn( "Plugins now take 5 parameters (address, port, config, status, logger). " +
164
+ "You appear to be using the old style 4 parameter method (port, config, status, logger). " +
165
+ "Please update your plugins as the 4 parameter method is deprecated." );
166
+ plugin = name.new(port, config, status, logger)
167
+ else
168
+ plugin = name.new(address, port, config, status, logger)
169
+ end
170
+
163
171
  plugin.run(*args)
164
172
  end
165
173
  end
@@ -27,6 +27,8 @@ module Goliath
27
27
  # end
28
28
  #
29
29
  module TestHelper
30
+ DEFAULT_ERROR = Proc.new { fail "API request failed" }
31
+
30
32
  def self.included(mod)
31
33
  Goliath.env = :test
32
34
  end
@@ -114,7 +116,7 @@ module Goliath
114
116
  # @param request_data [Hash] Any data to pass to the HEAD request.
115
117
  # @param errback [Proc] An error handler to attach
116
118
  # @param blk [Proc] The callback block to execute
117
- def head_request(request_data = {}, errback = nil, &blk)
119
+ def head_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
118
120
  req = create_test_request(request_data).head(request_data)
119
121
  hookup_request_callbacks(req, errback, &blk)
120
122
  end
@@ -124,7 +126,7 @@ module Goliath
124
126
  # @param request_data [Hash] Any data to pass to the GET request.
125
127
  # @param errback [Proc] An error handler to attach
126
128
  # @param blk [Proc] The callback block to execute
127
- def get_request(request_data = {}, errback = nil, &blk)
129
+ def get_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
128
130
  req = create_test_request(request_data).get(request_data)
129
131
  hookup_request_callbacks(req, errback, &blk)
130
132
  end
@@ -134,7 +136,7 @@ module Goliath
134
136
  # @param request_data [Hash] Any data to pass to the POST request.
135
137
  # @param errback [Proc] An error handler to attach
136
138
  # @param blk [Proc] The callback block to execute
137
- def post_request(request_data = {}, errback = nil, &blk)
139
+ def post_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
138
140
  req = create_test_request(request_data).post(request_data)
139
141
  hookup_request_callbacks(req, errback, &blk)
140
142
  end
@@ -144,7 +146,7 @@ module Goliath
144
146
  # @param request_data [Hash] Any data to pass to the PUT request.
145
147
  # @param errback [Proc] An error handler to attach
146
148
  # @param blk [Proc] The callback block to execute
147
- def put_request(request_data = {}, errback = nil, &blk)
149
+ def put_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
148
150
  req = create_test_request(request_data).put(request_data)
149
151
  hookup_request_callbacks(req, errback, &blk)
150
152
  end
@@ -154,7 +156,7 @@ module Goliath
154
156
  # @param request_data [Hash] Any data to pass to the PUT request.
155
157
  # @param errback [Proc] An error handler to attach
156
158
  # @param blk [Proc] The callback block to execute
157
- def patch_request(request_data = {}, errback = nil, &blk)
159
+ def patch_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
158
160
  req = create_test_request(request_data).patch(request_data)
159
161
  hookup_request_callbacks(req, errback, &blk)
160
162
  end
@@ -164,7 +166,7 @@ module Goliath
164
166
  # @param request_data [Hash] Any data to pass to the DELETE request.
165
167
  # @param errback [Proc] An error handler to attach
166
168
  # @param blk [Proc] The callback block to execute
167
- def delete_request(request_data = {}, errback = nil, &blk)
169
+ def delete_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
168
170
  req = create_test_request(request_data).delete(request_data)
169
171
  hookup_request_callbacks(req, errback, &blk)
170
172
  end
@@ -174,7 +176,7 @@ module Goliath
174
176
  # @param request_data [Hash] Any data to pass to the OPTIONS request.
175
177
  # @param errback [Proc] An error handler to attach
176
178
  # @param blk [Proc] The callback block to execute
177
- def options_request(request_data = {}, errback = nil, &blk)
179
+ def options_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
178
180
  req = create_test_request(request_data).options(request_data)
179
181
  hookup_request_callbacks(req, errback, &blk)
180
182
  end
@@ -1,4 +1,4 @@
1
1
  module Goliath
2
2
  # The current version of Goliath
3
- VERSION = '1.0.2'
3
+ VERSION = '1.0.3'
4
4
  end
@@ -59,4 +59,13 @@ describe Echo do
59
59
  end
60
60
  end
61
61
  end
62
+
63
+ it 'echos PATCH data' do
64
+ with_api(Echo) do
65
+ patch_request({:body => {'echo' => 'test'}}, err) do |c|
66
+ b = MultiJson.load(c.response)
67
+ b['response'].should == 'test'
68
+ end
69
+ end
70
+ end
62
71
  end
@@ -55,6 +55,7 @@ describe Goliath::Request do
55
55
  describe 'parse_headers' do
56
56
  it 'sets content_type correctly' do
57
57
  parser = mock('parser').as_null_object
58
+ parser.stub(:request_url).and_return('')
58
59
 
59
60
  @r.parse_header({'Content-Type' => 'text/plain'}, parser)
60
61
  @r.env['CONTENT_TYPE'].should == 'text/plain'
@@ -62,6 +63,7 @@ describe Goliath::Request do
62
63
 
63
64
  it 'sets content_length correctly' do
64
65
  parser = mock('parser').as_null_object
66
+ parser.stub(:request_url).and_return('')
65
67
 
66
68
  @r.parse_header({'Content-Length' => 42}, parser)
67
69
  @r.env['CONTENT_LENGTH'].should == 42
@@ -69,6 +71,7 @@ describe Goliath::Request do
69
71
 
70
72
  it 'sets server_name and server_port correctly' do
71
73
  parser = mock('parser').as_null_object
74
+ parser.stub(:request_url).and_return('')
72
75
 
73
76
  @r.parse_header({'Host' => 'myhost.com:3000'}, parser)
74
77
  @r.env['SERVER_NAME'].should == 'myhost.com'
@@ -95,6 +95,17 @@ describe Goliath::Runner do
95
95
  log = @r.send(:setup_logger)
96
96
  end
97
97
  end
98
+
99
+ describe "custom logger" do
100
+
101
+ it "doesn't configure Log4r" do
102
+ CustomLogger = Struct.new(:info, :debug, :error, :fatal)
103
+ Log4r::Logger.should_not_receive(:new)
104
+ @r.logger = CustomLogger.new
105
+ log = @r.send(:setup_logger)
106
+ end
107
+
108
+ end
98
109
  end
99
110
 
100
111
  it 'creates the log dir if neeed' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: goliath
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-04-27 00:00:00.000000000 Z
13
+ date: 2013-06-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: eventmachine
@@ -67,7 +67,7 @@ dependencies:
67
67
  requirements:
68
68
  - - '='
69
69
  - !ruby/object:Gem::Version
70
- version: 0.5.3
70
+ version: 0.6.0.beta.2
71
71
  type: :runtime
72
72
  prerelease: false
73
73
  version_requirements: !ruby/object:Gem::Requirement
@@ -75,7 +75,7 @@ dependencies:
75
75
  requirements:
76
76
  - - '='
77
77
  - !ruby/object:Gem::Version
78
- version: 0.5.3
78
+ version: 0.6.0.beta.2
79
79
  - !ruby/object:Gem::Dependency
80
80
  name: log4r
81
81
  requirement: !ruby/object:Gem::Requirement
@@ -487,12 +487,10 @@ files:
487
487
  - examples/activerecord/config/srv.rb
488
488
  - examples/activerecord/srv.rb
489
489
  - examples/api_proxy.rb
490
- - examples/around.rb
491
490
  - examples/async_aroundware_demo.rb
492
491
  - examples/async_upload.rb
493
492
  - examples/auth_and_rate_limit.rb
494
493
  - examples/chunked_streaming.rb
495
- - examples/clone.rb
496
494
  - examples/conf_test.rb
497
495
  - examples/config/auth_and_rate_limit.rb
498
496
  - examples/config/conf_test.rb
@@ -519,12 +517,9 @@ files:
519
517
  - examples/rasterize/rasterize.js
520
518
  - examples/rasterize/rasterize.rb
521
519
  - examples/rasterize/rasterize_and_shorten.rb
522
- - examples/router.rb
523
520
  - examples/stream.rb
524
521
  - examples/template.rb
525
- - examples/test.rb
526
522
  - examples/test_rig.rb
527
- - examples/upload.rb
528
523
  - examples/views/debug.haml
529
524
  - examples/views/joke.markdown
530
525
  - examples/views/layout.erb
@@ -1,38 +0,0 @@
1
- #!/usr/bin/env ruby
2
- $:<< '../lib' << 'lib'
3
-
4
- require 'goliath'
5
-
6
- class ApiValidationAroundware
7
- include Goliath::Rack::SimpleAroundware
8
- class InvalidApiKeyError < Goliath::Validation::BadRequestError; end
9
-
10
- def pre_process
11
- validate_api_key!
12
- env.logger.info "past api_key validation" #<-- this is output, then an empty response header & body as if it is just hanging...
13
- Goliath::Connection::AsyncResponse
14
- end
15
-
16
- def post_process
17
- [status, headers, body]
18
- end
19
-
20
- def validate_api_key!
21
- server_api_key = env['config']['server_api_key'].to_s
22
- if api_key != server_api_key
23
- raise InvalidApiKeyError
24
- end
25
- end
26
-
27
- # retreive the client's api_key
28
- def api_key
29
- env['HTTP_API_KEY'].to_s
30
- end
31
- end
32
-
33
- class AwesomeApiWithLogging < Goliath::API
34
- use Goliath::Rack::SimpleAroundwareFactory, ApiValidationAroundware
35
- def response(env)
36
- [200, {}, "Hello"]
37
- end
38
- end
@@ -1,26 +0,0 @@
1
- #!/usr/bin/env ruby
2
- $:<< '../lib' << 'lib'
3
-
4
- require 'goliath'
5
-
6
- class RandomAPI2 < Goliath::API
7
- use Goliath::Rack::Params
8
- use Goliath::Rack::Validation::Param, :key => 'user'
9
-
10
- def response(env)
11
- [200, {}, "Hello 2!"]
12
- end
13
- end
14
-
15
- class Router < Goliath::API
16
- map '/', RandomAPI2
17
- end
18
-
19
- # class PlainApi < Goliath::API
20
- # use Goliath::Rack::Params
21
- # use Goliath::Rack::Validation::Param, :key => 'user'
22
-
23
- # def response(env)
24
- # [200, {}, "Hello 2!"]
25
- # end
26
- # end
@@ -1,15 +0,0 @@
1
- #!/usr/bin/env ruby
2
- $:<< '../lib' << 'lib'
3
-
4
- require 'goliath'
5
- require 'test'
6
-
7
-
8
- class Router < Goliath::API
9
- get '/v1/app/:appid/binary/:key', RawFileApp
10
- put '/v1/app/:appid/binary/:key', RawFileApp
11
-
12
- not_found do
13
- run Proc.new { |env| [404, {"Content-Type" => "text/html"}, "not found"] }
14
- end
15
- end
@@ -1,31 +0,0 @@
1
- #!/usr/bin/env ruby
2
- $:<< '../lib' << 'lib'
3
-
4
- require 'goliath'
5
-
6
- class RawFileApp < Goliath::API
7
- use Goliath::Rack::Params # parse & merge query and body parameters
8
- use Goliath::Rack::DefaultMimeType # cleanup accepted media types
9
- use Goliath::Rack::Formatters::JSON # JSON output formatter
10
- use Goliath::Rack::Render # auto-negotiate response format
11
-
12
- def response(env)
13
- p params
14
- p params['key2']
15
- p params[:key2]
16
- obj = {
17
- somekey: 'val',
18
- otherkey: 42
19
- }
20
- [200, { 'Content-Type' => 'application/json' }, obj]
21
- end
22
- end
23
-
24
- class Router < Goliath::API
25
- get '/v1/app/:appid/binary/:key', RawFileApp
26
- put '/v1/app/:appid/binary/:key', RawFileApp
27
-
28
- not_found do
29
- run Proc.new { |env| [404, {"Content-Type" => "text/html"}, "not found"] }
30
- end
31
- end
@@ -1,17 +0,0 @@
1
- #!/usr/bin/env ruby
2
- $:<< '../lib' << 'lib'
3
-
4
- require 'goliath'
5
-
6
- class Upload < Goliath::API
7
-
8
- def on_headers(env, h)
9
- if h['Expect'] == '100-continue'
10
- env.stream_send "HTTP/1.1 100 Continue\r\n"
11
- end
12
- end
13
-
14
- def response(env)
15
- [200, {}, "oh hai"]
16
- end
17
- end