class TornadoTestCase(testing.AsyncHTTPTestCase):
    """
    """
    def setUp(self):
        global received_file
        global received_meta

        # reset global
        received_file = received_meta = None

        super(TornadoTestCase, self).setUp()
        self.client = TornadoClient()


    def get_new_ioloop(self):
        return IOLoop.instance()

    def get_app(self):
        global app
        return app

    @testing.gen_test
    def test_updatePet(self):
        """ updatePet """
        global pet_db
        resp = yield self.client.request(
            sapp.op['updatePet'](body=dict(id=1, name='Tom1')),
            opt=dict(
                url_netloc='localhost:'+str(self.get_http_port())
            ))

        self.assertEqual(resp.status, 200)
        self.assertEqual(pet_db.read_(1)['name'], 'Tom1')

    @testing.gen_test
    def test_reuse_req_and_resp(self):
        """ make sure reusing (req, resp) is fine """
        global pet_db
        cache = sapp.op['updatePet'](body=dict(id=1, name='Tom1'))
        resp = yield self.client.request(
            cache,
            opt=dict(
                url_netloc='localhost:'+str(self.get_http_port())
            ))
        self.assertEqual(resp.status, 200)
        self.assertEqual(pet_db.read_(1)['name'], 'Tom1')
        resp = yield self.client.request(
            cache,
            opt=dict(
                url_netloc='localhost:'+str(self.get_http_port())
            ))
        self.assertEqual(resp.status, 200)
        self.assertEqual(pet_db.read_(1)['name'], 'Tom1')

    @testing.gen_test
    def test_addPet(self):
        """ addPet """
        global pet_db

        resp = yield self.client.request(
            sapp.op['addPet'](body=dict(id=5, name='Mission')),
            opt=dict(
                url_netloc='localhost:'+str(self.get_http_port())
            ))

        self.assertEqual(resp.status, 200)
        self.assertEqual(pet_db.read_(5)['name'], 'Mission')

    @testing.gen_test
    def test_deletePet(self):
        """ deletePet """
        resp = yield self.client.request(
            sapp.op['deletePet'](petId=5),
            opt=dict(
                url_netloc='localhost:'+str(self.get_http_port())
            ))

        self.assertEqual(resp.status, 200)
        self.assertEqual(pet_db.read_(5), None)

    @testing.gen_test
    def test_getPetById(self):
        """ getPetById """
        resp = yield self.client.request(
            sapp.op['getPetById'](petId=2),
            opt=dict(
                url_netloc='localhost:'+str(self.get_http_port())
            ))

        self.assertEqual(resp.status, 200)
        self.assertEqual(resp.data, pet_Mary)

    @testing.gen_test
    def test_uploadFile(self):
        """ uploadFile """
        global received_file
        global received_meta

        resp = yield self.client.request(
            sapp.op['uploadFile'](
                additionalMetadata='a test file', file=dict(data=six.StringIO('a test Content'), filename='test.txt')),
            opt=dict(
                url_netloc='localhost:'+str(self.get_http_port())
            ))

        self.assertEqual(resp.status, 200)
        self.assertEqual(received_file.decode(), 'a test Content')
        self.assertEqual(received_meta, 'a test file')

    @testing.gen_test
    def test_uploadImages(self):
        """ test for uploading multiple files """
        global received_files

        app = App._create_(get_test_data_folder(version='2.0', which=os.path.join('io', 'files')))
        resp = yield self.client.request(
            app.op['upload_images'](images=[
                dict(data=six.BytesIO(six.b('test image 1')), filename='_1.k'),
                dict(data=six.BytesIO(six.b('test image 2')), filename='_2.k'),
                dict(data=six.BytesIO(six.b('test image 3')), filename='_3.k'),
            ]),
            opt=dict(
                url_netloc='localhost:'+str(self.get_http_port())
            )
        )

        self.assertEqual(received_files[0], {'body': six.b('test image 1'), 'content_type': 'application/unknown', 'filename': u'_1.k'})
        self.assertEqual(received_files[1], {'body': six.b('test image 2'), 'content_type': 'application/unknown', 'filename': u'_2.k'})
        self.assertEqual(received_files[2], {'body': six.b('test image 3'), 'content_type': 'application/unknown', 'filename': u'_3.k'})

    @testing.gen_test
    def test_custom_headers(self):
        """ test customized headers """
        global received_headers

        yield self.client.request(
            sapp.op['updatePet'](
                body=dict(id=1, name='Tom1')),
                opt=dict(
                    url_netloc='localhost:'+str(self.get_http_port())
                ),
                headers={'X-TEST-HEADER': 'aaa'}
            )

        self.assertEqual(received_headers['X-TEST-HEADER'], 'aaa')

    @testing.gen_test
    def test_custom_headers_multiple_values_to_one_key(self):
        """ test customized headers with multiple values to one key """
        global received_headers

        yield self.client.request(
            sapp.op['updatePet'](
                body=dict(id=1, name='Tom1')),
                opt=dict(
                    url_netloc='localhost:'+str(self.get_http_port()),
                ),
                headers=[('X-TEST-HEADER', 'aaa'), ('X-TEST-HEADER', 'bbb')]
            )

        self.assertEqual(received_headers.get_list('X-TEST-HEADER'), ['aaa', 'bbb'])
