@@ -21,8 +21,8 @@ def load_sample(self, sample_path):
2121 """
2222 Load a sample file into an AbeMock object.
2323 """
24- sample_file = os .path .join (self .samples_root , sample_path )
25- return AbeMock ( sample_file )
24+ sample_filename = os .path .join (self .samples_root , sample_path )
25+ return AbeMock . from_filename ( sample_filename )
2626
2727 def get_sample_request (self , path , label ):
2828 """
@@ -107,16 +107,39 @@ def assert_headers_contain(self, response_data, spec_data):
107107 "header {0}" .format (expected_header )
108108 )
109109
110- def assert_matches_sample (self , path , label , url , response ):
110+ def assert_matches_request (self , sample_request , wsgi_request ):
111+ """
112+ Check that the sample request and wsgi request match.
113+ """
114+ self .assertEqual (wsgi_request .META ['PATH_INFO' ], sample_request ['url' ])
115+ self .assertEqual (wsgi_request .META ['REQUEST_METHOD' ],
116+ sample_request ['method' ])
117+
118+ if 'headers' in sample_request :
119+ self .assert_headers_contain (
120+ wsgi_request .META , sample_request ['headers' ]
121+ )
122+
123+ if 'body' in sample_request :
124+ self .assert_data_equal (wsgi_request .POST , sample_request ['body' ])
125+
126+ def assert_matches_response (self , sample_response , wsgi_response ):
127+ """
128+ Check that the sample response and wsgi response match.
129+ """
130+ self .assertEqual (wsgi_response .status_code , sample_response .status )
131+ if 'body' in sample_response :
132+ response_parsed = wsgi_response .data
133+ self .assert_data_equal (response_parsed , sample_response .body )
134+
135+ def assert_matches_sample (self , path , label , response ):
111136 """
112137 Check a URL and response against a sample.
113138
114139 :param sample_name:
115140 The name of the sample file, e.g. 'query.json'
116141 :param label:
117142 The label for a specific sample request/response, e.g. 'OK'
118- :param url:
119- The actual URL we want to compare with the sample
120143 :param response:
121144 The actual API response we want to match with the sample.
122145 It is assumed to be a Django Rest Framework response object
@@ -125,13 +148,5 @@ def assert_matches_sample(self, path, label, url, response):
125148 sample_request = sample .examples [label ].request
126149 sample_response = sample .examples [label ].response
127150
128- self .assertEqual (url , sample_request .url )
129- if 'headers' in sample_request :
130- self .assert_headers_contain (
131- response .request , sample_request .headers
132- )
133-
134- self .assertEqual (response .status_code , sample_response .status )
135- if 'body' in sample_response :
136- response_parsed = response .data
137- self .assert_data_equal (response_parsed , sample_response .body )
151+ self .assert_matches_response (sample_response , response )
152+ self .assert_matches_request (sample_request , response .wsgi_request )
0 commit comments