Esempio n. 1
0
class Test_headers(Test_url):
    def setUp(self):
        super().setUp()
        self.url = Chibi_url('http://a.4cdn.org/{board}/threads.json')

    def test_headers_by_default_should_be_a_empty_dict(self):
        self.assertIsInstance(self.url.headers, dict)
        self.assertEqual(self.url.headers, {})

    def test_should_add_the_content_type(self):
        self.url.headers.content_type = 'application/json'
        self.assertEqual(self.url.headers.content_type, 'application/json')

    @patch('requests.get')
    def test_should_send_the_auth_using_get(self, requests):
        self.url.headers.content_type = 'application/json'
        self.url.get()
        self.assertEqual(requests.call_args[1]['headers'], self.url.headers)

    @patch('requests.post')
    def test_should_send_the_auth_using_post(self, requests):
        self.url.headers.content_type = 'application/json'
        self.url.post()
        self.assertEqual(requests.call_args[1]['headers'], self.url.headers)

    @patch('requests.post')
    def test_should_be_parse_data_with_content_type(self, requests):
        self.url.headers.content_type = 'application/json'
        self.url.post({'data': 'asdf'})
        j = requests.call_args[0][1]
        self.assertEqual(j, '{"data": "asdf"}')
Esempio n. 2
0
class Test_auth(Test_url):
    def setUp(self):
        super().setUp()
        self.url = Chibi_url('http://a.4cdn.org/{board}/threads.json')

    def test_when_add_a_auth_class_should_create_a_new_object(self):
        url_other = self.url + HTTPBasicAuth('some_user', 'some_password')
        self.assertIsNot(self.url, url_other)
        self.assertEqual(str(self.url), str(url_other))
        self.assertIsNotNone(url_other.auth)
        self.assertIsNone(self.url.auth)

    def test_using_iadd_should_add_iternaly_the_auth(self):
        auth = HTTPBasicAuth('some_user', 'some_password')
        self.url += auth
        self.assertIsNotNone(self.url.auth)
        self.assertEqual(self.url.auth, auth)

    def test_add_parmas_to_the_url_shoudl_carry_the_auth(self):
        auth = HTTPBasicAuth('some_user', 'some_password')
        self.url += auth
        other_url = self.url + {'param1': 'value1'}

        self.assertNotEqual(self.url, other_url)
        self.assertEqual(self.url.auth, other_url.auth)

    @patch('requests.get')
    def test_should_send_the_auth_using_get(self, requests):
        self.url += HTTPBasicAuth('some_user', 'some_password')
        self.url.get()
        self.assertEqual(requests.call_args[1]['auth'], self.url.auth)

    @patch('requests.post')
    def test_should_send_the_auth_using_post(self, requests):
        self.url += HTTPBasicAuth('some_user', 'some_password')
        self.url.post()
        self.assertEqual(requests.call_args[1]['auth'], self.url.auth)
Esempio n. 3
0
class Test_methods(Test_url):
    def setUp(self):
        self.url = Chibi_url('http://ifconfig.me')

    def test_get(self):
        response = self.url.get()
        self.assertTrue(response)
        self.assertIsInstance(response, Response)
        self.assertTrue(response.is_text)
        self.assertIsInstance(response.native, str)
        self.assertTrue(response.native)

    def test_post(self):
        response = self.url.post()
        self.assertTrue(response)
        self.assertIsInstance(response, Response)
        self.assertTrue(response.is_json)
        self.assertIsInstance(response.native, Chibi_atlas)
        self.assertTrue(response.native)