Exemple #2
0
class TornadoTestCase(testing.AsyncHTTPTestCase):
    """
    """
    def setUp(self):
        global received_file
        global received_meta

        # reset global
        received_file = received_meta = None

        super(TornadoTestCase, self).setUp()
        self.client = TornadoClient()

    def get_new_ioloop(self):
        return IOLoop.instance()

    def get_app(self):
        global app
        return app

    @testing.gen_test
    def test_updatePet(self):
        """ updatePet """
        global pet_db
        resp = yield self.client.request(
            sapp.op['updatePet'](body=dict(id=1, name='Tom1')),
            opt=dict(url_netloc='localhost:' + str(self.get_http_port())))

        self.assertEqual(resp.status, 200)
        self.assertEqual(pet_db.read_(1)['name'], 'Tom1')

    @testing.gen_test
    def test_reuse_req_and_resp(self):
        """ make sure reusing (req, resp) is fine """
        global pet_db
        cache = sapp.op['updatePet'](body=dict(id=1, name='Tom1'))
        resp = yield self.client.request(cache,
                                         opt=dict(url_netloc='localhost:' +
                                                  str(self.get_http_port())))
        self.assertEqual(resp.status, 200)
        self.assertEqual(pet_db.read_(1)['name'], 'Tom1')
        resp = yield self.client.request(cache,
                                         opt=dict(url_netloc='localhost:' +
                                                  str(self.get_http_port())))
        self.assertEqual(resp.status, 200)
        self.assertEqual(pet_db.read_(1)['name'], 'Tom1')

    @testing.gen_test
    def test_addPet(self):
        """ addPet """
        global pet_db

        resp = yield self.client.request(
            sapp.op['addPet'](body=dict(id=5, name='Mission')),
            opt=dict(url_netloc='localhost:' + str(self.get_http_port())))

        self.assertEqual(resp.status, 200)
        self.assertEqual(pet_db.read_(5)['name'], 'Mission')

    @testing.gen_test
    def test_deletePet(self):
        """ deletePet """
        resp = yield self.client.request(sapp.op['deletePet'](petId=5),
                                         opt=dict(url_netloc='localhost:' +
                                                  str(self.get_http_port())))

        self.assertEqual(resp.status, 200)
        self.assertEqual(pet_db.read_(5), None)

    @testing.gen_test
    def test_getPetById(self):
        """ getPetById """
        resp = yield self.client.request(sapp.op['getPetById'](petId=2),
                                         opt=dict(url_netloc='localhost:' +
                                                  str(self.get_http_port())))

        self.assertEqual(resp.status, 200)
        self.assertEqual(resp.data, pet_Mary)

    @testing.gen_test
    def test_uploadFile(self):
        """ uploadFile """
        global received_file
        global received_meta

        resp = yield self.client.request(
            sapp.op['uploadFile'](additionalMetadata='a test file',
                                  file=dict(
                                      data=six.StringIO('a test Content'),
                                      filename='test.txt')),
            opt=dict(url_netloc='localhost:' + str(self.get_http_port())))

        self.assertEqual(resp.status, 200)
        self.assertEqual(received_file.decode(), 'a test Content')
        self.assertEqual(received_meta, 'a test file')

    @testing.gen_test
    def test_uploadImages(self):
        """ test for uploading multiple files """
        global received_files

        app = App._create_(
            get_test_data_folder(version='2.0',
                                 which=os.path.join('io', 'files')))
        resp = yield self.client.request(app.op['upload_images'](images=[
            dict(data=six.BytesIO(six.b('test image 1')), filename='_1.k'),
            dict(data=six.BytesIO(six.b('test image 2')), filename='_2.k'),
            dict(data=six.BytesIO(six.b('test image 3')), filename='_3.k'),
        ]),
                                         opt=dict(url_netloc='localhost:' +
                                                  str(self.get_http_port())))

        self.assertEqual(
            received_files[0], {
                'body': six.b('test image 1'),
                'content_type': 'application/unknown',
                'filename': u'_1.k'
            })
        self.assertEqual(
            received_files[1], {
                'body': six.b('test image 2'),
                'content_type': 'application/unknown',
                'filename': u'_2.k'
            })
        self.assertEqual(
            received_files[2], {
                'body': six.b('test image 3'),
                'content_type': 'application/unknown',
                'filename': u'_3.k'
            })

    @testing.gen_test
    def test_custom_headers(self):
        """ test customized headers """
        global received_headers

        yield self.client.request(
            sapp.op['updatePet'](body=dict(id=1, name='Tom1')),
            opt=dict(url_netloc='localhost:' + str(self.get_http_port())),
            headers={'X-TEST-HEADER': 'aaa'})

        self.assertEqual(received_headers['X-TEST-HEADER'], 'aaa')

    @testing.gen_test
    def test_custom_headers_multiple_values_to_one_key(self):
        """ test customized headers with multiple values to one key """
        global received_headers

        yield self.client.request(
            sapp.op['updatePet'](body=dict(id=1, name='Tom1')),
            opt=dict(url_netloc='localhost:' + str(self.get_http_port()), ),
            headers=[('X-TEST-HEADER', 'aaa'), ('X-TEST-HEADER', 'bbb')])

        self.assertEqual(received_headers.get_list('X-TEST-HEADER'),
                         ['aaa', 'bbb'])
