def test_source_ip(self): body = '{}' path = '/_ah/api/guestbook/v1/greetings' env = {'SERVER_PORT': 42, 'REQUEST_METHOD': 'GET', 'SERVER_NAME': 'localhost', 'HTTP_CONTENT_TYPE': 'application/json', 'PATH_INFO': path, 'wsgi.input': io.StringIO(body)} request = api_request.ApiRequest(env) self.assertEqual(request.source_ip, None) env['REMOTE_ADDR'] = '1.2.3.4' request = api_request.ApiRequest(env) self.assertEqual(request.source_ip, '1.2.3.4')
def build_request(path, body='', http_headers=None): """Build an ApiRequest for the given path and body. Args: path: A string containing the URL for the proposed request. body: A string containing the body of the proposed request. http_headers: A list of (header, value) headers to add to the request. Returns: An ApiRequest object built based on the incoming parameters. """ (unused_scheme, unused_netloc, path, query, unused_fragment) = six.moves.urllib.parse.urlsplit(path) env = {'SERVER_PORT': 42, 'REQUEST_METHOD': 'GET', 'SERVER_NAME': 'localhost', 'HTTP_CONTENT_TYPE': 'application/json', 'PATH_INFO': path, 'wsgi.input': io.StringIO(body)} if query: env['QUERY_STRING'] = query if http_headers: for header, value in http_headers: header = 'HTTP_%s' % header.upper().replace('-', '_') env[header] = value cgi_request = api_request.ApiRequest(env) return cgi_request
def __call__(self, environ, start_response): """Handle an incoming request. Args: environ: An environ dict for the request as defined in PEP-333. start_response: A function used to begin the response to the caller. This follows the semantics defined in PEP-333. In particular, it's called with (status, response_headers, exc_info=None), and it returns an object with a write(body_data) function that can be used to write the body of the response. Yields: An iterable over strings containing the body of the HTTP response. """ request = api_request.ApiRequest(environ) # PEP-333 requires that we return an iterator that iterates over the # response body. Yielding the returned body accomplishes this. yield self.dispatch(request, start_response)