def test_properties(self): path = '/foo/bar' path_args = ['foo', 'bar'] r = Request() r.path = path self.assertEqual(r.path, path) self.assertEqual(r.path_args, path_args) r = Request() r.path_args = path_args self.assertEqual(r.path, path) self.assertEqual(r.path_args, path_args) query = "foo=bar&che=baz&foo=che" query_kwargs = {'foo': ['bar', 'che'], 'che': 'baz'} r = Request() r.query = query self.assertEqual(parse.parse_qs(r.query, True), parse.parse_qs(query, True)) self.assertEqual(r.query_kwargs, query_kwargs) r = Request() r.query_kwargs = query_kwargs self.assertEqual(parse.parse_qs(r.query, True), parse.parse_qs(query, True)) self.assertEqual(r.query_kwargs, query_kwargs)
def test_properties(self): path = '/foo/bar' path_args = ['foo', 'bar'] r = Request() r.path = path self.assertEqual(r.path, path) self.assertEqual(r.path_args, path_args) r = Request() r.path_args = path_args self.assertEqual(r.path, path) self.assertEqual(r.path_args, path_args) query = "foo=bar&che=baz&foo=che" query_kwargs = {'foo': ['bar', 'che'], 'che': 'baz'} r = Request() r.query = query self.assertEqual(urlparse.parse_qs(r.query, True), urlparse.parse_qs(query, True)) self.assertEqual(r.query_kwargs, query_kwargs) r = Request() r.query_kwargs = query_kwargs self.assertEqual(urlparse.parse_qs(r.query, True), urlparse.parse_qs(query, True)) self.assertEqual(r.query_kwargs, query_kwargs)
def test_copy(self): r = Request() r.set_headers({ "Host": "localhost", }) r.query = "foo=bar" r.path = "/baz/che" r.environ['wsgi.url_scheme'] = "http" r.environ['SERVER_PORT'] = "80" r.foo = 1 r2 = r.copy() self.assertEqual(r.foo, r2.foo) self.assertEqual(r.environ["SERVER_PORT"], r2.environ["SERVER_PORT"])
def test_throttle(self): class TARA(object): @ratelimit_ip(limit=3, ttl=1) def foo(self): return 1 @ratelimit_ip(limit=10, ttl=1) def bar(self): return 2 r_foo = endpoints.Request() r_foo.set_header("X_FORWARDED_FOR", "276.0.0.1") r_foo.path = "/foo" c = TARA() c.request = r_foo for x in range(3): r = c.foo() self.assertEqual(1, r) for x in range(2): with self.assertRaises(CallError): c.foo() # make sure another path isn't messed with by foo r_bar = Request() r_bar.set_header("X_FORWARDED_FOR", "276.0.0.1") r_bar.path = "/bar" c.request = r_bar for x in range(10): r = c.bar() self.assertEqual(2, r) time.sleep(0.1) with self.assertRaises(CallError): c.bar() c.request = r_foo for x in range(3): r = c.foo() self.assertEqual(1, r) for x in range(2): with self.assertRaises(CallError): c.foo()
def test_throttle(self): class TARA(object): @ratelimit(limit=3, ttl=1) def foo(self): return 1 @ratelimit(limit=10, ttl=1) def bar(self): return 2 r_foo = endpoints.Request() r_foo.set_header("X_FORWARDED_FOR", "276.0.0.1") r_foo.path = "/foo" c = TARA() c.request = r_foo for x in range(3): r = c.foo() self.assertEqual(1, r) for x in range(2): with self.assertRaises(CallError): c.foo() # make sure another path isn't messed with by foo r_bar = Request() r_bar.set_header("X_FORWARDED_FOR", "276.0.0.1") r_bar.path = "/bar" c.request = r_bar for x in range(10): r = c.bar() self.assertEqual(2, r) time.sleep(0.1) with self.assertRaises(CallError): c.bar() c.request = r_foo for x in range(3): r = c.foo() self.assertEqual(1, r) for x in range(2): with self.assertRaises(CallError): c.foo()
def test_url(self): """make sure the .url attribute is correctly populated""" # this is wsgi configuration r = Request() r.set_headers({ "Host": "localhost", }) r.query = "foo=bar" r.path = "/baz/che" r.environ['wsgi.url_scheme'] = "http" r.environ['SERVER_PORT'] = "80" u = r.url self.assertEqual("http://localhost/baz/che?foo=bar", r.url) r.port = 555 u = r.url self.assertEqual("http://localhost:555/baz/che?foo=bar", r.url) # handle proxied connections r.host = "localhost:10000" r.port = "9000" u = r.url self.assertTrue(":10000" in u)
def get_http_instances(self, path="", method="GET"): req = Request() req.method = method req.path = path res = Response() return req, res
def create_request(ip): r = Request() r.path = "/fooparam" r.set_header("X_FORWARDED_FOR", ip) self.set_bearer_auth_header(r, "footoken") return r