Exemple #3
0
class TornadoTestCase(testing.AsyncHTTPTestCase):
    """
    """
    def setUp(self):
        global received_file
        global received_meta

        # reset global
        received_file = received_meta = None

        super(TornadoTestCase, self).setUp()
        self.client = TornadoClient()


    def get_new_ioloop(self):
        return IOLoop.instance()

    def get_app(self):
        global app
        return app

    @testing.gen_test
    def test_updatePet(self):
        """ updatePet """
        global pet_db
        resp = yield self.client.request(
            sapp.op['updatePet'](body=dict(id=1, name='Tom1')),
            opt=dict(
                url_netloc='localhost:'+str(self.get_http_port())
            ))

        self.assertEqual(resp.status, 200)
        self.assertEqual(pet_db.read_(1)['name'], 'Tom1')

    @testing.gen_test
    def test_addPet(self):
        """ addPet """
        global pet_db

        resp = yield self.client.request(
            sapp.op['addPet'](body=dict(id=5, name='Mission')),
            opt=dict(
                url_netloc='localhost:'+str(self.get_http_port())
            ))

        self.assertEqual(resp.status, 200)
        self.assertEqual(pet_db.read_(5)['name'], 'Mission')

    @testing.gen_test
    def test_deletePet(self):
        """ deletePet """
        resp = yield self.client.request(
            sapp.op['deletePet'](petId=5),
            opt=dict(
                url_netloc='localhost:'+str(self.get_http_port())
            ))

        self.assertEqual(resp.status, 200)
        self.assertEqual(pet_db.read_(5), None)

    @testing.gen_test
    def test_getPetById(self):
        """ getPetById """
        resp = yield self.client.request(
            sapp.op['getPetById'](petId=2),
            opt=dict(
                url_netloc='localhost:'+str(self.get_http_port())
            ))

        self.assertEqual(resp.status, 200)
        self.assertEqual(resp.data, pet_Mary)

    @testing.gen_test
    def test_uploadFile(self):
        """ uploadFile """
        global received_file
        global received_meta

        resp = yield self.client.request(
            sapp.op['uploadFile'](
                additionalMetadata='a test file', file=dict(data=six.StringIO('a test Content'), filename='test.txt')),
            opt=dict(
                url_netloc='localhost:'+str(self.get_http_port())
            ))

        self.assertEqual(resp.status, 200)
        self.assertEqual(received_file.decode(), 'a test Content')
        self.assertEqual(received_meta, 'a test file')
