def test_dispatch_value_error(self):
    """Tests that ValueError raised by dispatch stub is handled properly."""
    error = ValueError('Invalid Token', httplib.BAD_REQUEST)
    stub_dispatcher.dispatch(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(),
                             mox.IgnoreArg()).AndRaise(error)

    self.run_request('GET', {}, '/_ah/some_bucket', '', '',
                     '400 Bad Request', [], 'Invalid Token')
  def expect_dispatch(self, method, headers, path, body, result):
    """Setup a mox expectation to gcs_dispatch.dispatch."""

    # webob always adds Host header and optionally adds Content-Length header
    # for requests with non-empty body.
    new_headers = dict(headers)
    new_headers['Host'] = self._host
    if body:
      new_headers['Content-Length'] = str(len(body))

    url = 'http://%s%s' % (self._host, path)
    stub_dispatcher.dispatch(method, new_headers, url, body).AndReturn(result)
Пример #3
0
  def __call__(self, environ, start_response):
    """Handles WSGI requests.

    Args:
      environ: An environ dict for the current request as defined in PEP-333.
      start_response: A function with semantics defined in PEP-333.

    Returns:
      An iterable over strings containing the body of the HTTP response.
    """
    request = webob.Request(environ)

    try:
      result = stub_dispatcher.dispatch(request.method, request.headers,
                                        request.url, request.body)
    except ValueError as e:
      status_message = httplib.responses.get(e.args[1], '')
      start_response('%d %s' % (e.args[1], status_message), [])
      return [e.args[0]]

    # The metadata headers must be convereted from unicode to string.
    headers = []
    for k, v in result.headers.iteritems():
      headers.append((str(k), str(v)))

    # GCS uses non-standard HTTP 308 status code.
    status_code = result.status_code
    if status_code == 308:
      status_message = HTTP_308_STATUS_MESSAGE
    else:
      status_message = httplib.responses.get(status_code, '')

    start_response('%d %s' % (status_code, status_message), headers)

    return [result.content]
Пример #4
0
  def __call__(self, environ, start_response):
    """Handles WSGI requests.

    Args:
      environ: An environ dict for the current request as defined in PEP-333.
      start_response: A function with semantics defined in PEP-333.

    Returns:
      An iterable over strings containing the body of the HTTP response.
    """
    request = webob.Request(environ)

    try:
      result = stub_dispatcher.dispatch(request.method, request.headers,
                                        request.url, request.body)
    except ValueError as e:
      status_message = httplib.responses.get(e.args[1], '')
      start_response('%d %s' % (e.args[1], status_message), [])
      return [e.args[0]]

    # The metadata headers must be convereted from unicode to string.
    headers = []
    for k, v in result.headers.iteritems():
      headers.append((str(k), str(v)))

    # GCS uses non-standard HTTP 308 status code.
    status_code = result.status_code
    if status_code == 308:
      status_message = HTTP_308_STATUS_MESSAGE
    else:
      status_message = httplib.responses.get(status_code, '')

    start_response('%d %s' % (status_code, status_message), headers)

    return [result.content]
Пример #5
0
    def __call__(self, environ, start_response):
        request = webob.Request(environ)

        try:
            result = stub_dispatcher.dispatch(request.method, request.headers,
                                              request.url, request.body)
        except ValueError as e:
            status_message = httplib.responses.get(e.args[1], '')
            start_response('%d %s' % (e.args[1], status_message), [])
            return [e.args[0]]

        # The metadata headers must be convereted from unicode to string.
        headers = []
        for k, v in result.headers.iteritems():
            headers.append((str(k), str(v)))

        # GCS uses non-standard HTTP 308 status code.
        status_code = result.status_code
        if status_code == 308:
            status_message = HTTP_308_STATUS_MESSAGE
        else:
            status_message = httplib.responses.get(status_code, '')

        start_response('%d %s' % (status_code, status_message), headers)

        return [result.content]
Пример #6
0
  def __call__(self, environ, start_response):
    request = webob.Request(environ)

    try:
      result = stub_dispatcher.dispatch(request.method, request.headers,
                                        request.url, request.body)
    except ValueError as e:
      status_message = httplib.responses.get(e.args[1], '')
      start_response('%d %s' % (e.args[1], status_message), [])
      return [e.args[0]]

    # The metadata headers must be convereted from unicode to string.
    headers = []
    for k, v in result.headers.iteritems():
      headers.append((str(k), str(v)))

    # GCS uses non-standard HTTP 308 status code.
    status_code = result.status_code
    if status_code == 308:
      status_message = HTTP_308_STATUS_MESSAGE
    else:
      status_message = httplib.responses.get(status_code, '')

    start_response('%d %s' % (status_code, status_message), headers)

    return [result.content]
Пример #7
0
def urlfetch_to_gcs_stub(url, payload, method, headers, request, response,
                         follow_redirects=False, deadline=None,
                         validate_certificate=None):

  """Forwards Google Cloud Storage `urlfetch` requests to gcs_dispatcher."""
  headers_map = dict(
      (header.key().lower(), header.value()) for header in headers)
  result = gcs_dispatcher.dispatch(method, headers_map, url, payload)
  response.set_statuscode(result.status_code)
  response.set_content(result.content[:urlfetch_stub.MAX_RESPONSE_SIZE])
  for k, v in result.headers.iteritems():
    if k.lower() == 'content-length' and method != 'HEAD':
      v = len(response.content())
    header_proto = response.add_header()
    header_proto.set_key(k)
    header_proto.set_value(str(v))
  if len(result.content) > urlfetch_stub.MAX_RESPONSE_SIZE:
    response.set_contentwastruncated(True)
Пример #8
0
def urlfetch_to_gcs_stub(url, payload, method, headers, request, response,
                         follow_redirects=False, deadline=None,
                         validate_certificate=None):

  """Forwards gcs urlfetch requests to gcs_dispatcher."""
  headers_map = dict(
      (header.key().lower(), header.value()) for header in headers)
  result = gcs_dispatcher.dispatch(method, headers_map, url, payload)
  response.set_statuscode(result.status_code)
  response.set_content(result.content[:urlfetch_stub.MAX_RESPONSE_SIZE])
  for k, v in result.headers.iteritems():
    if k.lower() == 'content-length' and method != 'HEAD':
      v = len(response.content())
    header_proto = response.add_header()
    header_proto.set_key(k)
    header_proto.set_value(str(v))
  if len(result.content) > urlfetch_stub.MAX_RESPONSE_SIZE:
    response.set_contentwastruncated(True)