def test_merged_response(self):
     start_response = start_response_utils.CapturingStartResponse()
     stream = start_response('200 OK', [('header1', 'value1')])
     stream.write('Hello World!')
     self.assertEqual(
         'Hello World! Goodbye World!',
         start_response.merged_response([' Goodbye ', 'World!']))
Пример #2
0
 def test_success(self):
   start_response = start_response_utils.CapturingStartResponse()
   stream = start_response('200 OK', [('header1', 'value1')])
   stream.write('Hello World!')
   self.assertEqual('200 OK', start_response.status)
   self.assertEqual(None, start_response.exc_info)
   self.assertEqual([('header1', 'value1')], start_response.response_headers)
   self.assertEqual('Hello World!', start_response.response_stream.getvalue())
Пример #3
0
  def add_request(self, method, relative_url, headers, body, source_ip,
                  module_name=None, version=None, instance_id=None,
                  fake_login=False):
    """Process an HTTP request.

    Args:
      method: A str containing the HTTP method of the request.
      relative_url: A str containing path and query string of the request.
      headers: A list of (key, value) tuples where key and value are both str.
      body: A str containing the request body.
      source_ip: The source ip address for the request.
      module_name: An optional str containing the module name to service this
          request. If unset, the request will be dispatched according to the
          host header and relative_url.
      version: An optional str containing the version to service this request.
          If unset, the request will be dispatched according to the host header
          and relative_url.
      instance_id: An optional str containing the instance_id of the instance to
          service this request. If unset, the request will be dispatched
          according to the host header and relative_url and, if applicable, the
          load-balancing for the module and version.
      fake_login: A bool indicating whether login checks should be bypassed,
          i.e. "login: required" should be ignored for this request.

    Returns:
      A request_info.ResponseTuple containing the response information for the
      HTTP request.
    """
    if module_name:
      _module = self._get_module_with_soft_routing(module_name, version)
      inst = _module.get_instance(instance_id) if instance_id else None
    else:
      headers_dict = wsgiref.headers.Headers(headers)
      _module, inst = self._resolve_target(
          headers_dict['Host'], urlparse.urlsplit(relative_url).path)
    if inst:
      try:
        port = _module.get_instance_port(inst.instance_id)
      except request_info.NotSupportedWithAutoScalingError:
        port = _module.balanced_port
    else:
      port = _module.balanced_port
    environ = _module.build_request_environ(method, relative_url, headers, body,
                                            source_ip, port,
                                            fake_login=fake_login)
    start_response = start_response_utils.CapturingStartResponse()
    response = self._handle_request(environ,
                                    start_response,
                                    _module,
                                    inst)

    # merged_response can have side effects which modify start_response.*, so
    # we cannot safely inline it into the ResponseTuple initialization below.
    merged = start_response.merged_response(response)
    return request_info.ResponseTuple(start_response.status,
                                      start_response.response_headers,
                                      merged)
    def test_exception(self):
        exc_info = (object(), object(), object())

        start_response = start_response_utils.CapturingStartResponse()
        start_response('200 OK', [('header1', 'value1')])
        start_response('500 Internal Server Error', [], exc_info)

        self.assertEqual('500 Internal Server Error', start_response.status)
        self.assertEqual(exc_info, start_response.exc_info)
        self.assertEqual([], start_response.response_headers)
Пример #5
0
    def _get_health_check_response(self, is_last_successful):
        """Sends the health check request and checks the result.

    Args:
      is_last_successful: Whether the last request was successful.

    Returns:
      A bool indicating whether or not the instance is healthy.
    """
        start_response = start_response_utils.CapturingStartResponse()
        try:
            response = self._send_request(start_response, is_last_successful)
        except request_info.Error, e:
            logging.warning(
                'Health check failure for instance %s. Exception: %s',
                self._instance.instance_id, e)
            return False
Пример #6
0
  def _get_health_check_response(self, is_last_successful):
    """Sends the health check request and checks the result.

    Args:
      is_last_successful: Whether the last request was successful.

    Returns:
      A bool indicating whether or not the instance is healthy.
    """
    start_response = start_response_utils.CapturingStartResponse()
    try:
      response = self._send_request(start_response, is_last_successful)
    except request_info.Error:
      logging.warning('Health check for instance {instance} is not '
                      'ready yet.'.format(instance=self._instance.instance_id))
      return False
    logging.debug('Health check response %s and status %s for instance %s.',
                  response, start_response.status, self._instance.instance_id)

    return response == ['ok'] and start_response.status == '200 OK'