respect-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/FAQ.md +98 -0
- data/MIT-LICENSE +20 -0
- data/README.md +291 -0
- data/RELATED_WORK.md +47 -0
- data/RELEASE_NOTES.md +20 -0
- data/Rakefile +32 -0
- data/app/assets/javascripts/respect/rails/schemas.js +32 -0
- data/app/assets/stylesheets/respect/rails/schemas.css +160 -0
- data/app/controllers/respect/rails/schemas_controller.rb +36 -0
- data/app/helpers/respect/rails/schemas_helper.rb +78 -0
- data/app/views/layouts/respect/rails/schemas.html.erb +14 -0
- data/app/views/respect/rails/request_validation_exception.html.erb +38 -0
- data/app/views/respect/rails/schemas/doc.html.erb +158 -0
- data/app/views/respect/rails/schemas/index.html.erb +4 -0
- data/config/routes.rb +4 -0
- data/lib/respect/rails/action_def.rb +37 -0
- data/lib/respect/rails/action_schema.rb +41 -0
- data/lib/respect/rails/application_info.rb +26 -0
- data/lib/respect/rails/controller_helper.rb +107 -0
- data/lib/respect/rails/engine.rb +63 -0
- data/lib/respect/rails/engine_info.rb +27 -0
- data/lib/respect/rails/headers_helper.rb +11 -0
- data/lib/respect/rails/headers_simplifier.rb +31 -0
- data/lib/respect/rails/info.rb +72 -0
- data/lib/respect/rails/request_def.rb +38 -0
- data/lib/respect/rails/request_helper.rb +101 -0
- data/lib/respect/rails/request_schema.rb +102 -0
- data/lib/respect/rails/response_def.rb +43 -0
- data/lib/respect/rails/response_helper.rb +67 -0
- data/lib/respect/rails/response_schema.rb +84 -0
- data/lib/respect/rails/response_schema_set.rb +60 -0
- data/lib/respect/rails/route_info.rb +101 -0
- data/lib/respect/rails/version.rb +5 -0
- data/lib/respect/rails.rb +74 -0
- data/lib/tasks/respect_tasks.rake +4 -0
- data/test/dummy/README.rdoc +261 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/assets/javascripts/application.js +15 -0
- data/test/dummy/app/assets/stylesheets/application.css +13 -0
- data/test/dummy/app/controllers/application_controller.rb +3 -0
- data/test/dummy/app/controllers/automatic_validation_controller.rb +300 -0
- data/test/dummy/app/controllers/caught_exception_controller.rb +58 -0
- data/test/dummy/app/controllers/disabled_controller.rb +37 -0
- data/test/dummy/app/controllers/manual_validation_controller.rb +63 -0
- data/test/dummy/app/controllers/no_schema_controller.rb +17 -0
- data/test/dummy/app/controllers/skipped_automatic_validation_controller.rb +35 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/helpers/respect/application_macros.rb +10 -0
- data/test/dummy/app/helpers/respect/circle_schema.rb +16 -0
- data/test/dummy/app/helpers/respect/point_schema.rb +19 -0
- data/test/dummy/app/helpers/respect/rgba_schema.rb +18 -0
- data/test/dummy/app/views/automatic_validation/request_format.html.erb +1 -0
- data/test/dummy/app/views/automatic_validation/request_format.pdf.erb +1 -0
- data/test/dummy/app/views/caught_exception/response_validator.html.erb +1 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/config/application.rb +58 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +67 -0
- data/test/dummy/config/environments/test.rb +37 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +15 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/respect.rb +9 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +38 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/schema.rb +16 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/lib/exts/Rgba.rb +11 -0
- data/test/dummy/lib/exts/circle.rb +11 -0
- data/test/dummy/lib/exts/point.rb +11 -0
- data/test/dummy/log/development.log +6 -0
- data/test/dummy/log/test.log +851 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +25 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/script/rails +6 -0
- data/test/functional/automatic_validation_controller_test.rb +131 -0
- data/test/functional/caught_exception_controller_test.rb +50 -0
- data/test/functional/disabled_controller_test.rb +16 -0
- data/test/functional/manual_validation_controller_test.rb +24 -0
- data/test/functional/no_schema_controller_test.rb +12 -0
- data/test/functional/respect/rails/schemas_controller_test.rb +18 -0
- data/test/functional/skipped_automatic_validation_controller_test.rb +12 -0
- data/test/headers_can_dumped_in_json.sh +33 -0
- data/test/integration/navigation_test.rb +38 -0
- data/test/request_headers_validation_in_dev_mode.sh +33 -0
- data/test/test_helper.rb +17 -0
- data/test/unit/action_schema_test.rb +21 -0
- data/test/unit/application_info_test.rb +11 -0
- data/test/unit/controller_helper_test.rb +4 -0
- data/test/unit/engine_info_test.rb +11 -0
- data/test/unit/engine_test.rb +7 -0
- data/test/unit/info_test.rb +42 -0
- data/test/unit/request_def_test.rb +22 -0
- data/test/unit/request_helper_test.rb +67 -0
- data/test/unit/request_schema_test.rb +164 -0
- data/test/unit/response_def_test.rb +9 -0
- data/test/unit/response_helper_test.rb +73 -0
- data/test/unit/response_schema_set_test.rb +18 -0
- data/test/unit/response_schema_test.rb +147 -0
- metadata +334 -0
@@ -0,0 +1,26 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>The page you were looking for doesn't exist (404)</title>
|
5
|
+
<style type="text/css">
|
6
|
+
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
|
7
|
+
div.dialog {
|
8
|
+
width: 25em;
|
9
|
+
padding: 0 4em;
|
10
|
+
margin: 4em auto 0 auto;
|
11
|
+
border: 1px solid #ccc;
|
12
|
+
border-right-color: #999;
|
13
|
+
border-bottom-color: #999;
|
14
|
+
}
|
15
|
+
h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
|
16
|
+
</style>
|
17
|
+
</head>
|
18
|
+
|
19
|
+
<body>
|
20
|
+
<!-- This file lives in public/404.html -->
|
21
|
+
<div class="dialog">
|
22
|
+
<h1>The page you were looking for doesn't exist.</h1>
|
23
|
+
<p>You may have mistyped the address or the page may have moved.</p>
|
24
|
+
</div>
|
25
|
+
</body>
|
26
|
+
</html>
|
@@ -0,0 +1,26 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>The change you wanted was rejected (422)</title>
|
5
|
+
<style type="text/css">
|
6
|
+
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
|
7
|
+
div.dialog {
|
8
|
+
width: 25em;
|
9
|
+
padding: 0 4em;
|
10
|
+
margin: 4em auto 0 auto;
|
11
|
+
border: 1px solid #ccc;
|
12
|
+
border-right-color: #999;
|
13
|
+
border-bottom-color: #999;
|
14
|
+
}
|
15
|
+
h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
|
16
|
+
</style>
|
17
|
+
</head>
|
18
|
+
|
19
|
+
<body>
|
20
|
+
<!-- This file lives in public/422.html -->
|
21
|
+
<div class="dialog">
|
22
|
+
<h1>The change you wanted was rejected.</h1>
|
23
|
+
<p>Maybe you tried to change something you didn't have access to.</p>
|
24
|
+
</div>
|
25
|
+
</body>
|
26
|
+
</html>
|
@@ -0,0 +1,25 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>We're sorry, but something went wrong (500)</title>
|
5
|
+
<style type="text/css">
|
6
|
+
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
|
7
|
+
div.dialog {
|
8
|
+
width: 25em;
|
9
|
+
padding: 0 4em;
|
10
|
+
margin: 4em auto 0 auto;
|
11
|
+
border: 1px solid #ccc;
|
12
|
+
border-right-color: #999;
|
13
|
+
border-bottom-color: #999;
|
14
|
+
}
|
15
|
+
h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
|
16
|
+
</style>
|
17
|
+
</head>
|
18
|
+
|
19
|
+
<body>
|
20
|
+
<!-- This file lives in public/500.html -->
|
21
|
+
<div class="dialog">
|
22
|
+
<h1>We're sorry, but something went wrong.</h1>
|
23
|
+
</div>
|
24
|
+
</body>
|
25
|
+
</html>
|
File without changes
|
@@ -0,0 +1,6 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
|
3
|
+
|
4
|
+
APP_PATH = File.expand_path('../../config/application', __FILE__)
|
5
|
+
require File.expand_path('../../config/boot', __FILE__)
|
6
|
+
require 'rails/commands'
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class AutomaticValidationControllerTest < ActionController::TestCase
|
4
|
+
|
5
|
+
def test_basic_get_works
|
6
|
+
get :basic_get, format: 'json', param1: "42"
|
7
|
+
assert_response :success
|
8
|
+
assert response.has_schema?
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_basic_request_error_raises_exception
|
12
|
+
assert_raise(Respect::Rails::RequestValidationError) do
|
13
|
+
get :basic_get, format: 'json', param1: 54
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_no_schema_at_all_always_validate_request_and_response
|
18
|
+
get :no_schema_at_all, format: 'json', param1: "whatever"
|
19
|
+
assert_response :success
|
20
|
+
assert !response.has_schema?
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_no_request_schema_still_validate_good_response
|
24
|
+
get :no_request_schema, format: 'json', returned_id: 42
|
25
|
+
assert_response :success
|
26
|
+
assert response.has_schema?
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_no_request_schema_still_invalidate_wrong_response
|
30
|
+
assert_raises(Respect::Rails::ResponseValidationError) do
|
31
|
+
get :no_request_schema, format: 'json', returned_id: 6666666
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_route_constraints_validates_before_validator
|
36
|
+
# FIXME(Nicolas Despres): This test does not work because route's constraints do not
|
37
|
+
# seem to be applied in test mode. The test work manually from the web browser. Here we
|
38
|
+
# get a ValidationError instead of a RoutingError.
|
39
|
+
# assert_raise(ActionController::RoutingError) do
|
40
|
+
# get :route_constraints, format: 'json', param1: "51"
|
41
|
+
# end
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_composite_custom_types
|
45
|
+
# assert_nothing_raised do
|
46
|
+
get :composite_custom_types, format: 'json',
|
47
|
+
circle: { center: { x: "1.0", y: "2.0" }, radius: "5.5" },
|
48
|
+
color: [ 0.0, 0.1, 0.2, 0.3 ]
|
49
|
+
# end
|
50
|
+
assert_response :success
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_engine_uri_helpers
|
54
|
+
get :dump_uri_helpers, format: 'json'
|
55
|
+
assert_response :success
|
56
|
+
|
57
|
+
json = ActiveSupport::JSON.decode(@response.body)
|
58
|
+
|
59
|
+
assert_equal "/rest_spec", json['respect_path']
|
60
|
+
assert_equal "/rest_spec/doc", json['respect']['doc_path']
|
61
|
+
assert_equal "/rest_spec/", json['respect']['root_path']
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_request_validation_error_have_context
|
65
|
+
begin
|
66
|
+
get :request_contextual_error, format: 'json', o1: { o2: { i: 54 } }
|
67
|
+
assert false, "exception should be raised"
|
68
|
+
rescue Respect::Rails::RequestValidationError => e
|
69
|
+
assert(e.context.first == e.error.message)
|
70
|
+
assert_match e.context.last, /\b#{e.part}\b/
|
71
|
+
assert_equal(5, e.context.size)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_response_validation_error_have_context
|
76
|
+
begin
|
77
|
+
get :response_contextual_error, format: 'json'
|
78
|
+
assert false
|
79
|
+
rescue Respect::Rails::ResponseValidationError => e
|
80
|
+
assert(e.context.first == e.error.message)
|
81
|
+
assert_match e.context.last, /\b#{e.part}\b/
|
82
|
+
assert_equal(5, e.context.size)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_format_is_not_required
|
87
|
+
get :request_format
|
88
|
+
assert_response :success
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_format_html_validate
|
92
|
+
get :request_format, format: "html"
|
93
|
+
assert_response :success
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_format_json_validate
|
97
|
+
get :request_format, format: "json"
|
98
|
+
assert_response :success
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_format_pdf_validate
|
102
|
+
get :request_format, format: "pdf"
|
103
|
+
assert_response :success
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_post_valid_request
|
107
|
+
post :basic_post, format: 'json', path_param: "42", body_param: "42", response_param: 42
|
108
|
+
assert_response :success
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_successful_response_headers_check
|
112
|
+
get :check_response_headers, format: 'json', response_header_value: "good"
|
113
|
+
assert_response :success
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_failed_response_headers_check
|
117
|
+
assert_raises(Respect::Rails::ResponseValidationError) do
|
118
|
+
get :check_response_headers, format: 'json', response_header_value: "wrong"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# FIXME(Nicolas Despres): We cannot do functional test easily on this action because
|
123
|
+
# it expects an header to be set and Rails 3 does not provide an easy way to set it here.
|
124
|
+
# That's why request headers validation is disabled by default in test mode so we can do
|
125
|
+
# non-headers related test on this method.
|
126
|
+
# def test_successful_request_http_headers_check
|
127
|
+
# get :check_request_headers, format: 'json'
|
128
|
+
# assert_response :success
|
129
|
+
# end
|
130
|
+
|
131
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CaughtExceptionControllerTest < ActionController::TestCase
|
4
|
+
|
5
|
+
def test_request_validation_error_rendered_when_rescued_in_html_format
|
6
|
+
get :request_validator, format: 'html', id: "error_value"
|
7
|
+
assert_response :internal_server_error
|
8
|
+
assert_select "title", { count: 1, text: "Request validation error caught" }
|
9
|
+
assert_select "h1", { count: 1, text: /validation error/i }
|
10
|
+
assert_select "h1", /in CaughtExceptionController#request_validator/
|
11
|
+
assert_select "pre", /\bin hash property\b/
|
12
|
+
assert_select "pre", /\bid\b/
|
13
|
+
assert_select "pre", /\bmalformed integer value\b/
|
14
|
+
assert_select "pre", /\berror_value\b/
|
15
|
+
assert_select "p", /\bInvalid query parameters\b/
|
16
|
+
assert_select "p", /\bProcessed object\b/ do
|
17
|
+
assert_select "div[class=json_highlight]", true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_request_validation_error_rendered_when_rescued_in_json_format
|
22
|
+
get :request_validator, format: 'json', id: "error_value"
|
23
|
+
assert_response :internal_server_error
|
24
|
+
json = ActiveSupport::JSON.decode(response.body)
|
25
|
+
assert json.key?("error")
|
26
|
+
assert_equal("Respect::Rails::RequestValidationError", json["error"]["class"])
|
27
|
+
assert_match(/\bmalformed integer value\b/, json["error"]["message"])
|
28
|
+
assert_match(/\berror_value\b/, json["error"]["message"])
|
29
|
+
assert_kind_of(Array, json["error"]["context"])
|
30
|
+
assert_equal("query", json["error"]["part"])
|
31
|
+
assert_equal({"id"=>"error_value",
|
32
|
+
"format"=>"json",
|
33
|
+
"controller"=>"caught_exception",
|
34
|
+
"action"=>"request_validator"}, json["error"]["object"])
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_response_validation_error_not_raised_when_caught
|
38
|
+
Respect::Rails::Engine.stubs(:catch_response_validation_error).returns(true)
|
39
|
+
get :response_validator, format: 'json', id: "error_value"
|
40
|
+
assert_response :internal_server_error
|
41
|
+
assert_equal(response.body, response.last_validation_error.to_json)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_no_response_validation_for_html_format
|
45
|
+
get :response_validator, format: 'html', id: "error_value"
|
46
|
+
assert_response :success
|
47
|
+
assert_select "body", "No response validation for HTML format."
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DisabledControllerTest < ActionController::TestCase
|
4
|
+
|
5
|
+
def test_basic_good_request
|
6
|
+
get :basic, format: 'json', param1: 42
|
7
|
+
assert_response :success
|
8
|
+
assert !response.has_schema?
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_basic_wrong_request_dont_raise
|
12
|
+
get :basic, format: 'json', param1: 54
|
13
|
+
assert_response :success
|
14
|
+
assert !response.has_schema?
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ManualValidationControllerTest < ActionController::TestCase
|
4
|
+
|
5
|
+
def test_raise_custom_error_with_manual_request_validation
|
6
|
+
assert_raise(RuntimeError) do
|
7
|
+
get :raise_custom_error, format: 'json', param1: 54
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_raise_custom_error_with_manual_request_validation_without_rescue
|
12
|
+
assert_raise(RuntimeError) do
|
13
|
+
get :raise_custom_error_without_rescue, format: 'json', param1: 54
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_no_schema_always_validate
|
18
|
+
get :no_schema, format: 'json', param1: 1664
|
19
|
+
assert_response :success
|
20
|
+
assert !response.has_schema?
|
21
|
+
assert response.validate_schema?
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class NoSchemaControllerTest < ActionController::TestCase
|
4
|
+
|
5
|
+
def test_no_schema_always_validate
|
6
|
+
# We send invalid value but it validates anyway because their is no schema.
|
7
|
+
get :basic, format: 'json', param1: 51
|
8
|
+
assert_response :success
|
9
|
+
assert !response.has_schema?
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SchemasControllerTest < ActionController::TestCase
|
4
|
+
tests Respect::Rails::SchemasController
|
5
|
+
|
6
|
+
def test_should_get_index
|
7
|
+
get :index, use_route: :respect
|
8
|
+
assert_response :success
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_should_get_doc
|
12
|
+
get :doc, use_route: :respect
|
13
|
+
assert_response :success
|
14
|
+
# FIXME(Nicolas Despres): Test view using assert_select once we
|
15
|
+
# have restructured the HTML more cleanly.
|
16
|
+
# FIXME(Nicolas Despres): Test listing are sorted.
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SkippedAutomaticValidationControllerTest < ActionController::TestCase
|
4
|
+
|
5
|
+
def test_basic_get_works
|
6
|
+
assert_raises(RuntimeError) do
|
7
|
+
# The schema say that 42 is expected for param1, not 51.
|
8
|
+
get :basic_get, format: 'json', param1: 51
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
#
|
3
|
+
# Some integration test in development mode.
|
4
|
+
# It tests that headers can be dumped in JSON. It happens when a request headers validation error is caught.
|
5
|
+
|
6
|
+
set -o errexit
|
7
|
+
set -o nounset
|
8
|
+
|
9
|
+
export LC_ALL=C
|
10
|
+
|
11
|
+
: ${RAILS_ROOT:=.}
|
12
|
+
: ${PORT:=3000}
|
13
|
+
|
14
|
+
(
|
15
|
+
cd "$RAILS_ROOT/test/dummy"
|
16
|
+
rails server --daemon --port=$PORT 2>&1 >/dev/null
|
17
|
+
sleep 2
|
18
|
+
)
|
19
|
+
|
20
|
+
result=$(curl \
|
21
|
+
-v \
|
22
|
+
-H "X-Test-Header: wrong" \
|
23
|
+
"http://localhost:$PORT/caught_exception/headers_check.json" 2>&1)
|
24
|
+
|
25
|
+
kill -9 $(cat "$RAILS_ROOT/test/dummy/tmp/pids/server.pid")
|
26
|
+
|
27
|
+
if grep -q '^< HTTP/1.1 500 Internal Server Error' <<< "$result" && grep -q '^< Content-Type: application/json' <<< "$result"
|
28
|
+
then
|
29
|
+
exit 0
|
30
|
+
else
|
31
|
+
echo "$result"
|
32
|
+
exit 1
|
33
|
+
fi
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class NavigationTest < ActionDispatch::IntegrationTest
|
4
|
+
fixtures :all
|
5
|
+
|
6
|
+
# FIXME(Nicolas Despres): Replace this test by lighter routes assertion
|
7
|
+
# when we succeed to make them works on an isolated engine (the use_route
|
8
|
+
# option does not seems to work here).
|
9
|
+
test "get the full API doc" do
|
10
|
+
get "/rest_spec"
|
11
|
+
assert_response :success
|
12
|
+
|
13
|
+
get "/rest_spec/doc"
|
14
|
+
assert_response :success
|
15
|
+
|
16
|
+
get "/rest_spec/doc.html"
|
17
|
+
assert_response :success
|
18
|
+
end
|
19
|
+
|
20
|
+
test "params set by route are accepted" do
|
21
|
+
get "/"
|
22
|
+
assert_response :success
|
23
|
+
end
|
24
|
+
|
25
|
+
test "request HTTP headers validate successfully" do
|
26
|
+
get "/automatic_validation/check_request_headers.json", {}, { "X-Test-Header" => "value" }
|
27
|
+
assert_response :success
|
28
|
+
end
|
29
|
+
|
30
|
+
test "request HTTP headers fail to validate" do
|
31
|
+
# We force request headers validation for the sake of automated tests.
|
32
|
+
Respect::Rails::Engine.stubs(:disable_request_headers_validation).returns(false)
|
33
|
+
assert_raises(Respect::Rails::RequestValidationError) do
|
34
|
+
get "/automatic_validation/check_request_headers.json", {}, {"X-Test-Header" => "erroneous_value"}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
#
|
3
|
+
# Test request headers check in development mode.
|
4
|
+
#
|
5
|
+
|
6
|
+
set -o errexit
|
7
|
+
set -o nounset
|
8
|
+
|
9
|
+
export LC_ALL=C
|
10
|
+
|
11
|
+
: ${RAILS_ROOT:=.}
|
12
|
+
: ${PORT:=3000}
|
13
|
+
|
14
|
+
(
|
15
|
+
cd "$RAILS_ROOT/test/dummy"
|
16
|
+
rails server --daemon --port=$PORT 2>&1 >/dev/null
|
17
|
+
sleep 2
|
18
|
+
)
|
19
|
+
|
20
|
+
result=$(curl \
|
21
|
+
-v \
|
22
|
+
-H "X-Test-Header: value" \
|
23
|
+
"http://localhost:3000/automatic_validation/check_request_headers.json" 2>&1)
|
24
|
+
|
25
|
+
kill -9 $(cat "$RAILS_ROOT/test/dummy/tmp/pids/server.pid")
|
26
|
+
|
27
|
+
if grep -q "^< HTTP/1.1 200 OK" <<< "$result"
|
28
|
+
then
|
29
|
+
exit 0
|
30
|
+
else
|
31
|
+
echo "$result"
|
32
|
+
exit 1
|
33
|
+
fi
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# Configure Rails Environment
|
2
|
+
ENV["RAILS_ENV"] = "test"
|
3
|
+
|
4
|
+
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
5
|
+
require "rails/test_help"
|
6
|
+
|
7
|
+
Rails.backtrace_cleaner.remove_silencers!
|
8
|
+
|
9
|
+
# Load support files
|
10
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
11
|
+
|
12
|
+
# Load fixtures from the engine
|
13
|
+
if ActiveSupport::TestCase.method_defined?(:fixture_path=)
|
14
|
+
ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
|
15
|
+
end
|
16
|
+
|
17
|
+
require "mocha/setup"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ActionSchematest < ActiveSupport::TestCase
|
4
|
+
test "instantiating from controller do not catch NoMethodError" do
|
5
|
+
assert_raise(NoMethodError) do
|
6
|
+
Respect::Rails::ActionSchema.from_controller(:automatic_validation, :raise_no_method_error)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
test "instantiating from controller do not catch NameError" do
|
11
|
+
assert_raise(NameError) do
|
12
|
+
Respect::Rails::ActionSchema.from_controller(:automatic_validation, :raise_name_error)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
test "has_schema" do
|
17
|
+
assert(!Respect::Rails::ActionSchema.from_controller(:automatic_validation, :no_schema_at_all))
|
18
|
+
assert(Respect::Rails::ActionSchema.from_controller(:automatic_validation, :no_request_schema).has_schema?)
|
19
|
+
assert(Respect::Rails::ActionSchema.from_controller(:automatic_validation, :only_documentation).has_schema?)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class InfoTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@info = Respect::Rails::Info.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_engine_route_are_prefixed
|
10
|
+
assert(@info.routes.any?{|r| r.path =~ %r{^/rest_spec/doc} })
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_app_is_stored_in_engines
|
14
|
+
assert(@info.engines.has_value?(@info.app))
|
15
|
+
assert(@info.routes == @info.app.routes)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_toc
|
19
|
+
# FIXME(Nicolas Despres): Use a moch instead of relying on the dummy app which may change too often.
|
20
|
+
assert_equal 6, @info.toc.size
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_route_not_collected_if_no_schema
|
24
|
+
# FIXME(Nicolas Despres): Use a moch instead of relying on the dummy app which may change too often.
|
25
|
+
assert(@info.routes.none?{|r| r.path =~ %r{/automatic_validation/no_schema_at_all} })
|
26
|
+
assert(@info.routes.any?{|r| r.path =~ %r{/automatic_validation/no_request_schema} })
|
27
|
+
assert(@info.routes.none?{|r| r.path =~ %r{/manual_validation/no_schema} })
|
28
|
+
assert(@info.routes.none?{|r| r.path =~ %r{/no_schema/basic} })
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_action_not_in_toc_if_no_schemas
|
32
|
+
# FIXME(Nicolas Despres): Use a moch instead of relying on the dummy app which may change too often.
|
33
|
+
assert(!@info.toc["automatic_validation"].key?("no_schema_at_all"))
|
34
|
+
assert(@info.toc["automatic_validation"].key?("no_request_schema"))
|
35
|
+
assert(!@info.toc["manual_validation"].key?("no_schema"))
|
36
|
+
assert(!@info.toc.key?("no_schema"))
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_action_with_only_documentation_are_in_toc
|
40
|
+
assert(@info.toc["automatic_validation"].key?("only_documentation"))
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class RequestDefTest < ActiveSupport::TestCase
|
4
|
+
test "can use kernel method" do
|
5
|
+
Respect::Rails::RequestDef.eval("controller", "action") do
|
6
|
+
p
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
test "path params are not merged when called twice" do
|
11
|
+
r = Respect::Rails::RequestDef.new("ctrl", "act").eval do |r|
|
12
|
+
r.path_parameters do |s|
|
13
|
+
s.integer "foo"
|
14
|
+
end
|
15
|
+
r.path_parameters do |s|
|
16
|
+
s.integer "bar"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
assert r.path_parameters.has_property?("bar")
|
20
|
+
assert !r.path_parameters.has_property?("foo")
|
21
|
+
end
|
22
|
+
end
|