def test_Cookie(self): http = self.client() # First set the cookies r = make_async(http.get(self.httpbin('cookies', 'set', 'bla', 'foo'))) yield r r = r.result self.assertEqual(r.status_code, 200) self.assertTrue(r.history) self.assertTrue(r.history[0].headers['set-cookie']) # Now check if I get them r = make_async(http.get(self.httpbin('cookies'))) yield r r = r.result self.assertEqual(r.status_code, 200) result = r.content_json() self.assertTrue(result['cookies']) self.assertEqual(result['cookies']['bla'],'foo') # Try without saving cookies http = self.client(store_cookies=False) r = make_async(http.get(self.httpbin('cookies', 'set', 'bla', 'foo'))) yield r r = r.result self.assertEqual(r.status_code, 200) self.assertTrue(r.history) self.assertTrue(r.history[0].headers['set-cookie']) r = make_async(http.get(self.httpbin('cookies'))) yield r r = r.result self.assertEqual(r.status_code, 200) result = r.content_json() self.assertFalse(result['cookies'])
def test_digest_authentication(self): http = self.client() r = make_async(http.get(self.httpbin('digest-auth/auth/bla/foo'))) yield r r = r.result self.assertEqual(r.status_code, 401) http.add_digest_authentication('bla', 'foo') r = make_async(http.get(self.httpbin('digest-auth/auth/bla/foo'))) yield r r = r.result self.assertEqual(r.status_code, 200)
def testDeferredErrorbackInGenerator(self): d = Deferred() ioloop = IOLoop() # Add a callback which returns a deferred rd = Cbk() d.add_callback(lambda r : rd.add(r)) d.add_callback(lambda r : r + ('second',)) def _gen(): yield d a = make_async(_gen()) result = d.callback('ciao') self.assertTrue(d.called) self.assertEqual(d.paused, 1) # The generator has added its consume callback self.assertEqual(len(d._callbacks), 2) self.assertEqual(len(rd._callbacks), 1) # self.assertEqual(rd.r, ('ciao',)) self.assertFalse(a.called) # # set Error back rd.set_error() self.assertFalse(d.paused) self.assertTrue(a.called) self.assertTrue(is_failure(d.result))
def test_expect(self): http = self.client() data = (('bla', 'foo'), ('unz', 'whatz'), ('numero', '1'), ('numero', '2')) bdata = urlencode(data) r = make_async(http.post(self.httpbin('post'), data=b'', headers=[('expect','100-continue'), ('Content-length', str(len(bdata)))])) yield r response = r.result self.assertEqual(response.status_code, 100) r = make_async(response.write(bdata)) yield r response = r.result self.assertEqual(response.status_code, 200) self.assertEqual(response.connection, None)
def testRedirect(self): http = self.client() r = make_async(http.get(self.httpbin('redirect', '1'))) yield r r = r.result self.assertEqual(r.status_code, 200) history = r.request.history self.assertEqual(len(history), 1) self.assertTrue(history[0].url.endswith('/redirect/1'))
def testResponseHeaders(self): http = self.client() r = make_async(http.get(self.httpbin('response-headers'))) yield r r = r.result self.assertEqual(r.status_code, 200) result = r.content_json() self.assertEqual(result['Transfer-Encoding'], 'chunked') parser = r.parser self.assertTrue(parser.is_chunked())
def test_404_get(self): '''Not Found 404''' http = self.client() r = make_async(http.get(self.httpbin('status', '404'))) yield r r = r.result self.assertEqual(r.status_code, 404) self.assertEqual(r.response, 'Not Found') self.assertTrue(r.content) self.assertRaises(httpurl.HTTPError, r.raise_for_status)
def test_400_get(self): '''Bad request 400''' http = self.client() r = make_async(http.get(self.httpbin('status', '400'))) yield r r = r.result self.assertEqual(r.status_code, 400) self.assertEqual(r.response, 'Bad Request') self.assertTrue(r.content) self.assertRaises(httpurl.HTTPError, r.raise_for_status)
def test_200_gzip(self): http = self.client() r = make_async(http.get(self.httpbin('gzip'))) yield r r = r.result headers = r.headers self.assertEqual(r.status_code, 200) self.assertEqual(r.response, 'OK') content = r.content_json() self.assertTrue(content['gzipped']) self.assertTrue(r.headers['content-encoding'],'gzip')
def test_200_get_data(self): http = self.client() r = make_async(http.get(self.httpbin('get',''), data={'bla':'foo'})) yield r r = r.result self.assertEqual(r.status_code, 200) self.assertEqual(r.response, 'OK') result = r.content_json() self.assertEqual(result['args'], {'bla':['foo']}) self.assertEqual(r.url, self.httpbin(httpurl.iri_to_uri('get/',{'bla':'foo'})))
def test_200_get(self): http = self.client() r = make_async(http.get(self.httpbin())) yield r r = r.result self.assertEqual(str(r), '200 OK') self.assertEqual(repr(r), 'HttpResponse(200 OK)') self.assertEqual(r.client, http) self.assertEqual(r.status_code, 200) self.assertEqual(r.response, 'OK') self.assertTrue(r.content) self.assertEqual(r.url, self.httpbin())
def on_connect(self, request): "Initialize the connection, authenticate and select a database" client = request.client.client cmnd = None if self.password: cmnd = self.request(client, 'AUTH', self.password, release_connection=False).execute()\ .add_errback(lambda r: self.connection_error(r, 'Invalid Password')) if self.db: cmnd = make_async(cmnd) cmnd.add_callback( lambda r: self.request(client, 'SELECT', self.db, release_connection=False).execute(), lambda r: self.connection_error(r, 'Invalid Database')) return cmnd
def testDeferredCallbackInGenerator(self): d = Deferred() ioloop = IOLoop() # Add a callback which returns a deferred rd = Cbk() d.add_callback(lambda r : rd.add(r)) d.add_callback(lambda r : r + ('second',)) def _gen(): yield d a = make_async(_gen()) result = d.callback('ciao') self.assertTrue(d.called) self.assertEqual(d.paused,1) self.assertEqual(len(d._callbacks), 2) self.assertEqual(len(rd._callbacks), 1) # self.assertEqual(rd.r, ('ciao',)) self.assertFalse(a.called) # # set callback rd.set_result('luca') self.assertTrue(a.called) self.assertFalse(d.paused) self.assertEqual(d.result,('ciao','luca','second'))
def test_stream_response(self): http = self.client() r = make_async(http.get(self.httpbin('stream/3000/20'))) yield r r = r.result self.assertEqual(r.status_code, 200)
def testLargeResponse(self): http = self.client() r = make_async(http.get(self.httpbin('getsize/600000'))) yield r r = r.result self.assertEqual(r.status_code, 200)
def request(r): return make_async(r).addBoth(request_callback)