def test_modified_generate(): """Resource generates a good Last-Modified response header.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res4?id=myid') res = app._handle_request(req) print res._headers assert res._headers['Last-Modified'] == 'Fri, 01 May 2009 14:30:00 GMT'
def test_etag_generate_json_ext(): """ETags are calculated by adding the extension to the custom etag.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res4.json?id=myid') res = app._handle_request(req) print res assert res._headers['ETag'] == '"myid_json"'
def test_exception_json(): """An exception is serialized as a dictionary in JSON.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res5?throw=1', {'HTTP_ACCEPT': 'application/json'}) res = app._handle_request(req) print res assert res.status == '500 Internal Server Error' assert res.body == '{"error": "Some random exception."}'
def test_res6_default(): """Resource6 works normally for keys which exist on the resource.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res6/uid') res = app._handle_request(req) print res assert res.status == '200 OK' assert res.body == '<response>works</response>'
def test_etag_if_match_not_set(): """A GET request without an If-Match header passes.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res4?id=myid') res = app._handle_request(req) print res assert res._headers['ETag'] == '"myid_xml"' assert res.status == '200 OK'
def test_etag_if_match_true(): """A GET request with a matching If-Match passes.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res4?id=myid', {'HTTP_IF_MATCH': '"myid_xml"'}) res = app._handle_request(req) print res assert res._headers['ETag'] == '"myid_xml"' assert res.status == '200 OK'
def test_etag_if_match_false(): """A GET request with a non-matching If-Match returns 412.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res4?id=myid', {'HTTP_IF_MATCH': '"otherid"'}) res = app._handle_request(req) print res assert res._headers['ETag'] == '"myid_xml"' assert res.status == '412 Precondition Failed'
def test_notfound_xml(): """Requests that cause a NOT_FOUND exception return 404.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res6/foo') res = app._handle_request(req) print res assert res.status == '404 Not Found' assert res.body == '<response><error>Not Found</error></response>'
def test_with_expires_calculations(): """expires decorator correctly sets the Expires header.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res4') res = app._handle_request(req) print res._headers assert res._headers['Cache-Control'] == 'max-age=138' assert res._headers['Expires'] == 'Mon, 20 Apr 2009 17:55:45 GMT'
def test_etag_if_none_match_false(): """A GET request with a non-matching If-None-Match executes normally.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res4?id=myid', {'HTTP_IF_NONE_MATCH': '"otherid"'}) res = app._handle_request(req) print res assert res._headers['ETag'] == '"myid_xml"' assert res.status == '200 OK'
def test_with_expires(): """expires decorator correctly sets the Cache-Control header.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res3') res = app._handle_request(req) print str(res) print res._headers assert res._headers['Cache-Control'] == 'max-age=86400'
def test_app_handle_404(): """Application returns a 404 status code if no resource is found.""" app = wsgiservice.get_app(globals()) req = Request.blank('/foo') res = app._handle_request(req) print res assert res.status == '404 Not Found' assert res.body == ''
def test_app_handle_options(): """Resource provides a good default for the OPTIONS method.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res2', {'REQUEST_METHOD': 'OPTIONS'}) res = app._handle_request(req) print res assert res.status == '200 OK' assert res._headers['Allow'] == 'OPTIONS, POST, PUT'
def test_app_handle_response_201_ext(): """raise_201 ignores extension in the current path.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res2.json', {'REQUEST_METHOD': 'PUT'}) res = app._handle_request(req) print res assert res.status == '201 Created' assert res.body == '' assert res.location == '/res2/foo'
def test_app_handle_response_201_rel(): """raise_201 adds relative location header to the current request path.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res2', {'REQUEST_METHOD': 'PUT'}) res = app._handle_request(req) print res assert res.status == '201 Created' assert res.body == '' assert res.location == '/res2/foo'
def test_app_handle_response_201_abs(): """raise_201 used the location header directly if it is absolute.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res2', {'REQUEST_METHOD': 'POST'}) res = app._handle_request(req) print res assert res.status == '201 Created' assert res.body == '' assert res.location == '/res2/test'
def test_app_handle_method_not_known(): """Application returns 501 for unknown and unimplemented methods.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res2', {'REQUEST_METHOD': 'PATCH'}) res = app._handle_request(req) print res assert res.status == '501 Not Implemented' assert res.body == '' assert res._headers['Allow'] == 'OPTIONS, POST, PUT'
def test_with_expires_vary(): """expires decorator can set the Vary header.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res6/uid') res = app._handle_request(req) print str(res) print res._headers assert res._headers['Cache-Control'] == 'max-age=86400' assert res._headers['Vary'] == 'Authorization, Accept'
def test_app_handle_method_not_allowed(): """Application returns 405 for known but unimplemented methods.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res2', {'REQUEST_METHOD': 'GET'}) res = app._handle_request(req) print res assert res.status == '405 Method Not Allowed' assert res.body == '' assert res._headers['Allow'] == 'OPTIONS, POST, PUT'
def test_exception_xml(): """An exception is serialized as an error response in XML.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res5?throw=1') res = app._handle_request(req) print res assert res.status == '500 Internal Server Error' assert res.body == '<response><error>Some random exception.' \ '</error></response>'
def test_with_expires_calculations_double_wrapped(): """Wrapped expires decorators work by just using the last one.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res4', {'REQUEST_METHOD': 'POST'}) res = app._handle_request(req) print str(res) print res._headers assert res._headers['Cache-Control'] == 'max-age=138' assert res._headers['Expires'] == 'Mon, 20 Apr 2009 17:55:45 GMT'
def test_post_xml_object_fail(): """Create object with XML request body not supported""" app = wsgiservice.get_app(globals()) req = Request.blank('/humans', {'REQUEST_METHOD': 'POST', 'HTTP_ACCEPT': 'application/xml'}) req.content_type = 'application/xml' req.body = "<name>patrice</name>" res = app._handle_request(req) print res assert res.status == '400 Bad Request'
def test_if_modified_since_true(): """A GET request with an outdated If-Modified-Since passes.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res4?id=myid', {'HTTP_IF_MODIFIED_SINCE': 'Fri, 01 May 2009 14:18:10 GMT'}) res = app._handle_request(req) print res assert res._headers['Last-Modified'] == 'Fri, 01 May 2009 14:30:00 GMT' assert res.status == '200 OK'
def test_etag_if_none_match_post_true(): """A POST request with a matching If-None-Match returns 412.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res4?id=myid', {'HTTP_IF_NONE_MATCH': '"myid_xml"', 'REQUEST_METHOD': 'POST'}) res = app._handle_request(req) print res assert res._headers['ETag'] == '"myid_xml"' assert res.status == '412 Precondition Failed'
def test_verify_content_md5_valid(): """A request with a body that matches Content-MD5 passes.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res1/theid', { 'HTTP_CONTENT_MD5': '89d5739baabbbe65be35cbe61c88e06d', 'wsgi.input': StringIO.StringIO('Foobar')}) res = app._handle_request(req) print res assert res.status == '200 OK'
def test_etag_if_none_match_head_true(): """A HEAD request with a matching If-None-Match returns 304.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res4?id=myid', {'HTTP_IF_NONE_MATCH': '"myid_xml"', 'REQUEST_METHOD': 'HEAD'}) res = app._handle_request(req) print res assert res.body == '' assert res._headers['ETag'] == '"myid_xml"' assert res.status == '304 Not Modified'
def test_etag_if_none_match_get_true(): """A GET request with a matching If-None-Match returns 304.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res4?id=myid', {'HTTP_IF_NONE_MATCH': '"myid_xml"'}) res = app._handle_request(req) print res assert res.body == '' assert res._headers['ETag'] == '"myid_xml"' assert res.status == '304 Not Modified' assert 'Content-Type' not in res.headers
def test_app_handle_404(): """Application returns a 404 status code if no resource is found.""" app = wsgiservice.get_app(globals()) req = Request.blank('/foo', {'HTTP_ACCEPT': 'text/xml'}) res = app._handle_request(req) print res assert res.status == '404 Not Found' assert res.body == '<response><error>' \ 'The requested resource does not exist.</error></response>' assert res.headers['Content-Type'] == 'text/xml; charset=UTF-8'
def test_verify_content_md5_valid(): """A request with a body that matches Content-MD5 passes.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res1/theid', { 'HTTP_CONTENT_MD5': '89d5739baabbbe65be35cbe61c88e06d', }) req.body_file = StringIO.StringIO('Foobar') res = app._handle_request(req) print res assert res.status == '200 OK'
def test_if_unmodified_since_false(): """A GET request with an outdated If-Unmodified-Since returns 412.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res4?id=myid', {'HTTP_IF_UNMODIFIED_SINCE': 'Fri, 01 May 2009 12:30:00 GMT'}) res = app._handle_request(req) print res assert res._headers['ETag'] == '"myid_xml"' assert res._headers['Last-Modified'] == 'Fri, 01 May 2009 14:30:00 GMT' assert res.status == '412 Precondition Failed'
def test_if_modified_since_true(): """A GET request with an outdated If-Modified-Since passes.""" app = wsgiservice.get_app(globals()) req = Request.blank( '/res4?id=myid', {'HTTP_IF_MODIFIED_SINCE': 'Fri, 01 May 2009 14:18:10 GMT'}) res = app._handle_request(req) print res assert res._headers['Last-Modified'] == 'Fri, 01 May 2009 14:30:00 GMT' assert res.status == '200 OK'
def test_post_json_object(): """Create object with JSON request body""" app = wsgiservice.get_app(globals()) req = Request.blank('/humans', {'REQUEST_METHOD': 'POST', 'HTTP_ACCEPT': 'application/json'}) req.content_type = 'application/json' req.body = json.dumps({'name': 'flavio'}) res = app._handle_request(req) print res assert res.status == '200 OK' assert res.body == '{"id": "theid", "name": "flavio"}'
def test_if_unmodified_since_false(): """A GET request with an outdated If-Unmodified-Since returns 412.""" app = wsgiservice.get_app(globals()) req = Request.blank( '/res4?id=myid', {'HTTP_IF_UNMODIFIED_SINCE': 'Fri, 01 May 2009 12:30:00 GMT'}) res = app._handle_request(req) print res assert res._headers['ETag'] == '"myid_xml"' assert res._headers['Last-Modified'] == 'Fri, 01 May 2009 14:30:00 GMT' assert res.status == '412 Precondition Failed'
def test_etag_if_none_match_post_true(): """A POST request with a matching If-None-Match returns 412.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res4?id=myid', { 'HTTP_IF_NONE_MATCH': '"myid_xml"', 'REQUEST_METHOD': 'POST' }) res = app._handle_request(req) print res assert res._headers['ETag'] == '"myid_xml"' assert res.status == '412 Precondition Failed'
def test_if_modified_since_false(): """A GET request with a matching If-Modified-Since returns 304.""" app = wsgiservice.get_app(globals()) req = Request.blank( '/res4?id=myid', {'HTTP_IF_MODIFIED_SINCE': 'Fri, 01 May 2009 14:30:00 GMT'}) res = app._handle_request(req) print res assert res.body == '' assert res._headers['Last-Modified'] == 'Fri, 01 May 2009 14:30:00 GMT' assert res._headers['ETag'] == '"myid_xml"' assert res.status == '304 Not Modified'
def test_etag_if_none_match_head_true(): """A HEAD request with a matching If-None-Match returns 304.""" app = wsgiservice.get_app(globals()) req = Request.blank('/res4?id=myid', { 'HTTP_IF_NONE_MATCH': '"myid_xml"', 'REQUEST_METHOD': 'HEAD' }) res = app._handle_request(req) print res assert res.body == '' assert res._headers['ETag'] == '"myid_xml"' assert res.status == '304 Not Modified'
def test_if_unmodified_since_true(): """A GET request with a current If-Unmodified-Since returns 200.""" app = wsgiservice.get_app(globals()) req = Request.blank( '/res4?id=myid', { 'HTTP_IF_UNMODIFIED_SINCE': 'Fri, 01 May 2009 14:30:00 GMT', 'REQUEST_METHOD': 'POST' }) res = app._handle_request(req) print res assert res._headers['ETag'] == '"myid_xml"' assert res._headers['Last-Modified'] == 'Fri, 01 May 2009 14:30:00 GMT' assert res.status == '200 OK'
def test_app_wsgi(): """Application instance works as a WSGI application.""" app = wsgiservice.get_app(globals()) env = Request.blank('/res1/theid.json').environ start_response = mox.MockAnything() start_response('200 OK', [('Content-Length', '40'), ('Content-Type', 'application/json; charset=UTF-8'), ('Content-MD5', 'd6fe631718727b542d2ecb70dfd41e4b')]) mox.Replay(start_response) res = app(env, start_response) print res mox.Verify(start_response) assert res == ['"GET was called with id theid, foo None"']