def test_passing_in_strange_data_is_rejected(self): """ StubTreq rejects data that isn't list/dictionary/tuple/bytes/unicode. """ stub = StubTreq(_StaticTestResource()) self.assertRaises(AssertionError, stub.request, "method", "http://url", data=object()) self.successResultOf(stub.request("method", "http://url", data={})) self.successResultOf(stub.request("method", "http://url", data=[])) self.successResultOf(stub.request("method", "http://url", data=())) self.successResultOf(stub.request("method", "http://url", data=binary_type(b""))) self.successResultOf(stub.request("method", "http://url", data=text_type("")))
def test_passing_in_strange_data_is_rejected(self): """ StubTreq rejects data that isn't list/dictionary/tuple/bytes/unicode. """ stub = StubTreq(_StaticTestResource()) self.assertRaises( AssertionError, stub.request, 'method', 'http://url', data=object()) self.successResultOf(stub.request('method', 'http://url', data={})) self.successResultOf(stub.request('method', 'http://url', data=[])) self.successResultOf(stub.request('method', 'http://url', data=())) self.successResultOf( stub.request('method', 'http://url', data=binary_type(""))) self.successResultOf( stub.request('method', 'http://url', data=text_type("")))
def test_providing_resource_to_stub_treq(self): """ The resource provided to StubTreq responds to every request no matter what the URI or parameters or data. """ verbs = ("GET", "PUT", "HEAD", "PATCH", "DELETE", "POST") urls = ( "http://supports-http.com", "https://supports-https.com", "http://this/has/a/path/and/invalid/domain/name", "https://supports-https.com:8080", "http://supports-http.com:8080", ) params = (None, {}, {b"page": [1]}) headers = (None, {}, {b"x-random-header": [b"value", b"value2"]}) data = (None, b"", b"some data", b'{"some": "json"}') stub = StubTreq(_StaticTestResource()) combos = ( (verb, {"url": url, "params": p, "headers": h, "data": d}) for verb in verbs for url in urls for p in params for h in headers for d in data ) for combo in combos: verb, kwargs = combo deferreds = (stub.request(verb, **kwargs), getattr(stub, verb.lower())(**kwargs)) for d in deferreds: resp = self.successResultOf(d) self.assertEqual(418, resp.code) self.assertEqual([b"teapot!"], resp.headers.getRawHeaders(b"x-teapot")) self.assertEqual(b"" if verb == "HEAD" else b"I'm a teapot", self.successResultOf(stub.content(resp)))
def test_handles_successful_asynchronous_requests_with_streaming(self): """ Handle a resource returning NOT_DONE_YET and then streaming data back gradually over time. """ rsrc = _EventuallyResponsiveTestResource() stub = StubTreq(rsrc) d = stub.request("method", "http://example.com/", data="1234") self.assertNoResult(d) chunks = [] rsrc.stored_request.write(b"spam ") rsrc.stored_request.write(b"eggs") stub.flush() resp = self.successResultOf(d) d = stub.collect(resp, chunks.append) self.assertNoResult(d) self.assertEqual(b"".join(chunks), b"spam eggs") del chunks[:] rsrc.stored_request.write(b"eggs\r\nspam\r\n") stub.flush() self.assertNoResult(d) self.assertEqual(b"".join(chunks), b"eggs\r\nspam\r\n") rsrc.stored_request.finish() stub.flush() self.successResultOf(d)
def test_handles_successful_asynchronous_requests_with_streaming(self): """ Handle a resource returning NOT_DONE_YET and then streaming data back gradually over time. """ rsrc = _EventuallyResponsiveTestResource() stub = StubTreq(rsrc) d = stub.request('method', 'http://example.com/', data="1234") self.assertNoResult(d) chunks = [] rsrc.stored_request.write(b'spam ') rsrc.stored_request.write(b'eggs') stub.flush() resp = self.successResultOf(d) d = stub.collect(resp, chunks.append) self.assertNoResult(d) self.assertEqual(b''.join(chunks), b'spam eggs') del chunks[:] rsrc.stored_request.write(b'eggs\r\nspam\r\n') stub.flush() self.assertNoResult(d) self.assertEqual(b''.join(chunks), b'eggs\r\nspam\r\n') rsrc.stored_request.finish() stub.flush() self.successResultOf(d)
def test_handles_asynchronous_requests(self): """ Handle a resource returning NOT_DONE_YET. """ stub = StubTreq(_NonResponsiveTestResource()) d = stub.request('method', 'http://url', data="1234") self.assertNoResult(d) d.cancel() self.failureResultOf(d, ResponseFailed)
def test_handles_failing_asynchronous_requests(self): """ Handle a resource returning NOT_DONE_YET and then canceling the request. """ stub = StubTreq(_NonResponsiveTestResource()) d = stub.request("method", "http://url", data=b"1234") self.assertNoResult(d) d.cancel() self.failureResultOf(d, ResponseFailed)
def test_handles_successful_asynchronous_requests(self): """ Handle a resource returning NOT_DONE_YET and then later finishing the response. """ rsrc = _EventuallyResponsiveTestResource() stub = StubTreq(rsrc) d = stub.request("method", "http://example.com/", data=b"1234") self.assertNoResult(d) rsrc.stored_request.finish() stub.flush() resp = self.successResultOf(d) self.assertEqual(resp.code, 200)
def test_handles_successful_asynchronous_requests(self): """ Handle a resource returning NOT_DONE_YET and then later finishing the response. """ rsrc = _EventuallyResponsiveTestResource() stub = StubTreq(rsrc) d = stub.request('method', 'http://example.com/', data=b"1234") self.assertNoResult(d) rsrc.stored_request.finish() stub.flush() resp = self.successResultOf(d) self.assertEqual(resp.code, 200)
def test_handles_successful_asynchronous_requests_with_response_data(self): """ Handle a resource returning NOT_DONE_YET and then sending some data in the response. """ rsrc = _EventuallyResponsiveTestResource() stub = StubTreq(rsrc) d = stub.request('method', 'http://example.com/', data="1234") self.assertNoResult(d) chunks = [] rsrc.stored_request.write('spam ') rsrc.stored_request.write('eggs') stub.flush() resp = self.successResultOf(d) d = stub.collect(resp, chunks.append) self.assertNoResult(d) self.assertEqual(''.join(chunks), 'spam eggs') rsrc.stored_request.finish() stub.flush() self.successResultOf(d)
def test_providing_resource_to_stub_treq(self): """ The resource provided to StubTreq responds to every request no matter what the URI or parameters or data. """ verbs = ('GET', 'PUT', 'HEAD', 'PATCH', 'DELETE', 'POST') urls = ( 'http://supports-http.com', 'https://supports-https.com', 'http://this/has/a/path/and/invalid/domain/name' 'https://supports-https.com:8080', 'http://supports-http.com:8080', ) params = (None, {}, {'page': [1]}) headers = (None, {}, {'x-random-header': ['value', 'value2']}) data = (None, "", 'some data', '{"some": "json"}') stub = StubTreq(_StaticTestResource()) combos = ( (verb, {"url": url, "params": p, "headers": h, "data": d}) for verb in verbs for url in urls for p in params for h in headers for d in data ) for combo in combos: verb, kwargs = combo deferreds = (stub.request(verb, **kwargs), getattr(stub, verb.lower())(**kwargs)) for d in deferreds: resp = self.successResultOf(d) self.assertEqual(418, resp.code) self.assertEqual(['teapot!'], resp.headers.getRawHeaders('x-teapot')) self.assertEqual("" if verb == "HEAD" else "I'm a teapot", self.successResultOf(stub.content(resp)))