def testDefaultFilterAlternateError(self): application = filters.static_page(content='it went awry', status=(500, 'oh no!')) self.StartWebServer(filters.filter_request(lambda environ: True, app=application)) self.CheckError('/', content='it went arwy', expected_code=500, expected_content='it went arwy')
def testMatch(self): self.StartWebServer(filters.match_environ( 'HTTP_X', r'exact', app=filters.static_page('it was ok'))) response = self.SendRequest('/', headers={'x': 'exact'}) self.CheckResponse(response, expected_content='it was ok') response = self.SendRequest('/', headers={'x': 'exact not so much'}) self.CheckResponse(response, expected_content='it was ok')
def testFilterEnvironError(self): bad = filters.static_page('it was bad', status=(402, 'Bad')) self.was_run = False def filter(value): self.was_run = True self.assertEquals(None, value) return False self.StartWebServer(filters.filter_environ('x', filter, on_error=bad)) self.CheckError('/', expected_content='it was bad', expected_code=402) self.assertTrue(self.was_run)
def testFilterEnviron(self): ok = filters.static_page('it was ok') self.was_run = False def filter(value): self.was_run = True self.assertEquals('set', value) return True self.StartWebServer(filters.filter_header('x', filter, app=ok)) response = self.SendRequest('/', headers={'x': 'set'}) self.CheckResponse(response, expected_content='it was ok') self.assertTrue(self.was_run)
def testFilterEnviron(self): ok = filters.static_page('it was ok') self.was_run = False def filter(value): self.was_run = True self.assertEquals('POST', value) return True self.StartWebServer(filters.filter_environ('REQUEST_METHOD', filter, app=ok)) response = self.SendRequest('/') self.CheckResponse(response, expected_content='it was ok') self.assertTrue(self.was_run)
def testNoMatch(self): self.StartWebServer(filters.match_environ( 'HTTP_X', r'None', app=filters.static_page('it was ok'))) self.CheckError('/') self.CheckError('/', headers={'x': 'not a match'})
def testErrorContent(self): self.StartWebServer(filters.static_page(status='400 Badness', content='kaboom')) self.CheckError('/', expected_content='kaboom')
def has_protocols(environ, start_response): content = str(environ[filters.PROTOCOLS_ENVIRON] is protocols) return filters.static_page(content)(environ, start_response)
def echo_header(environ, start_response): return filters.static_page(environ['HTTP_X'])(environ, start_response)
def echo_environ(environ, start_response): return filters.static_page(environ['x'])(environ, start_response)
def testDoesNotHaveError(self): not_ok = filters.static_page(status=(401, 'it was bad')) self.StartWebServer(filters.expect_header('x-whatever', on_error=not_ok)) self.CheckError('/', expected_code=401)
def testErrorStatusStringAlternateCode(self): self.StartWebServer(filters.static_page(status='500 Badness')) self.CheckError('/', expected_code=500)
def testHeadersList(self): self.StartWebServer(filters.static_page('hello world', headers=[('a', 'b'), ('c', 'd')])) self.CheckResponse(self.SendRequest('/'), expected_content='hello world', expected_headers={'a': 'b', 'c': 'd'})
def testContent(self): self.StartWebServer(filters.static_page('hello world')) self.CheckResponse(self.SendRequest('/'), expected_content='hello world')
def testErrorStatusString(self): self.StartWebServer(filters.static_page(status='400 Badness')) self.CheckError('/')
def testDefaultFilterAlternateApplication(self): application = filters.static_page(content='it was ok') self.StartWebServer(filters.filter_request(lambda environ: True, app=application)) response = self.SendRequest('/') self.CheckResponse(response, expected_content='it was ok')
def testErrorContentType(self): self.StartWebServer(filters.static_page(status='400 Badness', content_type='text/plain')) self.CheckError('/', expected_content_type='text/plain')
def application(environ, start_response): content = '%s %s' % (environ['PATH_INFO.0'], environ.get('HTTP_X.1') is None) return filters.static_page(content)(environ, start_response)
def testHasHeader(self): ok = filters.static_page('it was ok') self.StartWebServer(filters.expect_header('x-whatever', app=ok)) response = self.SendRequest('/', headers={'x-whatever': 'does not matter'}) self.CheckResponse(response, expected_content='it was ok')
def testStatusPair(self): self.StartWebServer(filters.static_page(status=(200, 'Okeedokee'))) self.CheckResponse(self.SendRequest('/'))
def testErrorStatusTuple(self): self.StartWebServer(filters.static_page(status=(402, 'Badness'))) self.CheckError('/', expected_code=402)
def testContentType(self): self.StartWebServer(filters.static_page('hello world', content_type='application/json')) self.CheckResponse(self.SendRequest('/'), expected_content='hello world', expected_content_type='application/json')
def protorpc_response(message, protocol, *args, **kwargs): encoded_message = protocol.encode_message(message) return filters.static_page(encoded_message, content_type=protocol.CONTENT_TYPE, *args, **kwargs)