Exemple #1
0
    def test_webapp_body(self):

        # python requests module has windows bug?!?!?
        self.thisHostMustNot(platform='windows')

        class Haha:
            def bar(self, hehe, body=None):
                return (hehe,body.decode('utf8'))

        haha = Haha()

        wapp = s_webapp.WebApp()
        wapp.listen(0, host='127.0.0.1')
        wapp.addApiPath('/v1/haha/bar/([a-z]+)', haha.bar)

        client = AsyncHTTPClient(self.io_loop)
        port = wapp.getServBinds()[0][1]

        headers={'Content-Type': 'application/octet-stream'}

        resp = yield client.fetch('http://127.0.0.1:%d/v1/haha/bar/visi' % port, headers=headers, body='GRONK', allow_nonstandard_methods=True)
        resp = json.loads(resp.body.decode('utf-8'))
        self.assertEqual( tuple(resp.get('ret')), ('visi','GRONK') )

        resp = yield client.fetch('http://127.0.0.1:%d/v1/haha/bar/visi' % port, method='POST', headers=headers, body='GRONK')
        resp = json.loads(resp.body.decode('utf-8'))
        self.assertEqual( tuple(resp.get('ret')), ('visi','GRONK') )

        wapp.fini()
Exemple #2
0
    def test_webapp_publish(self):

        # tornado does not support windows (yet)
        self.thisHostMustNot(platform='windows')
        foo = Foo()

        wapp = s_webapp.WebApp()
        wapp.listen(0, host='127.0.0.1')
        wapp.addApiPath('/v1/horked', foo.horked )
        wapp.addApiPath('/v1/addup/([0-9]+)', foo.addup )

        client = AsyncHTTPClient(self.io_loop)
        port = wapp.getServBinds()[0][1]
        resp = yield client.fetch('http://127.0.0.1:%d/v1/addup/30?y=40' % port)
        resp = json.loads(resp.body.decode('utf-8'))

        self.assertEqual( resp.get('ret'), 70 )
        self.assertEqual( resp.get('status'), 'ok' )

        resp = yield client.fetch('http://127.0.0.1:%d/v1/addup/20' % port)
        resp = json.loads(resp.body.decode('utf-8'))

        self.assertEqual( resp.get('ret'), 20 )
        self.assertEqual( resp.get('status'), 'ok' )

        resp = yield client.fetch('http://127.0.0.1:%d/v1/horked' % port)
        resp = json.loads(resp.body.decode('utf-8'))

        self.assertEqual( resp.get('err'), 'Horked' )
        self.assertEqual( resp.get('status'), 'err' )

        wapp.fini()
Exemple #3
0
    def test_invalid_model(self):
        self.thisHostMustNot(platform='windows')
        client = AsyncHTTPClient(self.io_loop)

        with self.assertRaises(HTTPError) as context:
            yield client.fetch(self.host + '/v1/horked')
        self.assertEqual(context.exception.code, 404)
Exemple #4
0
	def test_idea_add(self):
		#Add
		client = AsyncHTTPClient(self.io_loop)
		
		resp = yield client.fetch(IDEA_URL)

		print resp.body
		self.assertIn("", resp.body)
Exemple #5
0
        def do_login():

            client = AsyncHTTPClient(self.io_loop)
            #examlpe url: /mr.login?message=<message in JSON>&other_data_for_the_query_string=value
            url = self.get_url('/mr.login?message=' + urllib.parse.quote(json.dumps(message0)))

            response = yield client.fetch(url, method='GET')

            self.assertEqual(response.code, 200)
Exemple #6
0
    def test_login(self):

        client = AsyncHTTPClient(self.io_loop)


        message0 = {"type": "login", "payload": "django"}

        url = self.get_url('/mr.login?message=' + urllib.parse.quote(json.dumps(message0)))

        response = yield client.fetch(url, method='GET')

        self.assertEqual(response.code, 200)

        bodystr = None
        try:
            bodystr = response.body.decode("utf-8")
        except ValueError:
            self.fail("Mr. Login gave an invalid response to a login; it was not valid utf-8; response: %s" % repr(response.body))


        login_response = None
        try:
            login_response = json.loads(bodystr)
        except ValueError:
            self.fail("Mr. Login gave an invalid response to a login; it was not valid json; response: %s" % repr(response.body))

        
        self.assertTrue( isinstance(login_response, dict), msg=login_response )


        self.assertTrue( 'success' in login_response, msg=login_response )
        self.assertTrue( isinstance(login_response['success'], bool), msg=login_response )
        self.assertTrue( login_response['success'], msg=login_response )



        self.assertTrue( 'msg' in login_response, msg=login_response )
        self.assertTrue( isinstance(login_response['msg'], str), msg=login_response )


        cache = self.myapp.global_message_buffer.cache
        self.assertTrue( len(cache) > 0, msg=cache )
        self.assertTrue( cache[-1]['type'] == message0['type'] )
        self.assertTrue( cache[-1]['payload'] == message0['payload'] )
