def test_cmp(self): x = H.Request(body="foo") y = H.Request(body="bar") self.assertGreater(x, y) self.assertGreaterEqual(x, y) self.assertGreaterEqual(x, x) self.assertLess(y, x) self.assertLessEqual(y, x)
def test_requests_w_forced_bodies(self): r1 = "GET /foo HTTP/1.1\r\nContent-Length: 7\r\n\r\n<body1>" r2 = "GET /bar HTTP/1.1\r\nContent-Length: 7\r\n\r\n<body2>" self.assertEqual( list(H.force_bodies(H.requests(S.IBytesStream(r1 + r2)))), [ H.Request(method="GET", uri="/foo", headers={"Content-Length": "7"}, body="<body1>"), H.Request(method="GET", uri="/bar", headers={"Content-Length": "7"}, body="<body2>") ])
def test_request(self): x = H.Request(uri="/foo") y = H.request(x) z = H.request(dict(uri="/foo")) self.assertEqual(x, y) self.assertEqual(x, z) self.assertIs(x, y) self.assertIsNot(x, z)
def test_repr(self): x = H.Request(body="<body>") y = [("method", "GET"), ("uri", H.URI("/")), ("version", "HTTP/1.1"), ("headers", U.idict()), ("body", (b"<body>", )), ("env", {})] self.assertEqual( repr(x), "Request({})".format(", ".join("{} = {}".format(k, repr(v)) for (k, v) in y)))
def test_unparse(self): x = H.Request(method="POST", uri="/foo", headers=dict(Foo="bar", X="42"), body="<body>") self.assertRegexpMatches( U.STR(x.unparse()), "\\APOST /foo HTTP/1.1\r\n((Foo|X|Content-Length): " "(bar|42|6)\r\n)+\r\n<body>\\Z")
def test_init_empty(self): x = H.Request() self.assertEqual( dict(x.iteritems()), # also tests iteritems dict(method="GET", uri="/", version="HTTP/1.1", headers=U.idict(), body=(b"", ), env={}))
def test_unparse_chunked(self): x = H.Request(method="POST", uri="/foo", headers=dict(Foo="bar", X="42"), body=(x for x in ["<bo", "dy>"])) self.assertRegexpMatches( U.STR(x.unparse()), "\\APOST /foo HTTP/1.1\r\n((Foo|X|Transfer-Encoding): " "(bar|42|chunked)\r\n)+" "\r\n3\r\n<bo\r\n3\r\ndy>\r\n0\r\n\r\n\\Z")
def test_init_kwds(self): x = H.Request(method="POST", uri="/foo", headers=dict(Foo="bar"), body="<body>") self.assertEqual( dict(x.iteritems()), dict(method="POST", uri="/foo", version="HTTP/1.1", headers=U.idict(Foo="bar"), body=(b"<body>", ), env={}))
def test_the_rest_post(self): req = HTTP.Request(uri = "/some/where", method = "POST") resp = X()(req) self.assertEqual(resp, HTTP.Response(status = 404, body = "some/where"))
def test_the_rest(self): req = HTTP.Request(uri = "/something/else") resp = X()(req) self.assertEqual(resp, HTTP.Response(status = 404, body = "something/else"))
def test_get_foo(self): req = HTTP.Request(uri = "/foo/42") resp = X()(req) self.assertEqual(resp, HTTP.Response(body = "got foo w/ id 42"))
def test_init_data_no_mapping(self): with self.assertRaisesRegexp(TypeError, "must be a mapping"): H.Request("hi!")
def test_init_headers(self): x = H.Request(headers=dict(x=37, y=42)) self.assertIsInstance(x.headers, U.idict) self.assertEqual(x.headers, U.idict(x=37, Y=42))
def test_init_kwds_and_data_no_mapping(self): with self.assertRaisesRegexp(TypeError, "must be a mapping"): H.Request("<body>", method="POST")
def test_get_foo(self): req = HTTP.Request(uri = "/bar/37") resp = Y()(req) self.assertEqual(resp, HTTP.Response(body = "got bar w/ id 37"))
def test_init_body_str(self): x = H.Request(body="<body>") self.assertEqual(x.body, (b"<body>", ))
def test_eq(self): x = H.Request(body="foo") y = H.Request(body="bar") self.assertEqual(x, x) self.assertNotEqual(x, y)
def test_force_body(self): x = H.Request(body=(x for x in "foo bar baz".split())) self.assertIsInstance(x.body, type(x for x in [])) self.assertEqual(x.force_body, b"foobarbaz") self.assertIsInstance(x.body, tuple) self.assertEqual(x.body, (b"foobarbaz", ))
def test_init_body_IStream(self): x = H.Request(body=S.IBytesStream("<body>")) self.assertIsInstance(x.body, type(x for x in [])) self.assertEqual(b"".join(x.body), b"<body>")
def test_oops(self): req = HTTP.Request(uri = "/nothing/to/see") resp = Y()(req) self.assertEqual(resp, HTTP.Response(status = 404))
def test_init_kwds_unknown(self): with self.assertRaisesRegexp(TypeError, "unknown keys"): H.Request(foo=42)