respect-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. data/FAQ.md +98 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +291 -0
  4. data/RELATED_WORK.md +47 -0
  5. data/RELEASE_NOTES.md +20 -0
  6. data/Rakefile +32 -0
  7. data/app/assets/javascripts/respect/rails/schemas.js +32 -0
  8. data/app/assets/stylesheets/respect/rails/schemas.css +160 -0
  9. data/app/controllers/respect/rails/schemas_controller.rb +36 -0
  10. data/app/helpers/respect/rails/schemas_helper.rb +78 -0
  11. data/app/views/layouts/respect/rails/schemas.html.erb +14 -0
  12. data/app/views/respect/rails/request_validation_exception.html.erb +38 -0
  13. data/app/views/respect/rails/schemas/doc.html.erb +158 -0
  14. data/app/views/respect/rails/schemas/index.html.erb +4 -0
  15. data/config/routes.rb +4 -0
  16. data/lib/respect/rails/action_def.rb +37 -0
  17. data/lib/respect/rails/action_schema.rb +41 -0
  18. data/lib/respect/rails/application_info.rb +26 -0
  19. data/lib/respect/rails/controller_helper.rb +107 -0
  20. data/lib/respect/rails/engine.rb +63 -0
  21. data/lib/respect/rails/engine_info.rb +27 -0
  22. data/lib/respect/rails/headers_helper.rb +11 -0
  23. data/lib/respect/rails/headers_simplifier.rb +31 -0
  24. data/lib/respect/rails/info.rb +72 -0
  25. data/lib/respect/rails/request_def.rb +38 -0
  26. data/lib/respect/rails/request_helper.rb +101 -0
  27. data/lib/respect/rails/request_schema.rb +102 -0
  28. data/lib/respect/rails/response_def.rb +43 -0
  29. data/lib/respect/rails/response_helper.rb +67 -0
  30. data/lib/respect/rails/response_schema.rb +84 -0
  31. data/lib/respect/rails/response_schema_set.rb +60 -0
  32. data/lib/respect/rails/route_info.rb +101 -0
  33. data/lib/respect/rails/version.rb +5 -0
  34. data/lib/respect/rails.rb +74 -0
  35. data/lib/tasks/respect_tasks.rake +4 -0
  36. data/test/dummy/README.rdoc +261 -0
  37. data/test/dummy/Rakefile +7 -0
  38. data/test/dummy/app/assets/javascripts/application.js +15 -0
  39. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  40. data/test/dummy/app/controllers/application_controller.rb +3 -0
  41. data/test/dummy/app/controllers/automatic_validation_controller.rb +300 -0
  42. data/test/dummy/app/controllers/caught_exception_controller.rb +58 -0
  43. data/test/dummy/app/controllers/disabled_controller.rb +37 -0
  44. data/test/dummy/app/controllers/manual_validation_controller.rb +63 -0
  45. data/test/dummy/app/controllers/no_schema_controller.rb +17 -0
  46. data/test/dummy/app/controllers/skipped_automatic_validation_controller.rb +35 -0
  47. data/test/dummy/app/helpers/application_helper.rb +2 -0
  48. data/test/dummy/app/helpers/respect/application_macros.rb +10 -0
  49. data/test/dummy/app/helpers/respect/circle_schema.rb +16 -0
  50. data/test/dummy/app/helpers/respect/point_schema.rb +19 -0
  51. data/test/dummy/app/helpers/respect/rgba_schema.rb +18 -0
  52. data/test/dummy/app/views/automatic_validation/request_format.html.erb +1 -0
  53. data/test/dummy/app/views/automatic_validation/request_format.pdf.erb +1 -0
  54. data/test/dummy/app/views/caught_exception/response_validator.html.erb +1 -0
  55. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  56. data/test/dummy/config/application.rb +58 -0
  57. data/test/dummy/config/boot.rb +10 -0
  58. data/test/dummy/config/database.yml +25 -0
  59. data/test/dummy/config/environment.rb +5 -0
  60. data/test/dummy/config/environments/development.rb +37 -0
  61. data/test/dummy/config/environments/production.rb +67 -0
  62. data/test/dummy/config/environments/test.rb +37 -0
  63. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  64. data/test/dummy/config/initializers/inflections.rb +15 -0
  65. data/test/dummy/config/initializers/mime_types.rb +5 -0
  66. data/test/dummy/config/initializers/respect.rb +9 -0
  67. data/test/dummy/config/initializers/secret_token.rb +7 -0
  68. data/test/dummy/config/initializers/session_store.rb +8 -0
  69. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  70. data/test/dummy/config/locales/en.yml +5 -0
  71. data/test/dummy/config/routes.rb +38 -0
  72. data/test/dummy/config.ru +4 -0
  73. data/test/dummy/db/development.sqlite3 +0 -0
  74. data/test/dummy/db/schema.rb +16 -0
  75. data/test/dummy/db/test.sqlite3 +0 -0
  76. data/test/dummy/lib/exts/Rgba.rb +11 -0
  77. data/test/dummy/lib/exts/circle.rb +11 -0
  78. data/test/dummy/lib/exts/point.rb +11 -0
  79. data/test/dummy/log/development.log +6 -0
  80. data/test/dummy/log/test.log +851 -0
  81. data/test/dummy/public/404.html +26 -0
  82. data/test/dummy/public/422.html +26 -0
  83. data/test/dummy/public/500.html +25 -0
  84. data/test/dummy/public/favicon.ico +0 -0
  85. data/test/dummy/script/rails +6 -0
  86. data/test/functional/automatic_validation_controller_test.rb +131 -0
  87. data/test/functional/caught_exception_controller_test.rb +50 -0
  88. data/test/functional/disabled_controller_test.rb +16 -0
  89. data/test/functional/manual_validation_controller_test.rb +24 -0
  90. data/test/functional/no_schema_controller_test.rb +12 -0
  91. data/test/functional/respect/rails/schemas_controller_test.rb +18 -0
  92. data/test/functional/skipped_automatic_validation_controller_test.rb +12 -0
  93. data/test/headers_can_dumped_in_json.sh +33 -0
  94. data/test/integration/navigation_test.rb +38 -0
  95. data/test/request_headers_validation_in_dev_mode.sh +33 -0
  96. data/test/test_helper.rb +17 -0
  97. data/test/unit/action_schema_test.rb +21 -0
  98. data/test/unit/application_info_test.rb +11 -0
  99. data/test/unit/controller_helper_test.rb +4 -0
  100. data/test/unit/engine_info_test.rb +11 -0
  101. data/test/unit/engine_test.rb +7 -0
  102. data/test/unit/info_test.rb +42 -0
  103. data/test/unit/request_def_test.rb +22 -0
  104. data/test/unit/request_helper_test.rb +67 -0
  105. data/test/unit/request_schema_test.rb +164 -0
  106. data/test/unit/response_def_test.rb +9 -0
  107. data/test/unit/response_helper_test.rb +73 -0
  108. data/test/unit/response_schema_set_test.rb +18 -0
  109. data/test/unit/response_schema_test.rb +147 -0
  110. metadata +334 -0
