racknga 0.9.3 → 0.9.4

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.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +3 -12
  3. data/README.textile +9 -8
  4. data/Rakefile +19 -272
  5. data/doc/text/news.textile +8 -0
  6. data/lib/racknga/access_log_parser.rb +1 -1
  7. data/lib/racknga/api-keys.rb +40 -0
  8. data/lib/racknga/cache_database.rb +2 -2
  9. data/lib/racknga/exception_mail_notifier.rb +5 -5
  10. data/lib/racknga/log_database.rb +1 -1
  11. data/lib/racknga/log_entry.rb +1 -1
  12. data/lib/racknga/middleware/auth/api-key.rb +95 -0
  13. data/lib/racknga/middleware/cache.rb +20 -6
  14. data/lib/racknga/middleware/deflater.rb +1 -1
  15. data/lib/racknga/middleware/exception_notifier.rb +1 -1
  16. data/lib/racknga/middleware/instance_name.rb +65 -12
  17. data/lib/racknga/middleware/jsonp.rb +24 -5
  18. data/lib/racknga/middleware/log.rb +1 -1
  19. data/lib/racknga/middleware/nginx_raw_uri.rb +1 -1
  20. data/lib/racknga/middleware/range.rb +1 -1
  21. data/lib/racknga/reverse_line_reader.rb +1 -1
  22. data/lib/racknga/utils.rb +1 -1
  23. data/lib/racknga/version.rb +2 -2
  24. data/lib/racknga.rb +2 -1
  25. data/munin/plugins/passenger_application_ +61 -47
  26. data/munin/plugins/passenger_status +64 -33
  27. data/test/racknga-test-utils.rb +2 -1
  28. data/test/run-test.rb +1 -3
  29. data/test/test-access-log-parser.rb +1 -1
  30. data/test/test-api-keys.rb +80 -0
  31. data/test/test-middleware-auth-api-key.rb +144 -0
  32. data/test/test-middleware-cache.rb +1 -1
  33. data/test/test-middleware-instance-name.rb +32 -10
  34. data/test/test-middleware-jsonp.rb +14 -2
  35. data/test/test-middleware-nginx-raw-uri.rb +1 -1
  36. data/test/test-middleware-range.rb +1 -1
  37. metadata +156 -156
  38. data/lib/racknga/will_paginate.rb +0 -48
@@ -12,12 +12,13 @@
12
12
  #
13
13
  # You should have received a copy of the GNU Lesser General Public
14
14
  # License along with this library; if not, write to the Free Software
15
- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
  require "test/unit/capybara"
18
18
  require 'json'
19
19
 
20
20
  require 'racknga'
21
+ require 'racknga/middleware/auth/api-key'
21
22
  require 'racknga/middleware/cache'
22
23
  require 'racknga/middleware/instance_name'
23
24
 
data/test/run-test.rb CHANGED
@@ -14,12 +14,10 @@
14
14
  #
15
15
  # You should have received a copy of the GNU Lesser General Public
16
16
  # License along with this library; if not, write to the Free Software
17
- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
 
19
19
  $VERBOSE = true
20
20
 
21
- $KCODE = "u" if RUBY_VERSION < "1.9"
22
-
23
21
  require 'pathname'
24
22
 
25
23
  base_dir = Pathname(__FILE__).dirname.parent.expand_path
@@ -15,7 +15,7 @@
15
15
  #
16
16
  # You should have received a copy of the GNU Lesser General Public
17
17
  # License along with this library; if not, write to the Free Software
18
- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
19
 
20
20
  require 'stringio'
21
21
 
