@@ -10,19 +10,38 @@ module Schema
1010 # get :index
1111 # assert_response_schema
1212 def assert_response_schema ( schema_path = nil , message = nil )
13- matcher = AssertResponseSchema . new ( schema_path , response , message )
13+ matcher = AssertResponseSchema . new ( schema_path , request , response , message )
14+ assert ( matcher . call , matcher . message )
15+ end
16+
17+ def assert_request_schema ( schema_path = nil , message = nil )
18+ matcher = AssertRequestSchema . new ( schema_path , request , response , message )
19+ assert ( matcher . call , matcher . message )
20+ end
21+
22+ # May be renamed
23+ def assert_request_response_schema ( schema_path = nil , message = nil )
24+ assert_request_schema ( schema_path , message )
25+ assert_response_schema ( schema_path , message )
26+ end
27+
28+ def assert_schema ( payload , schema_path = nil , message = nil )
29+ matcher = AssertSchema . new ( schema_path , request , response , message , payload )
1430 assert ( matcher . call , matcher . message )
1531 end
1632
1733 MissingSchema = Class . new ( Minitest ::Assertion )
1834 InvalidSchemaError = Class . new ( Minitest ::Assertion )
1935
20- class AssertResponseSchema
21- attr_reader :schema_path , :response , :message
36+ class AssertSchema
37+ attr_reader :schema_path , :request , : response, :message , :payload
2238
23- def initialize ( schema_path , response , message )
39+ # Interface may change.
40+ def initialize ( schema_path , request , response , message , payload = nil )
2441 require_json_schema!
42+ @request = request
2543 @response = response
44+ @payload = payload
2645 @schema_path = schema_path || schema_path_default
2746 @message = message
2847 @document_store = JsonSchema ::DocumentStore . new
@@ -41,11 +60,11 @@ def call
4160 attr_reader :document_store
4261
4362 def controller_path
44- response . request . filtered_parameters [ :controller ]
63+ request . filtered_parameters [ :controller ]
4564 end
4665
4766 def action
48- response . request . filtered_parameters [ :action ]
67+ request . filtered_parameters [ :action ]
4968 end
5069
5170 def schema_directory
@@ -68,6 +87,10 @@ def response_body
6887 load_json ( response . body )
6988 end
7089
90+ def request_params
91+ request . env [ 'action_dispatch.request.request_parameters' ]
92+ end
93+
7194 def json_schema
7295 @json_schema ||= JsonSchema . parse! ( schema_data )
7396 end
@@ -98,6 +121,18 @@ def require_json_schema!
98121 raise LoadError , "You don't have json_schema installed in your application. Please add it to your Gemfile and run bundle install"
99122 end
100123 end
124+ class AssertResponseSchema < AssertSchema
125+ def initialize ( *)
126+ super
127+ @payload = response_body
128+ end
129+ end
130+ class AssertRequestSchema < AssertSchema
131+ def initialize ( *)
132+ super
133+ @payload = request_params
134+ end
135+ end
101136 end
102137 end
103138end
0 commit comments