def testCallSucceeds(self): self.ResetServer( wsgi_util.static_page(self.encoded_response, content_type='application/json')) rpc = self.connection.send_rpc(my_method.remote, self.request) self.assertEquals(self.response, rpc.response)
def testHandleUnparsableErrorContent(self): self.ResetServer(wsgi_util.static_page('oops', status=httplib.BAD_REQUEST, content_type='application/json')) rpc = self.connection.send_rpc(my_method.remote, self.request) try: rpc.response except remote.ServerError, err: self.assertEquals('HTTP Error 400: oops', str(err))
def testHandleEmptyBadRpcStatus(self): self.ResetServer(wsgi_util.static_page('{"error_message": "x"}', status=httplib.BAD_REQUEST, content_type='application/json')) rpc = self.connection.send_rpc(my_method.remote, self.request) try: rpc.response except remote.ServerError, err: self.assertEquals('HTTP Error 400: {"error_message": "x"}', str(err))
def testDefault(self): default_page = wsgi_util.static_page() self.ResetServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEquals(200, status) self.assertEquals('OK', reason) self.assertEquals('', content) self.assertEquals({'content-length': '0', 'content-type': 'text/html; charset=utf-8', }, headers)
def testHasStatus(self): default_page = wsgi_util.static_page(status='400 Not Good Request') self.StartServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEquals(400, status) self.assertEquals('Not Good Request', reason) self.assertEquals('', content) self.assertEquals({'content-length': '0', 'content-type': 'text/html; charset=utf-8', }, headers)
def testHttpSocketError(self): self.ResetServer(wsgi_util.static_page(self.encoded_response, content_type='application/json')) bad_transport = transport.HttpTransport('http://localhost:-1/blar') try: bad_transport.send_rpc(my_method.remote, self.request) except remote.NetworkError, err: self.assertTrue(str(err).startswith('Socket error: gaierror (')) self.assertEquals(socket.gaierror, type(err.cause)) self.assertEquals(8, abs(err.cause.args[0])) # Sign is sys depednent.
def testHasStatusInt(self): default_page = wsgi_util.static_page(status=401) self.StartServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEquals(401, status) self.assertEquals('Unauthorized', reason) self.assertEquals('', content) self.assertEquals({'content-length': '0', 'content-type': 'text/html; charset=utf-8', }, headers)
def testHasStatusUnknown(self): default_page = wsgi_util.static_page(status=909) self.StartServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEquals(909, status) self.assertEquals('Unknown Error', reason) self.assertEquals('', content) self.assertEquals({'content-length': '0', 'content-type': 'text/html; charset=utf-8', }, headers)
def testHasStatusTuple(self): default_page = wsgi_util.static_page(status=(500, 'Bad Thing')) self.StartServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEquals(500, status) self.assertEquals('Bad Thing', reason) self.assertEquals('', content) self.assertEquals({'content-length': '0', 'content-type': 'text/html; charset=utf-8', }, headers)
def testHasContent(self): default_page = wsgi_util.static_page('my content') self.StartServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEquals(200, status) self.assertEquals('OK', reason) self.assertEquals('my content', content) self.assertEquals({'content-length': str(len('my content')), 'content-type': 'text/html; charset=utf-8', }, headers)
def testHasContentType(self): default_page = wsgi_util.static_page(content_type='text/plain') self.StartServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEquals(200, status) self.assertEquals('OK', reason) self.assertEquals('', content) self.assertEquals({'content-length': '0', 'content-type': 'text/plain', }, headers)
def testHttpSocketError(self): self.ResetServer(wsgi_util.static_page(self.encoded_response, content_type="application/json")) bad_transport = transport.HttpTransport("http://localhost:-1/blar") try: bad_transport.send_rpc(my_method.remote, self.request) except remote.NetworkError as err: self.assertTrue(str(err).startswith("Socket error: error (")) self.assertEquals(errno.ECONNREFUSED, err.cause.errno) else: self.fail("Expected error")
def testHandleStatusContent(self): self.ResetServer(wsgi_util.static_page('{"state": "REQUEST_ERROR",' ' "error_message": "a request error"' '}', status=httplib.BAD_REQUEST, content_type='application/json')) rpc = self.connection.send_rpc(my_method.remote, self.request) try: rpc.response except remote.RequestError, err: self.assertEquals('a request error', str(err))
def testHasContentType(self): default_page = wsgi_util.static_page(content_type='text/plain') self.ResetServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEqual(200, status) self.assertEqual('OK', reason) self.assertEqual('', content) self.assertEqual( { 'content-length': '0', 'content-type': 'text/plain', }, headers)
def testDefault(self): default_page = wsgi_util.static_page() self.ResetServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEquals(200, status) self.assertEquals('OK', reason) self.assertEquals('', content) self.assertEquals( { 'content-length': '0', 'content-type': 'text/html; charset=utf-8', }, headers)
def testHasStatusInt(self): default_page = wsgi_util.static_page(status=401) self.ResetServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEqual(401, status) self.assertEqual('Unauthorized', reason) self.assertEqual('', content) self.assertEqual( { 'content-length': '0', 'content-type': 'text/html; charset=utf-8', }, headers)
def testHandleUnparsableErrorContent(self): self.ResetServer( wsgi_util.static_page("oops", status=six.moves.http_client.BAD_REQUEST, content_type="application/json") ) rpc = self.connection.send_rpc(my_method.remote, self.request) try: rpc.response except remote.ServerError as err: self.assertEquals("HTTP Error 400: oops", str(err)) else: self.fail("Expected ServerError")
def testHasStatusUnknown(self): default_page = wsgi_util.static_page(status=909) self.ResetServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEqual(909, status) self.assertEqual('Unknown Error', reason) self.assertEqual('', content) self.assertEqual( { 'content-length': '0', 'content-type': 'text/html; charset=utf-8', }, headers)
def testHasContent(self): default_page = wsgi_util.static_page('my content') self.ResetServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEqual(200, status) self.assertEqual('OK', reason) self.assertEqual('my content', content) self.assertEqual( { 'content-length': str(len('my content')), 'content-type': 'text/html; charset=utf-8', }, headers)
def testHasStatus(self): default_page = wsgi_util.static_page(status='400 Not Good Request') self.ResetServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEqual(400, status) self.assertEqual('Not Good Request', reason) self.assertEqual('', content) self.assertEqual( { 'content-length': '0', 'content-type': 'text/html; charset=utf-8', }, headers)
def testHasStatusTuple(self): default_page = wsgi_util.static_page(status=(500, 'Bad Thing')) self.ResetServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEqual(500, status) self.assertEqual('Bad Thing', reason) self.assertEqual('', content) self.assertEqual( { 'content-length': '0', 'content-type': 'text/html; charset=utf-8', }, headers)
def testHandleUnparsableErrorContent(self): self.ResetServer( wsgi_util.static_page('oops', status=six.moves.http_client.BAD_REQUEST, content_type='application/json')) rpc = self.connection.send_rpc(my_method.remote, self.request) try: rpc.response except remote.ServerError as err: self.assertEquals('HTTP Error 400: oops', str(err)) else: self.fail('Expected ServerError')
def testHandleApplicationError(self): self.ResetServer(wsgi_util.static_page('{"state": "APPLICATION_ERROR",' ' "error_message": "an app error",' ' "error_name": "MY_ERROR_NAME"}', status=httplib.BAD_REQUEST, content_type='application/json')) rpc = self.connection.send_rpc(my_method.remote, self.request) try: rpc.response except remote.ApplicationError, err: self.assertEquals('an app error', str(err)) self.assertEquals('MY_ERROR_NAME', err.error_name)
def testHeadersUnicodeSafe(self): default_page = wsgi_util.static_page(headers=[('x', u'foo')]) self.ResetServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEquals(200, status) self.assertEquals('OK', reason) self.assertEquals('', content) self.assertEquals({'content-length': '0', 'content-type': 'text/html; charset=utf-8', 'x': 'foo', }, headers) self.assertTrue(isinstance(headers['x'], str))
def testHttpSocketError(self): self.ResetServer( wsgi_util.static_page(self.encoded_response, content_type='application/json')) bad_transport = transport.HttpTransport('http://localhost:-1/blar') try: bad_transport.send_rpc(my_method.remote, self.request) except remote.NetworkError as err: self.assertTrue(str(err).startswith('Socket error: error (')) self.assertEquals(errno.ECONNREFUSED, err.cause.errno) else: self.fail('Expected error')
def testHandleEmptyBadRpcStatus(self): self.ResetServer( wsgi_util.static_page( '{"error_message": "x"}', status=six.moves.http_client.BAD_REQUEST, content_type="application/json" ) ) rpc = self.connection.send_rpc(my_method.remote, self.request) try: rpc.response except remote.ServerError as err: self.assertEquals('HTTP Error 400: {"error_message": "x"}', str(err)) else: self.fail("Expected ServerError")
def testHeadersUnicodeSafe(self): default_page = wsgi_util.static_page(headers=[('x', 'foo')]) self.ResetServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEqual(200, status) self.assertEqual('OK', reason) self.assertEqual('', content) self.assertEqual( { 'content-length': '0', 'content-type': 'text/html; charset=utf-8', 'x': 'foo', }, headers) self.assertTrue(isinstance(headers['x'], str))
def testHasHeadersDict(self): default_page = wsgi_util.static_page(headers={'x': 'foo', 'a': 'bar', 'z': 'bin'}) self.StartServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEquals(200, status) self.assertEquals('OK', reason) self.assertEquals('', content) self.assertEquals({'content-length': '0', 'content-type': 'text/html; charset=utf-8', 'x': 'foo', 'a': 'bar', 'z': 'bin', }, headers)
def testHandleStatusContent(self): self.ResetServer( wsgi_util.static_page( '{"state": "REQUEST_ERROR",' ' "error_message": "a request error"' "}", status=six.moves.http_client.BAD_REQUEST, content_type="application/json", ) ) rpc = self.connection.send_rpc(my_method.remote, self.request) try: rpc.response except remote.RequestError as err: self.assertEquals("a request error", str(err)) else: self.fail("Expected RequestError")
def testHttpRequestError(self): self.ResetServer(wsgi_util.static_page(self.encoded_response, content_type='application/json')) def request_error(*args, **kwargs): raise TypeError('Generic Error') original_request = httplib.HTTPConnection.request httplib.HTTPConnection.request = request_error try: try: self.connection.send_rpc(my_method.remote, self.request) except remote.NetworkError, err: self.assertEquals('Error communicating with HTTP server', str(err)) self.assertEquals(TypeError, type(err.cause)) self.assertEquals('Generic Error', str(err.cause)) else:
def testHandleStatusContent(self): self.ResetServer( wsgi_util.static_page( '{"state": "REQUEST_ERROR",' ' "error_message": "a request error"' '}', status=six.moves.http_client.BAD_REQUEST, content_type='application/json')) rpc = self.connection.send_rpc(my_method.remote, self.request) try: rpc.response except remote.RequestError as err: self.assertEquals('a request error', str(err)) else: self.fail('Expected RequestError')
def testHasHeaders(self): default_page = wsgi_util.static_page( headers=[('x', 'foo'), ('a', 'bar'), ('z', 'bin')]) self.ResetServer(default_page) status, reason, content, headers = self.DoHttpRequest() self.assertEqual(200, status) self.assertEqual('OK', reason) self.assertEqual('', content) self.assertEqual( { 'content-length': '0', 'content-type': 'text/html; charset=utf-8', 'x': 'foo', 'a': 'bar', 'z': 'bin', }, headers)
def testHandleApplicationError(self): self.ResetServer( wsgi_util.static_page( '{"state": "APPLICATION_ERROR",' ' "error_message": "an app error",' ' "error_name": "MY_ERROR_NAME"}', status=six.moves.http_client.BAD_REQUEST, content_type='application/json')) rpc = self.connection.send_rpc(my_method.remote, self.request) try: rpc.response except remote.ApplicationError as err: self.assertEquals('an app error', str(err)) self.assertEquals('MY_ERROR_NAME', err.error_name) else: self.fail('Expected RequestError')
def testHandleApplicationError(self): self.ResetServer( wsgi_util.static_page( '{"state": "APPLICATION_ERROR",' ' "error_message": "an app error",' ' "error_name": "MY_ERROR_NAME"}', status=six.moves.http_client.BAD_REQUEST, content_type="application/json", ) ) rpc = self.connection.send_rpc(my_method.remote, self.request) try: rpc.response except remote.ApplicationError as err: self.assertEquals("an app error", str(err)) self.assertEquals("MY_ERROR_NAME", err.error_name) else: self.fail("Expected RequestError")
def testHttps(self): self.schema = "https" self.ResetServer(wsgi_util.static_page(self.encoded_response, content_type="application/json")) # Create a fake https connection function that really just calls http. self.used_https = False def https_connection(*args, **kwargs): self.used_https = True return six.moves.http_client.HTTPConnection(*args, **kwargs) original_https_connection = six.moves.http_client.HTTPSConnection six.moves.http_client.HTTPSConnection = https_connection try: rpc = self.connection.send_rpc(my_method.remote, self.request) finally: six.moves.http_client.HTTPSConnection = original_https_connection self.assertEquals(self.response, rpc.response) self.assertTrue(self.used_https)
def testHttpRequestError(self): self.ResetServer(wsgi_util.static_page(self.encoded_response, content_type="application/json")) def request_error(*args, **kwargs): raise TypeError("Generic Error") original_request = six.moves.http_client.HTTPConnection.request six.moves.http_client.HTTPConnection.request = request_error try: try: self.connection.send_rpc(my_method.remote, self.request) except remote.NetworkError as err: self.assertEquals("Error communicating with HTTP server", str(err)) self.assertEquals(TypeError, type(err.cause)) self.assertEquals("Generic Error", str(err.cause)) else: self.fail("Expected error") finally: six.moves.http_client.HTTPConnection.request = original_request
def testHttps(self): self.schema = 'https' self.ResetServer( wsgi_util.static_page(self.encoded_response, content_type='application/json')) # Create a fake https connection function that really just calls http. self.used_https = False def https_connection(*args, **kwargs): self.used_https = True return six.moves.http_client.HTTPConnection(*args, **kwargs) original_https_connection = six.moves.http_client.HTTPSConnection six.moves.http_client.HTTPSConnection = https_connection try: rpc = self.connection.send_rpc(my_method.remote, self.request) finally: six.moves.http_client.HTTPSConnection = original_https_connection self.assertEquals(self.response, rpc.response) self.assertTrue(self.used_https)
def testHttpRequestError(self): self.ResetServer( wsgi_util.static_page(self.encoded_response, content_type='application/json')) def request_error(*args, **kwargs): raise TypeError('Generic Error') original_request = six.moves.http_client.HTTPConnection.request six.moves.http_client.HTTPConnection.request = request_error try: try: self.connection.send_rpc(my_method.remote, self.request) except remote.NetworkError as err: self.assertEquals('Error communicating with HTTP server', str(err)) self.assertEquals(TypeError, type(err.cause)) self.assertEquals('Generic Error', str(err.cause)) else: self.fail('Expected error') finally: six.moves.http_client.HTTPConnection.request = original_request
"""WSGI utility library tests.""" import six from six.moves import filter __author__ = '[email protected] (Rafe Kaplan)' import six.moves.http_client import unittest from protorpc import test_util from protorpc import util from protorpc import webapp_test_util from protorpc.wsgi import util as wsgi_util APP1 = wsgi_util.static_page('App1') APP2 = wsgi_util.static_page('App2') NOT_FOUND = wsgi_util.error(six.moves.http_client.NOT_FOUND) class WsgiTestBase(webapp_test_util.WebServerTestBase): server_thread = None def CreateWsgiApplication(self): return None def DoHttpRequest(self, path='/', content=None, content_type='text/plain; charset=utf-8',
def expect_content_type(environ, start_response): self.assertEquals(expected_content_type, environ['CONTENT_TYPE']) app = wsgi_util.static_page('', content_type=environ['CONTENT_TYPE']) return app(environ, start_response)
def testCallSucceeds(self): self.ResetServer(wsgi_util.static_page(self.encoded_response, content_type="application/json")) rpc = self.connection.send_rpc(my_method.remote, self.request) self.assertEquals(self.response, rpc.response)
def expect_content_type(environ, start_response): self.assertEquals(expected_content_type, environ["CONTENT_TYPE"]) app = wsgi_util.static_page("", content_type=environ["CONTENT_TYPE"]) return app(environ, start_response)
# """WSGI utility library tests.""" import six from six.moves import filter __author__ = '[email protected] (Rafe Kaplan)' import six.moves.http_client import unittest from protorpc import test_util from protorpc import util from protorpc import webapp_test_util from protorpc.wsgi import util as wsgi_util APP1 = wsgi_util.static_page('App1') APP2 = wsgi_util.static_page('App2') NOT_FOUND = wsgi_util.error(six.moves.http_client.NOT_FOUND) class WsgiTestBase(webapp_test_util.WebServerTestBase): server_thread = None def CreateWsgiApplication(self): return None def DoHttpRequest(self, path='/', content=None, content_type='text/plain; charset=utf-8',