示例#1
0
    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))
示例#4
0
 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))
示例#14
0
 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)
示例#15
0
 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)
示例#16
0
 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")
示例#18
0
 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)
示例#19
0
 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)
示例#20
0
 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)
示例#21
0
 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)
示例#22
0
    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)
示例#24
0
 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))
示例#25
0
    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")
示例#27
0
 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:
示例#31
0
    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')
示例#32
0
 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)
示例#33
0
    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
示例#37
0
    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)
示例#38
0
    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
示例#39
0
"""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',
示例#40
0
 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)
示例#43
0
#
"""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',