@@ -0,0 +1,80 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright (C) 2012 Haruka Yoshihara <yoshihara@clear-code.com>
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+
19
+ class APIKeysTest < Test::Unit::TestCase
20
+ include RackngaTestUtils
21
+
22
+ def setup
23
+ @api_keys = Racknga::APIKeys.new(query_parameter, api_keys)
24
+ end
25
+
26
+ def test_matched_key
27
+ assert_matched(query_parameter, api_key)
28
+ end
29
+
30
+ def test_not_matched_key
31
+ assert_not_matched(query_parameter, "notapikey")
32
+ end
33
+
34
+ def test_empty_key
35
+ assert_not_matched(query_parameter, "")
36
+ end
37
+
38
+ def test_empty_query_parameter
39
+ assert_not_matched("", api_key)
40
+ end
41
+
42
+ def test_mismatched_query_parameter
43
+ assert_not_matched("not-api-key", api_key)
44
+ end
45
+
46
+ private
47
+ def assert_matched(parameter, key)
48
+ environment = generate_environment(parameter, key)
49
+ assert_true(@api_keys.include?(environment))
50
+ end
51
+
52
+ def assert_not_matched(parameter, key)
53
+ environment = generate_environment(parameter, key)
54
+ assert_false(@api_keys.include?(environment))
55
+ end
56
+
57
+ def generate_environment(parameter, key)
58
+ url = "http://example.org"
59
+
60
+ unless parameter.empty?
61
+ api_url = "#{url}?#{parameter}=#{key}"
62
+ else
63
+ api_url = url
64
+ end
65
+
66
+ Rack::MockRequest.env_for(api_url)
67
+ end
68
+
69
+ def query_parameter
70
+ "api-key"
71
+ end
72
+
73
+ def api_keys
74
+ ["key", "key2"]
75
+ end
76
+
77
+ def api_key
78
+ api_keys.first
79
+ end
80
+ end
@@ -0,0 +1,144 @@
1
+ # Copyright (C) 2012 Haruka Yoshihara <kou@clear-code.com>
2
+ #
3
+ # This library is free software; you can redistribute it and/or
4
+ # modify it under the terms of the GNU Lesser General Public
5
+ # License as published by the Free Software Foundation; either
6
+ # version 2.1 of the License, or (at your option) any later version.
7
+ #
8
+ # This library is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
+ # Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public
14
+ # License along with this library; if not, write to the Free Software
15
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+
17
+ class MiddlewareAuthAPIKeyTest < Test::Unit::TestCase
18
+ include RackngaTestUtils
19
+
20
+ def app
21
+ application = Proc.new do |environment|
22
+ @environment = environment
23
+ [
24
+ 200,
25
+ {"Content-Type" => "application/json"},
26
+ [@body]
27
+ ]
28
+ end
29
+ authorized_api_keys =
30
+ Racknga::APIKeys.new(@api_query_parameter, @api_keys)
31
+ api_key_options = {
32
+ :api_url_prefixes => [@api_url_prefix],
33
+ :authorized_api_keys => authorized_api_keys,
34
+ :error_response => @error_response,
35
+ :disable_authorization => disable_authorization?
36
+ }
37
+ api_key = api_key_authorizer(application, api_key_options)
38
+ Proc.new do |environment|
39
+ api_key.call(environment)
40
+ end
41
+ end
42
+
43
+ def setup
44
+ @api_query_parameter = "api-key"
45
+ @api_url_prefix = "/api"
46
+ @api_keys = ["key1", "key2"]
47
+ @body = "{}"
48
+ @error_response = {"error" => "this api key is not authorized"}
49
+ Capybara.app = app
50
+ end
51
+
52
+ def test_authorized_key
53
+ url = generate_url(url_prefix, query_parameter, valid_key)
54
+ visit(url)
55
+ assert_success_response
56
+ end
57
+
58
+ def test_unauthorized_key
59
+ url = generate_url(url_prefix, query_parameter, "invalidkey")
60
+ visit(url)
61
+ assert_failure_response
62
+ end
63
+
64
+ def test_unmatched_query_parameter
65
+ url = generate_url(url_prefix, "not-api-key", valid_key)
66
+ visit(url)
67
+ assert_failure_response
68
+ end
69
+
70
+ def test_unauthorized_key_and_unmatched_query_parameter
71
+ url = generate_url(url_prefix, "not-api-key", "invalidkey")
72
+ visit(url)
73
+ assert_failure_response
74
+ end
75
+
76
+ def test_not_api_url
77
+ visit("/not/api/url")
78
+ assert_success_response
79
+ end
80
+
81
+ private
82
+ def disable_authorization?
83
+ false
84
+ end
85
+
86
+ def api_key_authorizer(application, api_key_options)
87
+ Racknga::Middleware::Auth::APIKey.new(application, api_key_options)
88
+ end
89
+
90
+ def generate_url(path, query_parameter, api_key)
91
+ url = path
92
+ if query_parameter
93
+ url << "?#{query_parameter}=#{api_key}"
94
+ end
95
+ url
96
+ end
97
+
98
+ def url_prefix
99
+ @api_url_prefix
100
+ end
101
+
102
+ def query_parameter
103
+ @api_query_parameter
104
+ end
105
+
106
+ def valid_key
107
+ @api_keys.first
108
+ end
109
+
110
+ def assert_success_response
111
+ assert_equal(200, page.status_code)
112
+ assert_equal(@body, page.source)
113
+ end
114
+
115
+ def assert_failure_response
116
+ assert_equal(401, page.status_code)
117
+ assert_equal(@error_response.to_json, page.source)
118
+ end
119
+
120
+ class DisableAuthorizationTest < self
121
+ def test_unauthorized_key
122
+ url = generate_url(url_prefix, query_parameter, "invalidkey")
123
+ visit(url)
124
+ assert_success_response
125
+ end
126
+
127
+ def test_unmatched_query_parameter
128
+ url = generate_url(url_prefix, "not-api-key", valid_key)
129
+ visit(url)
130
+ assert_success_response
131
+ end
132
+
133
+ def test_unauthorized_key_and_unmatched_query_parameter
134
+ url = generate_url(url_prefix, "not-api-key", "invalidkey")
135
+ visit(url)
136
+ assert_success_response
137
+ end
138
+
139
+ private
140
+ def disable_authorization?
141
+ true
142
+ end
143
+ end
144
+ end
@@ -12,7 +12,7 @@
12
12
  #