Exemple #7
0
    def test_crud_multi(self):
        self.thisHostMustNot(platform='windows')
        client = AsyncHTTPClient(self.io_loop)

        # Can we create tufos?
        tufo = {}
        for i in range(2):
            i = str(i)
            data = json.dumps({'bar': i, 'key' + i: 'val' + i})
            resp = yield client.fetch(self.host + '/v1/bar', method='POST', body=data, headers={'Content-Type': 'application/json'})
            resp = json.loads(resp.body.decode('utf-8'))
            self.assertEqual(resp['status'], 'ok')
            self.assertDictMust(resp['ret'][1], {'bar': i, 'bar:bar': i, 'bar:key' + i: 'val' + i, 'tufo:form': 'bar',})
            tufo[resp['ret'][0]] = resp['ret'][1]

        # Do they persist?
        resp = yield client.fetch(self.host + '/v1/bar')
        resp = json.loads(resp.body.decode('utf-8'))
        self.assertEqual(resp['status'], 'ok')
        self.assertEqual(len(resp['ret']), len(tufo))
        for rslt in resp['ret']:
            self.assertDictMust(tufo[rslt[0]],rslt[1])

        # Can we get a subset?
        resp = yield client.fetch(self.host + '/v1/bar?prop=bar:key1&value=val1')
        resp = json.loads(resp.body.decode('utf-8'))
        self.assertEqual(resp['status'], 'ok')
        self.assertEqual(len(resp['ret']), 1)
        for rslt in resp['ret']:
            self.assertDictMust(tufo[rslt[0]],rslt[1])
            self.assertEqual(rslt[1]['bar:key1'], 'val1')

        # Can we delete a subset?
        resp = yield client.fetch(self.host + '/v1/bar?prop=bar:key1&value=val1', method='DELETE')
        resp = json.loads(resp.body.decode('utf-8'))
        self.assertEqual(resp['status'], 'ok')

        resp = yield client.fetch(self.host + '/v1/bar')
        resp = json.loads(resp.body.decode('utf-8'))
        self.assertEqual(resp['status'], 'ok')
        self.assertEqual(len(resp['ret']), 1)
        for rslt in resp['ret']:
            self.assertDictMust(tufo[rslt[0]], rslt[1])
            self.assertEqual(rslt[1]['bar:key0'], 'val0')

        # Can they be deleted?
        resp = yield client.fetch(self.host + '/v1/bar', method='DELETE')
        resp = json.loads(resp.body.decode('utf-8'))
        self.assertEqual(resp['status'], 'ok')

        resp = yield client.fetch(self.host + '/v1/bar')
        resp = json.loads(resp.body.decode('utf-8'))
        self.assertEqual(resp['status'], 'ok')
        self.assertEqual(resp['ret'], [])
Exemple #8
0
class TestHandlerBase(AsyncTestCase):
    def setUp(self):
        super(TestHandlerBase, self).setUp()
        self.client = AsyncHTTPClient(self.io_loop)


    def tearDown(self):
        pass

    def testgetItems(self):
        res = self.client.fetch('ok')
        self.wait()
        assert res.code == 412


    def get_app(self):
        import alfred.webserver
        return alfred.webserver.WebServer().start()
Exemple #9
0
    def test_crud_single(self):
        self.thisHostMustNot(platform='windows')
        client = AsyncHTTPClient(self.io_loop)

        # Can we create a tufo?
        data = json.dumps({'foo': 'val1', 'key2': 'val2'})
        tufo = {'foo': 'val1', 'foo:foo': 'val1', 'foo:key2': 'val2', 'tufo:form': 'foo'}
        resp = yield client.fetch(self.host + '/v1/foo', method='POST', body=data, headers={'Content-Type': 'application/json'})
        resp = json.loads(resp.body.decode('utf-8'))
        self.assertEqual(resp['status'], 'ok')
        #self.assertEqual(resp['ret'][1], tufo)
        self.assertDictMust(resp['ret'][1], tufo)
        iden = resp['ret'][0]

        # Does it persist?
        resp = yield client.fetch(self.host + '/v1/foo/' + iden)
        resp = json.loads(resp.body.decode('utf-8'))
        self.assertEqual(resp['status'], 'ok')
        self.assertEqual(resp['ret'], [iden, tufo])

        # Can it be updated?
        data = json.dumps({'key2': 'val22', 'key3': 'val3'})
        tufo = {'foo': 'val1', 'foo:foo': 'val1', 'foo:key2': 'val22', 'foo:key3': 'val3', 'tufo:form': 'foo'}
        resp = yield client.fetch(self.host + '/v1/foo/' + iden, method='PATCH', body=data, headers={'Content-Type': 'application/json'})
        resp = json.loads(resp.body.decode('utf-8'))
        self.assertEqual(resp['status'], 'ok')
        self.assertEqual(resp['ret'][1], tufo)

        # Can it be deleted?
        resp = yield client.fetch(self.host + '/v1/foo/' + iden, method='DELETE')
        resp = json.loads(resp.body.decode('utf-8'))
        self.assertEqual(resp['status'], 'ok')

        resp = yield client.fetch(self.host + '/v1/foo/' + iden)
        resp = json.loads(resp.body.decode('utf-8'))
        self.assertEqual(resp['status'], 'ok')
        assert resp['ret'] is None
