def handle_api_static_request(self, request, start_response): """Handler for requests to _ah/api/static/.*. This calls start_response and returns the response body. Args: request: An ApiRequest, the request from the user. start_response: A function with semantics defined in PEP-333. Returns: A string containing the response body. """ discovery_api = discovery_api_proxy.DiscoveryApiProxy() response, body = discovery_api.get_static_file(request.relative_url) status_string = '%d %s' % (response.status, response.reason) if response.status == 200: # Some of the headers that come back from the server can't be passed # along in our response. Specifically, the response from the server has # transfer-encoding: chunked, which doesn't apply to the response that # we're forwarding. There may be other problematic headers, so we strip # off everything but Content-Type. return util.send_wsgi_response( status_string, [('Content-Type', response.getheader('Content-Type'))], body, start_response) else: logging.error( 'Discovery API proxy failed on %s with %d. Details: %s', request.relative_url, response.status, body) return util.send_wsgi_response(status_string, response.getheaders(), body, start_response)
def test_static_non_existing_file(self): relative_url = '/_ah/api/static/blah.html' # Set up mocks for the call to DiscoveryApiProxy.get_static_file. discovery_api = self.mox.CreateMock( discovery_api_proxy.DiscoveryApiProxy) self.mox.StubOutWithMock(discovery_api_proxy, 'DiscoveryApiProxy') discovery_api_proxy.DiscoveryApiProxy().AndReturn(discovery_api) static_response = self.mox.CreateMock(six.moves.http_client.HTTPResponse) static_response.status = 404 static_response.reason = 'Not Found' static_response.getheaders().AndReturn([('Content-Type', 'test/type')]) test_body = 'No Body' discovery_api.get_static_file(relative_url).AndReturn( (static_response, test_body)) # Make sure the dispatch works as expected. request = test_utils.build_request(relative_url) self.mox.ReplayAll() response = self.server.dispatch(request, self.start_response) self.mox.VerifyAll() response = ''.join(response) self.assert_http_match(response, '404 Not Found', [('Content-Length', '%d' % len(test_body)), ('Content-Type', 'test/type')], test_body)
def _common_setup(self): self.discovery_api = discovery_api_proxy.DiscoveryApiProxy() api_config_file = os.path.join(os.path.dirname(__file__), 'testdata/tictactoe-v1.api') with open(api_config_file, 'r') as api_file: api_config = api_file.read() self.api_config_dict = json.loads(api_config)
def __init__(self, config_manager): """Initializes an instance of the DiscoveryService. Args: config_manager: An instance of ApiConfigManager. """ self._config_manager = config_manager self._discovery_proxy = discovery_api_proxy.DiscoveryApiProxy()