Exemple #4
0
class TornadoTestCase(testing.AsyncHTTPTestCase):
    """
    """
    def setUp(self):
        global received_file
        global received_meta

        # reset global
        received_file = received_meta = None

        super(TornadoTestCase, self).setUp()
        self.client = TornadoClient()

    def get_new_ioloop(self):
        return IOLoop.instance()

    def get_app(self):
        global app
        return app

    @testing.gen_test
    def test_updatePet(self):
        """ updatePet """
        global pet_db
        resp = yield self.client.request(
            sapp.op['updatePet'](body=dict(id=1, name='Tom1')),
            opt=dict(url_netloc='localhost:' + str(self.get_http_port())))

        self.assertEqual(resp.status, 200)
        self.assertEqual(pet_db.read_(1)['name'], 'Tom1')

    @testing.gen_test
    def test_addPet(self):
        """ addPet """
        global pet_db

        resp = yield self.client.request(
            sapp.op['addPet'](body=dict(id=5, name='Mission')),
            opt=dict(url_netloc='localhost:' + str(self.get_http_port())))

        self.assertEqual(resp.status, 200)
        self.assertEqual(pet_db.read_(5)['name'], 'Mission')

    @testing.gen_test
    def test_deletePet(self):
        """ deletePet """
        resp = yield self.client.request(sapp.op['deletePet'](petId=5),
                                         opt=dict(url_netloc='localhost:' +
                                                  str(self.get_http_port())))

        self.assertEqual(resp.status, 200)
        self.assertEqual(pet_db.read_(5), None)

    @testing.gen_test
    def test_getPetById(self):
        """ getPetById """
        resp = yield self.client.request(sapp.op['getPetById'](petId=2),
                                         opt=dict(url_netloc='localhost:' +
                                                  str(self.get_http_port())))

        self.assertEqual(resp.status, 200)
        self.assertEqual(resp.data, pet_Mary)

    @testing.gen_test
    def test_uploadFile(self):
        """ uploadFile """
        global received_file
        global received_meta

        resp = yield self.client.request(
            sapp.op['uploadFile'](additionalMetadata='a test file',
                                  file=dict(
                                      data=six.StringIO('a test Content'),
                                      filename='test.txt')),
            opt=dict(url_netloc='localhost:' + str(self.get_http_port())))

        self.assertEqual(resp.status, 200)
        self.assertEqual(received_file.decode(), 'a test Content')
        self.assertEqual(received_meta, 'a test file')