def test_can_queue_stub_responses(self): client = AsyncHTTPStubClient() stub("/hello").and_return(body="hello")\ .and_return(body="beautiful")\ .and_return(body="world") # First Response client.fetch("/hello", self.stop) response = self.wait() self.assertEqual(response.code, 200) self.assertEqual(response.body, "hello") # Second Response client.fetch("/hello", self.stop) response = self.wait() self.assertEqual(response.code, 200) self.assertEqual(response.body, "beautiful") # Third Response client.fetch("/hello", self.stop) response = self.wait() self.assertEqual(response.code, 200) self.assertEqual(response.body, "world") # Fourth Response client.fetch("/hello", self.stop) response = self.wait() self.assertEqual(response.code, 200) self.assertEqual(response.body, "hello")
def test_auth_callback(self): self._clear_cookies() # clear session main = self.fetch("/") split = main.body.split(",", 1) self.assertEqual("login", split[0], "After logout state should be login") params = url_to_dict(split[1]) state = params["state"] code = "CODECODE" http_patch = patch("tornado.httpclient.AsyncHTTPClient", new=get_mock_http) http_patch.start() with stub( miracl_api_tornado.OAUTH_ACCESS_TOKEN_URL, method="POST").and_return(code=200, body_json={"access_token": "TOKEN"}): with stub( miracl_api_tornado.OAUTH_USERINFO_URL, method="GET").and_return(code=200, body_json={"sub": "MOCK", "email": "MOCK@MOCK"}): auth_data = self.fetch( "/c2id?state=" + state + "&code=" + code).body self.assertEqual("success", auth_data, "Login success") http_patch.stop() user_data = self.fetch("/") user_data = user_data.body.split(",") self.assertEqual("authenticated", user_data[0]) self.assertEqual("MOCK", user_data[1]) self.assertEqual("MOCK@MOCK", user_data[2])
def test_stub_and_fetch(self): client = AsyncHTTPStubClient() stub("/hello").and_return(body="world") client.fetch("/hello", self.stop) response = self.wait() self.assertEqual(response.code, 200) self.assertEqual(response.body, "world")
def test_init_add_and_return(self): st = stub("/hello") req = st.request st = st.and_return(body="foobar body") response = st.response_partial(req) self.assertEquals(response.code, 200) self.assertEquals(response.body, "foobar body") self.assertEquals(response.request.url, "/hello")
def test_init_add_and_return(self): st = stub("/hello") req = st.request st = st.and_return(body=b"foobar body") response = st.response_partial(req) self.assertEquals(response.code, 200) self.assertEquals(response.body, b"foobar body") self.assertEquals(response.request.url, "/hello")
def testmethod(*args, **kwargs): _stub, _ret = stub(url), return_json if not isinstance(_ret, list): _ret = [_ret] _stub = reduce( lambda a, e: a.and_return(body_json=e), _ret, _stub) with _stub: f(*args, **kwargs)
def test_with_syntax(self): client = AsyncHTTPStubClient() with stub("/hello").and_return(body=b"world"): client.fetch("/hello", self.stop) response = self.wait() self.assertEqual(response.code, 200) self.assertEqual(response.body, b"world") client.fetch("/hello", self.stop) response = self.wait() self.assertEqual(response.code, 404)
def test_init_stub(self): s = stub("/hello") self.assertTrue(isinstance(s, stub)) self.assertEqual(s.request.url, "/hello")
def test_set_response_code_in_stub(self): st = stub("/hello").and_return(code=418) resp_partial = RequestCollection.find(st.request) resp = resp_partial(st.request) self.assertEqual(resp.code, 418)
def test_init_stub_creates_blank_response_partial(self): st = stub("/hello") response = st.response_partial(st.request, 200) self.assertEquals(response.code, 200) self.assertEquals(response.body, None)
def test_no_return_args(self): st = stub("/hello").and_return() resp_partial = RequestCollection.find(st.request) resp = resp_partial(st.request) self.assertEqual(resp.body, b'')
def test_stub_with_method(self): st = stub("/hello", method="POST").and_return(body=b"anything") resp_partial = RequestCollection.find(st.request) self.assertNotEqual(resp_partial, None)
def test_stub_no_return_doesnt_add_to_collection(self): st = stub("/hello") self.assertNotEqual(st.request, None) resp_partial = RequestCollection.find(st.request) self.assertEqual(resp_partial, None)
def test_no_body(self): st = stub("/hello").and_return(body=None) resp_partial = RequestCollection.find(st.request) resp = resp_partial(st.request, 200) self.assertEqual(resp.body, '')
def test_stub_with_method(self): st = stub("/hello", method="POST").and_return(body="anything") resp_partial = RequestCollection.find(st.request) self.assertNotEqual(resp_partial, None)
def test_return_with_body_json(self): st = stub("/hello").and_return(body_json={'name': 'somebody'}) resp_partial = RequestCollection.find(st.request) resp = resp_partial(st.request) self.assertEqual( json.loads(resp.body.decode()).get('name'), 'somebody')
def test_return_with_body_json(self): st = stub("/hello").and_return(body_json={'name': 'somebody'}) resp_partial = RequestCollection.find(st.request) resp = resp_partial(st.request) self.assertEqual(json.loads(resp.body).get('name'), 'somebody')
def test_no_return_args(self): st = stub("/hello").and_return() resp_partial = RequestCollection.find(st.request) resp = resp_partial(st.request) self.assertEqual(resp.body, '')