13
13
  # You should have received a copy of the GNU Lesser General Public
14
14
  # License along with this library; if not, write to the Free Software
15
- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
  class MiddlewareCacheTest < Test::Unit::TestCase
18
18
  include RackngaTestUtils
@@ -12,17 +12,31 @@
12
12
  #
13
13
  # You should have received a copy of the GNU Lesser General Public
14
14
  # License along with this library; if not, write to the Free Software
15
- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
  class InstanceNameTest < Test::Unit::TestCase
18
18
  include RackngaTestUtils
19
19
 
20
+ DEFAULT_HEADER_NAME = "X-Responsed-By"
21
+
20
22
  def test_no_option
21
23
  footer_variables = extract_from_default_instance_name
22
24
 
23
25
  instance_name_options({}) do
24
26
  request
25
- assert_header("Proc #{default_footer(*footer_variables)}")
27
+ assert_equal("Proc #{default_footer(*footer_variables)}",
28
+ response_header(DEFAULT_HEADER_NAME))
29
+ end
30
+ end
31
+
32
+ def test_header_name
33
+ header_name = "X-header-name"
34
+ footer_variables = extract_from_default_instance_name
35
+
36
+ instance_name_options(:header_name => header_name) do
37
+ request
38
+ assert_equal("Proc #{default_footer(*footer_variables)}",
39
+ response_header(header_name))
26
40
  end
27
41
  end
28
42
 
@@ -32,8 +46,9 @@ class InstanceNameTest < Test::Unit::TestCase
32
46
 
33
47
  instance_name_options(:application_name => application_name) do
34
48
  request
