Exemplo n.º 1
0
class JSONClientTests(TestCase):
    def setUp(self):
        self.client = JSONClient()

    @mock.patch('requests.request')
    def test_get(self, request):
        response = Response()
        response.status_code = 200
        response._content = '{"foo": "bar"}'
        response.headers['Content-Type'] = 'application/json'
        request.return_value = response

        response = self.client.get(uri='http://localhost/api')

        data = response.content
        self.assertIsInstance(data, dict)
        self.assertTrue('foo' in data)
        self.assertEqual(data['foo'], 'bar')

    @mock.patch('requests.request')
    def test_incorrect_content_type(self, request):
        response = Response()
        response.status_code = 200
        response._content = '{"foo": "bar"}'
        response.headers['Content-Type'] = 'foobar'
        request.return_value = response
        response = self.client.get(uri='http://localhost/api')

        data = response.content
        self.assertIsInstance(data, str)
        self.assertEqual(data, '{"foo": "bar"}')
Exemplo n.º 2
0
class JSONClientTests(TestCase):
    def setUp(self):
        self.client = JSONClient()
        
    @mock.patch('httplib2.Http.request')
    def test_get(self, request):
        request.return_value = ({'Status': 200, 'Content-Type': 'application/json'}, '{"foo": "bar"}')
        response = self.client.get(uri='http://localhost/api')
        
        data = response.content
        self.assertIsInstance(data, dict)
        self.assertTrue('foo' in data)
        self.assertEqual(data['foo'], 'bar')

    @mock.patch('httplib2.Http.request')
    def test_incorrect_content_type(self, request):
        request.return_value = ({'Status': 200, 'Content-Type': 'foobar'}, '{"foo": "bar"}')
        response = self.client.get(uri='http://localhost/api')
        
        data = response.content
        self.assertIsInstance(data, basestring)
        self.assertEqual(data, '{"foo": "bar"}')