Exemple #10
0
	def test_index_fetch(self):
		client = AsyncHTTPClient(self.io_loop)
		
		resp = yield client.fetch(INDEX_URL)
		self.assertIn("hello", resp.body)
Exemple #11
0
	def tearDown(self):
		print "TearDown IdeaTestCase..."
		AsyncHTTPClient._restore_configuration(self.saved)
		super(IdeaTestCase, self).tearDown()
Exemple #12
0
    def test_broadcast(self):



        message0 = {"type": "login", "payload": "django"}

        @tornado.gen.coroutine
        def do_login():

            client = AsyncHTTPClient(self.io_loop)
            #examlpe url: /mr.login?message=<message in JSON>&other_data_for_the_query_string=value
            url = self.get_url('/mr.login?message=' + urllib.parse.quote(json.dumps(message0)))

            response = yield client.fetch(url, method='GET')

            self.assertEqual(response.code, 200)


        yield do_login()



        client = AsyncHTTPClient(self.io_loop)

        url = self.get_url('/mr.broadcaster?cursor=100002325435')
        response = yield client.fetch(url, method='GET')
        self.assertEqual(response.code, 200)

        bodystr = None
        try:
            bodystr = response.body.decode("utf-8")
        except ValueError:
            self.fail("Mr. Broadcaster gave an invalid response to a broadcast request; it was not valid utf-8; response: %s" % repr(response.body))


        broadcast = None
        try:
            broadcast = json.loads(bodystr)
        except ValueError:
            self.fail("Mr. Broadcaster gave an invalid response to a broadcast request; it was not valid json; response: %s" % repr(response.body))


        self.assertTrue( isinstance(broadcast, dict), msg=broadcast )

        self.assertTrue( 'messages' in broadcast, msg=broadcast )
        self.assertTrue( isinstance(broadcast['messages'], list), msg=broadcast )


        found_login_message = False

        for message in broadcast['messages']:

            self.assertTrue( isinstance(message, dict), msg=message )



            self.assertTrue( 'id' in message, msg=message )
            self.assertTrue( isinstance(message['id'], (int,str)), msg=message )



            self.assertTrue( 'type' in message, msg=message )
            self.assertTrue( isinstance(message['type'], str), msg=message )
            self.assertTrue( message['type'] in ['login'], msg=message )



            self.assertTrue( 'payload' in message, msg=message )

            if (message['type'] == 'login'):
                self.assertTrue( isinstance(message['payload'], str), msg=message )



            if message['type'] == 'login' and message['payload'] == 'django':
                found_login_message = True



        self.assertTrue(found_login_message
                            , msg='Did not find the expected login message that we gave Mr. login; message0: %s, broadcast: %s'
                                     % (message0, broadcast))
Exemple #13
0
 def test_http_fetch(self):
     client = AsyncHTTPClient(self.io_loop)
     client.fetch("http://www.tornadoweb.org/", self.handle_fetch)
     self.wait()
Exemple #14
0
 def test_http_fetch(self):
     client = AsyncHTTPClient(self.io_loop)
     client.fetch("http://www.tornadoweb.org/", self.stop)
     response = self.wait()
     # Test contents of response
     self.assertIn("FriendFeed", response.body)
Exemple #15
0
 def setUp(self):
     super(TestHandlerBase, self).setUp()
     self.client = AsyncHTTPClient(self.io_loop)
Exemple #16
0
	def setUp(self):
		print "Init IdeaTestCase..."
		super(IdeaTestCase, self).setUp()
		self.saved = AsyncHTTPClient._save_configuration()
 def test_http_fetch(self):
     print("testMainHandler")
     client = AsyncHTTPClient(self.io_loop)
     response = yield client.fetch("http://localhost:8080")
     # Test contents of response
     self.assertEqual("hello world", response.body)
 def test_http_fetch(self):
     client = AsyncHTTPClient(self.io_loop)
     response = yield client.fetch("http://www.baidu.com")
     # Test contents of response
     print(response)
 def test_http_fetch(self):
     client = AsyncHTTPClient(self.io_loop)
     client.fetch("http://www.baidu.com/", self.handle_fetch)
     self.wait()
 def test_http_fetch(self):
     client = AsyncHTTPClient(self.io_loop)
     client.fetch("http://www.baidu.com/", self.stop)
     response = self.wait()
     print(response)