35
- assert_header("#{application_name} " +
36
- "#{default_footer(*footer_variables)}")
49
+ assert_equal("#{application_name} " +
50
+ "#{default_footer(*footer_variables)}",
51
+ response_header(DEFAULT_HEADER_NAME))
37
52
  end
38
53
  end
39
54
 
@@ -45,8 +60,9 @@ class InstanceNameTest < Test::Unit::TestCase
45
60
  instance_name_options(:application_name => application_name,
46
61
  :version => version) do
47
62
  request
48
- assert_header("#{application_name} v#{version} " +
49
- "#{default_footer(*footer_variables)}")
63
+ assert_equal("#{application_name} v#{version} " +
64
+ "#{default_footer(*footer_variables)}",
65
+ response_header(DEFAULT_HEADER_NAME))
50
66
  end
51
67
  end
52
68
 
@@ -83,7 +99,10 @@ class InstanceNameTest < Test::Unit::TestCase
83
99
  end
84
100
 
85
101
  def default_footer(server, user, revision, branch, ruby)
86
- "(at #{revision} (#{branch})) on #{server} by #{user} with #{ruby}"
102
+ footer = ""
103
+ footer << "(at #{revision} (#{branch})) " if format_is_possible?(revision)
104
+ footer << "on #{server} by #{user} with #{ruby}"
105
+ footer
87
106
  end
88
107
 
89
108
  def create_minimal_application
@@ -103,8 +122,11 @@ class InstanceNameTest < Test::Unit::TestCase
103
122
  yield
104
123
  end
105
124
 
106
- def assert_header(expected_header)
107
- actual_header = page.response_headers["X-Responsed-By"]
108
- assert_equal(expected_header, actual_header)
125
+ def response_header(name)
126
+ page.response_headers[name]
127
+ end
128
+
129
+ def format_is_possible?(data)
130
+ data and (data.respond_to?(:to_s) and not data.to_s.empty?)
109
131
  end
110
132
  end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2010 Kouhei Sutou <kou@clear-code.com>
1
+ # Copyright (C) 2010-2012 Kouhei Sutou <kou@clear-code.com>
2
2
  #
3
3
  # This library is free software; you can redistribute it and/or
4
4
  # modify it under the terms of the GNU Lesser General Public
@@ -12,7 +12,7 @@
12
12
  #
13
13
  # You should have received a copy of the GNU Lesser General Public
14
14
  # License along with this library; if not, write to the Free Software
15
- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
  class MiddlewareJSONPTest < Test::Unit::TestCase
18
18
  include RackngaTestUtils
@@ -76,6 +76,18 @@ class MiddlewareJSONPTest < Test::Unit::TestCase
76
76
  @environment[@cache_key_key])
77
77
  end
78
78
 
79
+ def test_jsonp_content_length
80
+ @update_environment = Proc.new do |environment|
81
+ environment["Content-Length"] = @body.bytesize
82
+ environment
83
+ end
84
+ visit("/?callback=jsonp_callback")
85
+ expected_response = "jsonp_callback(#{@body});"
86
+ assert_jsonp_response(expected_response)
87
+ assert_equal(expected_response.bytesize.to_s,
88
+ page.response_headers["Content-Length"])
89
+ end
90
+
79
91
  private
80
92
  def assert_jsonp_response(body)
81
93
  assert_equal({
@@ -12,7 +12,7 @@
12
12
  #
13
13
  # You should have received a copy of the GNU Lesser General Public
14
14
  # License along with this library; if not, write to the Free Software
15
- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
  class NginxRawURITest < Test::Unit::TestCase
18
18
  include RackngaTestUtils
@@ -12,7 +12,7 @@
12
12
  #
13
13
  # You should have received a copy of the GNU Lesser General Public
14
14
  # License along with this library; if not, write to the Free Software
15
- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
  module MiddlewareRangeTests
18
18
  include RackngaTestUtils