예제 #1
0
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'
예제 #2
0
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'
예제 #3
0
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"'
예제 #4
0
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"'
예제 #5
0
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."}'
예제 #6
0
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>'
예제 #7
0
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'
예제 #8
0
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'
예제 #9
0
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'
예제 #10
0
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>'
예제 #11
0
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'
예제 #12
0
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'
예제 #13
0
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'
예제 #14
0
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'
예제 #15
0
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>'
예제 #16
0
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."}'
예제 #17
0
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>'
예제 #18
0
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 == ''
예제 #19
0
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'
예제 #20
0
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'
예제 #21
0
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'
예제 #22
0
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'
예제 #23
0
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'
예제 #24
0
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'
예제 #25
0
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'
예제 #26
0
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'
예제 #27
0
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'
예제 #28
0
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'
예제 #29
0
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'
예제 #30
0
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>'
예제 #31
0
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'
예제 #32
0
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'
예제 #33
0
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'
예제 #34
0
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'
예제 #35
0
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'
예제 #36
0
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'
예제 #37
0
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'
예제 #38
0
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'
예제 #39
0
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>'
예제 #40
0
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'
예제 #41
0
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'
예제 #42
0
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'
예제 #43
0
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'
예제 #44
0
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'
예제 #45
0
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'
예제 #46
0
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'
예제 #47
0
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
예제 #48
0
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'
예제 #49
0
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'
예제 #50
0
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'
예제 #51
0
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'
예제 #52
0
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"}'
예제 #53
0
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'
예제 #54
0
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
예제 #55
0
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'
예제 #56
0
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'
예제 #57
0
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'
예제 #58
0
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'
예제 #59
0
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'
예제 #60
0
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"']