@@ -0,0 +1,67 @@
1
+ require "test_helper"
2
+
3
+ class RequestHelperTest < Test::Unit::TestCase
4
+ def test_successful_request_validation
5
+ request = Object.new
6
+ request.extend(Respect::Rails::RequestHelper)
7
+ schema = mock()
8
+ schema.stubs(:validate?).with(request).returns(true).once
9
+ request.stubs(:request_schema).with().returns(schema)
10
+ Benchmark.stubs(:realtime).with().yields().returns(1.2345) # 1234.5 seconds.
11
+ ::Rails.logger.stubs(:info).with(" Request validation: success (1234.5ms)").once
12
+ assert_nothing_raised do
13
+ request.validate_schema
14
+ end
15
+ end
16
+
17
+ def test_failed_request_validation
18
+ request = Object.new
19
+ request.extend(Respect::Rails::RequestHelper)
20
+ schema = mock()
21
+ schema.stubs(:validate?).with(request).returns(false).once
22
+ error = RuntimeError.new("test error")
23
+ error.stubs(:context).with().returns(["foo", "bar"])
24
+ schema.stubs(:last_error).with().returns(error)
25
+ request.stubs(:request_schema).with().returns(schema)
26
+ Benchmark.stubs(:realtime).with().yields().returns(1.2345) # 1234.5 seconds.
27
+ ::Rails.logger.stubs(:info).with(" Request validation: failure (1234.5ms)").once
28
+ ::Rails.logger.stubs(:info).with(" foo").once
29
+ ::Rails.logger.stubs(:info).with(" bar").once
30
+ begin
31
+ request.validate_schema
32
+ assert false, "nothing raised"
33
+ rescue RuntimeError => e
34
+ assert_equal error, e
35
+ end
36
+ end
37
+
38
+ def test_none_request_validation
39
+ request = Object.new
40
+ request.extend(Respect::Rails::RequestHelper)
41
+ request.stubs(:params).with().never
42
+ schema = mock()
43
+ schema.stubs(:validate!).never
44
+ request.stubs(:request_schema).with().returns(nil)
45
+ Benchmark.stubs(:realtime).with().yields().returns(1.2345) # 1234.5 seconds.
46
+ ::Rails.logger.stubs(:info).with(" Request validation: none (1234.5ms)").once
47
+ assert_nothing_raised do
48
+ request.validate_schema
49
+ end
50
+ end
51
+
52
+ def test_request_validation_schema_query_on_success
53
+ request = Object.new
54
+ request.extend(Respect::Rails::RequestHelper)
55
+ request.stubs(:validate_schema).returns(true)
56
+ assert_equal(true, request.validate_schema?)
57
+ end
58
+
59
+ def test_request_validation_schema_query_on_failure
60
+ error = mock()
61
+ error.stubs(:context).returns([])
62
+ request = Object.new
63
+ request.extend(Respect::Rails::RequestHelper)
64
+ request.stubs(:validate_schema).raises(Respect::Rails::RequestValidationError.new(error, :path, {}))
65
+ assert_equal(false, request.validate_schema?)
66
+ end
67
+ end
@@ -0,0 +1,164 @@
1
+ require "test_helper"
2
+
3
+ class RequestSchemaTest < Test::Unit::TestCase
4
+ def setup
5
+ @rs = Respect::Rails::RequestSchema.new("ctrl", "action")
6
+ end
7
+
8
+ def test_default_path_parameters
9
+ assert_equal "ctrl", @rs.default_path_parameters["controller"].options[:equal_to]
10
+ assert_equal false, @rs.default_path_parameters["controller"].doc
11
+ assert_equal "action", @rs.default_path_parameters["action"].options[:equal_to]
12
+ assert_equal false, @rs.default_path_parameters["action"].doc
13
+ end
14
+
15
+ def test_path_parameters_are_merged_with_default
16
+ params = Respect::HashSchema.new
17
+ result = Respect::HashSchema.new
18
+ @rs.default_path_parameters.stubs(:merge).with(params).returns(result).once
19
+ assert_not_equal result.object_id, @rs.path_parameters.object_id
20
+ assert_equal result.object_id, @rs.public_send(:path_parameters=, params).object_id
21
+ assert_equal result.object_id, @rs.path_parameters.object_id
22
+ end
23
+
24
+ def test_validate_all_parameters
25
+ Respect::Rails::Engine.stubs(:disable_request_headers_validation).returns(false)
26
+ request = mock()
27
+ headers = {}
28
+ request.stubs(:headers).with().returns(headers).at_least_once
29
+ @rs.headers.stubs(:validate).with(headers).once
30
+ params = {}
31
+ request.stubs(:params).with().returns(params).at_least_once
32
+ @rs.path_parameters.stubs(:validate).with(params).once
33
+ @rs.path_parameters.stubs(:sanitized_object).returns(params).once
34
+ @rs.query_parameters.stubs(:validate).with(params).once
35
+ @rs.query_parameters.stubs(:sanitized_object).returns(params).once
36
+ @rs.body_parameters.stubs(:validate).with(params).once
37
+ @rs.body_parameters.stubs(:sanitized_object).returns(params).once
38
+ assert_equal true, @rs.validate(request)
39
+ end
40
+
41
+ def test_validate_raise_request_validation_error_on_path_params_validation_error
42
+ Respect::Rails::Engine.stubs(:disable_request_headers_validation).returns(false)
43
+ request = mock()
44
+ headers = {}
45
+ request.stubs(:headers).with().returns(headers).at_least_once
46
+ params = {}
47
+ request.stubs(:params).with().returns(params).at_least_once
48
+ @rs.path_parameters.stubs(:validate).with(params).raises(Respect::ValidationError.new("error message")).once
49
+ @rs.query_parameters.stubs(:validate).with(params)
50
+ @rs.body_parameters.stubs(:validate).with(params)
51
+ begin
52
+ @rs.validate(request)
53
+ assert false, "nothing raised"
54
+ rescue Respect::Rails::RequestValidationError => e
55
+ assert e.error.is_a?(Respect::ValidationError)
56
+ assert_equal "error message; in path", e.message
57
+ assert e.part.path?
58
+ assert_equal params, e.object
59
+ end
60
+ end
61
+
62
+ def test_validate_raise_request_validation_error_on_query_params_validation_error
63
+ Respect::Rails::Engine.stubs(:disable_request_headers_validation).returns(false)
64
+ request = mock()
65
+ headers = {}
66
+ request.stubs(:headers).with().returns(headers).at_least_once
67
+ params = {}
68
+ request.stubs(:params).with().returns(params).at_least_once
69
+ @rs.path_parameters.stubs(:validate).with(params)
70
+ @rs.query_parameters.stubs(:validate).with(params).raises(Respect::ValidationError.new("error message")).once
71
+ @rs.body_parameters.stubs(:validate).with(params)
72
+ begin
73
+ @rs.validate(request)
74
+ assert false, "nothing raised"
75
+ rescue Respect::Rails::RequestValidationError => e
76
+ assert e.error.is_a?(Respect::ValidationError)
77
+ assert_equal "error message; in query", e.message
78
+ assert e.part.query?
79
+ assert_equal params, e.object
80
+ end
81
+ end
82
+
83
+ def test_validate_raise_request_validation_error_on_body_params_validation_error
84
+ Respect::Rails::Engine.stubs(:disable_request_headers_validation).returns(false)
85
+ request = mock()
86
+ headers = {}
87
+ request.stubs(:headers).with().returns(headers).at_least_once
88
+ params = {}
89
+ request.stubs(:params).with().returns(params).at_least_once
90
+ @rs.path_parameters.stubs(:validate).with(params)
91
+ @rs.query_parameters.stubs(:validate).with(params)
92
+ @rs.body_parameters.stubs(:validate).with(params).raises(Respect::ValidationError.new("error message")).once
93
+ begin
94
+ @rs.validate(request)
95
+ assert false, "nothing raised"
96
+ rescue Respect::Rails::RequestValidationError => e
97
+ assert e.error.is_a?(Respect::ValidationError)
98
+ assert_equal "error message; in body", e.message
99
+ assert e.part.body?
100
+ assert_equal params, e.object
101
+ end
102
+ end
103
+
104
+ def test_validate_raise_request_validation_error_on_headers_validation_error
105
+ Respect::Rails::Engine.stubs(:disable_request_headers_validation).returns(false)
106
+ request = mock()
107
+ headers = {}
108
+ request.stubs(:headers).with().returns(headers).at_least_once
109
+ @rs.headers.stubs(:validate).with(headers).raises(Respect::ValidationError.new("error message")).once
110
+ simplified_headers = {}
111
+ @rs.stubs(:simplify_headers).with(headers).returns(simplified_headers).once
112
+ params = {}
113
+ request.stubs(:params).with().returns(params)
114
+ @rs.path_parameters.stubs(:validate).with(params)
115
+ @rs.query_parameters.stubs(:validate).with(params)
116
+ @rs.body_parameters.stubs(:validate).with(params)
117
+ begin
118
+ @rs.validate(request)
119
+ assert false, "nothing raised"
120
+ rescue Respect::Rails::RequestValidationError => e
121
+ assert e.error.is_a?(Respect::ValidationError)
122
+ assert_equal "error message; in headers", e.message
123
+ assert e.part.headers?
124
+ assert_equal simplified_headers.object_id, e.object.object_id
125
+ end
126
+ end
127
+
128
+ def test_validate_query_returns_true_when_no_error
129
+ request = mock()
130
+ @rs.stubs(:validate).with(request).returns(nil)
131
+ assert_equal true, @rs.validate?(request)
132
+ end
133
+
134
+ def test_validate_query_returns_false_on_error_and_store_last_error
135
+ doc = {}
136
+ error = Respect::Rails::RequestValidationError.new(Respect::ValidationError.new("message"), :path, {})
137
+ @rs.stubs(:validate).with(doc).raises(error).once
138
+ assert_equal false, @rs.validate?(doc)
139
+ assert_equal error, @rs.last_error
140
+ end
141
+
142
+ def test_validate_shebang_returns_true_on_success_and_sanitize
143
+ request = mock()
144
+ @rs.stubs(:validate?).with(request).returns(true).once
145
+ @rs.stubs(:sanitize!).with(request).once
146
+ assert_equal true, @rs.validate!(request)
147
+ end
148
+
149
+ def test_validate_shebang_returns_false_on_failure
150
+ request = mock()
151
+ @rs.stubs(:validate?).with(request).returns(false).once
152
+ assert_equal false, @rs.validate!(request)
153
+ end
154
+
155
+ def test_sanitize_all_params
156
+ request = mock()
157
+ params = mock()
158
+ request.stubs(:params).with().returns(params).at_least_once
159
+ @rs.path_parameters.stubs(:sanitize_object!).with(params).once
160
+ @rs.query_parameters.stubs(:sanitize_object!).with(params).once
161
+ @rs.body_parameters.stubs(:sanitize_object!).with(params).once
162
+ @rs.sanitize!(request)
163
+ end
164
+ end
@@ -0,0 +1,9 @@
1
+ require "test_helper"
2
+
3
+ class ResponseDefTest < ActiveSupport::TestCase
4
+ test "can use kernel method" do
5
+ Respect::Rails::ResponseDef.eval do
6
+ p
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,73 @@
1
+ require "test_helper"
2
+
3
+ class ResponseHelperTest < Test::Unit::TestCase
4
+ def test_successful_response_validation
5
+ response = Object.new
6
+ response.extend(Respect::Rails::ResponseHelper)
7
+ body = ""
8
+ schema = mock()
9
+ response.stubs(:schema).with().returns(schema)
10
+ response.stubs(:content_type).with().returns(Mime::JSON)
11
+ schema.stubs(:validate?).with(response).returns(true).once
12
+ Benchmark.stubs(:realtime).with().yields().returns(1.2345) # 1234.5 seconds.
13
+ Rails.logger.stubs(:info).with(" Response validation: success (1234.5ms)").once
14
+ assert_nothing_raised do
15
+ response.validate_schema
16
+ end
17
+ end
18
+
19
+ def test_failed_response_validation
20
+ response = Object.new
21
+ response.extend(Respect::Rails::ResponseHelper)
22
+ body = ""
23
+ schema = mock()
24
+ response.stubs(:schema).with().returns(schema)
25
+ response.stubs(:content_type).with().returns(Mime::JSON)
26
+ schema.stubs(:validate?).with(response).returns(false).once
27
+ error = RuntimeError.new("test error")
28
+ error.stubs(:context).with().returns(["foo", "bar"])
29
+ schema.stubs(:last_error).with().returns(error)
30
+ Benchmark.stubs(:realtime).with().yields().returns(1.2345) # 1234.5 seconds.
31
+ Rails.logger.stubs(:info).with(" Response validation: failure (1234.5ms)").once
32
+ Rails.logger.stubs(:info).with(" foo").once
33
+ Rails.logger.stubs(:info).with(" bar").once
34
+ begin
35
+ response.validate_schema
36
+ assert false, "nothing raised"
37
+ rescue RuntimeError => e
38
+ assert_equal error, e
39
+ end
40
+ end
41
+
42
+ def test_none_response_validation
43
+ response = Object.new
44
+ response.extend(Respect::Rails::ResponseHelper)
45
+ body = ""
46
+ schema = mock()
47
+ response.stubs(:schema).with().returns(nil)
48
+ response.stubs(:body).with().never
49
+ response.stubs(:content_type).with().never
50
+ schema.stubs(:validate?).never
51
+ Benchmark.stubs(:realtime).with().yields().returns(1.2345) # 1234.5 seconds.
52
+ ::Rails.logger.stubs(:info).with(" Response validation: none (1234.5ms)").once
53
+ assert_nothing_raised do
54
+ response.validate_schema
55
+ end
56
+ end
57
+
58
+ def test_response_validation_schema_query_on_success
59
+ response = Object.new
60
+ response.extend(Respect::Rails::ResponseHelper)
61
+ response.stubs(:validate_schema).returns(true)
62
+ assert_equal(true, response.validate_schema?)
63
+ end
64
+
65
+ def test_response_validation_schema_query_on_failure
66
+ error = mock()
67
+ error.stubs(:context).returns([])
68
+ response = Object.new
69
+ response.extend(Respect::Rails::ResponseHelper)
70
+ response.stubs(:validate_schema).raises(Respect::Rails::ResponseValidationError.new(error, :body, {}))
71
+ assert_equal(false, response.validate_schema?)
72
+ end
73
+ end
@@ -0,0 +1,18 @@
1
+ require "test_helper"
2
+
3
+ class ResponseSchemaSetTest < Test::Unit::TestCase
4
+ def test_respond_to_object_method
5
+ assert_respond_to Respect::Rails::ResponseSchemaSet.new("automatic_validation", "response_schema_from_file"), :class
6
+ end
7
+
8
+ def test_normalize_status
9
+ assert_equal :ok, Respect::Rails::ResponseSchemaSet.symbolize_status(:ok)
10
+ assert_equal :ok, Respect::Rails::ResponseSchemaSet.symbolize_status("ok")
11
+ assert_equal :ok, Respect::Rails::ResponseSchemaSet.symbolize_status(200)
12
+ assert_equal :ok, Respect::Rails::ResponseSchemaSet.symbolize_status("200")
13
+ assert_equal :ok, Respect::Rails::ResponseSchemaSet.symbolize_status(200.5)
14
+ assert_raises(ArgumentError) do
15
+ Respect::Rails::ResponseSchemaSet.symbolize_status(Object.new)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,147 @@
1
+ require "test_helper"
2
+
3
+ class ResponseSchemaTest < Test::Unit::TestCase
4
+ def setup
5
+ @rs = Respect::Rails::ResponseSchema.new
6
+ end
7
+
8
+ def test_define_with_block
9
+ s = Respect::HashSchema.define do |s|
10
+ s.integer "result"
11
+ end
12
+ block_def = Proc.new do |r|
13
+ r.body do |s|
14
+ s.integer "result"
15
+ end
16
+ end
17
+ rs = Respect::Rails::ResponseSchema.define(:ok, &block_def)
18
+ assert_equal :ok, rs.status
19
+ assert_equal s, rs.body
20
+
21
+ rs = Respect::Rails::ResponseSchema.define(&block_def)
22
+ assert_equal :ok, rs.status
23
+ assert_equal s, rs.body
24
+ end
25
+
26
+ def test_duplicata_are_equal
27
+ s = Respect::Rails::ResponseSchema.new(:ok)
28
+ assert_equal s, s.dup
29
+ end
30
+
31
+ def test_differs_from_status
32
+ s1 = Respect::Rails::ResponseSchema.new(:ok)
33
+ s2 = Respect::Rails::ResponseSchema.new(:created)
34
+ assert(s1 != s2)
35
+ end
36
+
37
+ def test_differs_from_body
38
+ s1 = Respect::Rails::ResponseSchema.new
39
+ s1.body = Respect::IntegerSchema.new
40
+ s2 = Respect::Rails::ResponseSchema.new
41
+ s2.body = Respect::StringSchema.new
42
+ assert(s1 != s2)
43
+ end
44
+
45
+ def test_define_from_file
46
+ # Prepare mock file name and contents
47
+ filename = "/path/to/definition_file.rb"
48
+ file_content = <<-EOS.strip_heredoc
49
+ body do |s|
50
+ s.integer "result"
51
+ end
52
+ EOS
53
+ File.stubs(:read).with(filename).returns(file_content)
54
+ # Compute the expected response schema.
55
+ expected_response_schema = Respect::Rails::ResponseSchema.new
56
+ expected_response_schema.body = Respect::HashSchema.define do |s|
57
+ s.integer "result"
58
+ end
59
+ # Do the test.
60
+ rs = Respect::Rails::ResponseSchema.from_file(:ok, filename)
61
+ assert_equal expected_response_schema, rs
62
+ end
63
+
64
+ def test_validate_raise_response_validation_error_on_validation_error
65
+ response = mock()
66
+ headers = mock()
67
+ response.stubs(:headers).returns(headers)
68
+ headers_schema = Respect::HashSchema.new
69
+ @rs.stubs(:headers).returns(headers_schema)
70
+ headers_schema.stubs(:validate).with(headers).once
71
+
72
+ body = mock()
73
+ response.stubs(:body).returns(body)
74
+ body_schema = Respect::HashSchema.new
75
+ @rs.stubs(:body).returns(body_schema)
76
+ decoded_body = mock()
77
+ ActiveSupport::JSON.stubs(:decode).with(body).returns(decoded_body).once
78
+ body_schema.stubs(:validate).with(decoded_body).raises(Respect::ValidationError.new("message"))
79
+ assert_raises(Respect::Rails::ResponseValidationError) do
80
+ @rs.validate(response)
81
+ end
82
+ end
83
+
84
+ def test_validate_returns_true_on_success
85
+ response = mock()
86
+ headers = mock()
87
+ response.stubs(:headers).returns(headers)
88
+ headers_schema = Respect::HashSchema.new
89
+ @rs.stubs(:headers).returns(headers_schema)
90
+ headers_schema.stubs(:validate).with(headers).once
91
+ body = mock()
92
+ response.stubs(:body).returns(body)
93
+ body_schema = Respect::HashSchema.new
94
+ @rs.stubs(:body).returns(body_schema)
95
+ decoded_body = mock()
96
+ ActiveSupport::JSON.stubs(:decode).with(body).returns(decoded_body).once
97
+ body_schema.stubs(:validate).with(decoded_body).once
98
+ assert_equal true, @rs.validate(response)
99
+ end
100
+
101
+ def test_validate_query_returns_true_when_no_error
102
+ doc = {}
103
+ @rs.stubs(:validate).with(doc).returns(nil)
104
+ assert_equal true, @rs.validate?(doc)
105
+ end
106
+
107
+ def test_validate_query_returns_false_on_error_and_store_last_error
108
+ doc = {}
109
+ error = Respect::Rails::ResponseValidationError.new(Respect::ValidationError.new("message"), :body, {})
110
+ @rs.stubs(:validate).with(doc).raises(error).once
111
+ assert_equal false, @rs.validate?(doc)
112
+ assert_equal error, @rs.last_error
113
+ end
114
+
115
+ def test_symbolize_http_status
116
+ assert_equal :ok, Respect::Rails::ResponseSchema.symbolize_http_status(200)
117
+ assert_equal :internal_server_error, Respect::Rails::ResponseSchema.symbolize_http_status(0)
118
+ assert_equal :gateway_timeout, Respect::Rails::ResponseSchema.symbolize_http_status(504)
119
+ assert_equal :http_version_not_supported, Respect::Rails::ResponseSchema.symbolize_http_status(505)
120
+ end
121
+
122
+ def test_validate_raise_response_validation_error_for_headers_error
123
+ response = mock()
124
+ headers = mock()
125
+ response.stubs(:headers).returns(headers)
126
+ headers_schema = Respect::HashSchema.new
127
+ @rs.stubs(:headers).returns(headers_schema)
128
+ headers_schema.stubs(:validate).with(headers).raises(Respect::ValidationError.new("message")).once
129
+ simplified_headers = {}
130
+ @rs.stubs(:simplify_headers).with(headers).returns(simplified_headers).once
131
+
132
+ body = mock()
133
+ response.stubs(:body).returns(body)
134
+ body_schema = Respect::HashSchema.new
135
+ @rs.stubs(:body).returns(body_schema)
136
+ decoded_body = mock()
137
+ ActiveSupport::JSON.stubs(:decode).with(body).returns(decoded_body)
138
+ body_schema.stubs(:validate).with(decoded_body)
139
+ begin
140
+ @rs.validate(response)
141
+ assert false
142
+ rescue Respect::Rails::ResponseValidationError => e
143
+ assert_equal "headers", e.part
144
+ assert_equal simplified_headers.object_id, e.object.object_id
145
+ end
146
+ end
147
+ end