Skip to content
Myr

test_requests.py

This file

Path

test_requests.py

143 lines · 4.4 KiB · Python

Last changed by

add autoauth tuple http basic testKenneth Reitzbefore adoptionf494cd9

History of this file

Take it

1#!/usr/bin/env python2# -*- coding: utf-8 -*-34import unittest5import cookielib67import requests8910class RequestsTestSuite(unittest.TestCase):11    """Requests test cases."""1213    def setUp(self):14        pass1516    def tearDown(self):17        """Teardown."""18        pass1920    def test_invalid_url(self):21        self.assertRaises(ValueError, requests.get, 'hiwpefhipowhefopw')2223    def test_HTTP_200_OK_GET(self):24        r = requests.get('http://google.com')25        self.assertEqual(r.status_code, 200)2627    def test_HTTPS_200_OK_GET(self):28        r = requests.get('https://google.com')29        self.assertEqual(r.status_code, 200)3031    def test_HTTP_200_OK_GET_WITH_PARAMS(self):32        heads = {'User-agent': 'Mozilla/5.0'}3334        r = requests.get('http://www.google.com/search', params={'q': 'test'}, headers=heads)35        self.assertEqual(r.status_code, 200)3637    def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self):38        heads = {'User-agent': 'Mozilla/5.0'}3940        r = requests.get('http://google.com/search?test=true', params={'q': 'test'}, headers=heads)41        self.assertEqual(r.status_code, 200)4243    def test_user_agent_transfers(self):44        """Issue XX"""45        heads = {'User-agent':46                 'Mozilla/5.0 (github.com/kennethreitz/requests)'}4748        r = requests.get('http://whatsmyua.com', headers=heads);49        self.assertTrue(heads['User-agent'] in r.content)5051    def test_HTTP_200_OK_HEAD(self):52        r = requests.head('http://google.com')53        self.assertEqual(r.status_code, 200)5455    def test_HTTPS_200_OK_HEAD(self):56        r = requests.head('https://google.com')57        self.assertEqual(r.status_code, 200)5859    def test_AUTH_HTTPS_200_OK_GET(self):60        auth = ('requeststest', 'requeststest')61        url = 'https://convore.com/api/account/verify.json'62        r = requests.get(url, auth=auth)6364        self.assertEqual(r.status_code, 200)6566        r = requests.get(url)67        self.assertEqual(r.status_code, 200)6869        # reset auto authentication70        requests.auth_manager.empty()7172    def test_POSTBIN_GET_POST_FILES(self):73        bin = requests.post('http://www.postbin.org/')74        self.assertEqual(bin.status_code, 200)7576        post = requests.post(bin.url, data={'some': 'data'})77        self.assertEqual(post.status_code, 201)7879        post2 = requests.post(bin.url, files={'some': open('test_requests.py')})80        self.assertEqual(post2.status_code, 201)8182    def test_POSTBIN_GET_POST_FILES_WITH_PARAMS(self):83        bin = requests.post('http://www.postbin.org/')8485        self.assertEqual(bin.status_code, 200)8687        post2 = requests.post(bin.url, files={'some': open('test_requests.py')}, data={'some': 'data'})88        self.assertEqual(post2.status_code, 201)899091    def test_POSTBIN_GET_POST_FILES_WITH_HEADERS(self):92        bin = requests.post('http://www.postbin.org/')93        self.assertEqual(bin.status_code, 200)9495        post2 = requests.post(bin.url, files={'some': open('test_requests.py')},96        headers={'User-Agent': 'requests-tests'})9798        self.assertEqual(post2.status_code, 201)99100    def test_nonzero_evaluation(self):101        r = requests.get('http://google.com/some-404-url')102        self.assertEqual(bool(r), False)103104        r = requests.get('http://google.com/')105        self.assertEqual(bool(r), True)106107    def test_request_ok_set(self):108        r = requests.get('http://google.com/some-404-url')109        self.assertEqual(r.ok, False)110111    def test_status_raising(self):112        r = requests.get('http://google.com/some-404-url')113        self.assertRaises(requests.HTTPError, r.raise_for_status)114115        r = requests.get('http://google.com/')116        self.assertFalse(r.error)117        r.raise_for_status()118119    def test_cookie_jar(self):120        """121        .. todo:: This really doesn't test to make sure the cookie is working122        """123        jar = cookielib.CookieJar()124        self.assertFalse(jar)125126        requests.get('http://google.com', cookies=jar)127        self.assertTrue(jar)128129    def test_decompress_gzip(self):130131        r = requests.get('http://api.stackoverflow.com/1.1/users/495995/top-answer-tags')132        r.content.decode('ascii')133134    def test_autoauth(self):135136        conv_auth = ('requeststest', 'requeststest')137        requests.auth_manager.add_auth('convore.com', conv_auth)138139        r = requests.get('https://convore.com/api/account/verify.json')140        self.assertEquals(r.status_code, 200)141142if __name__ == '__main__':143    unittest.main()