diff --git a/AUTHORS b/AUTHORS index b6e45cf4..6d2d1965 100644 --- a/AUTHORS +++ b/AUTHORS @@ -30,4 +30,8 @@ Patches and Suggestions - 潘旭 (Xu Pan) - Tamás Gulácsi - Rubén Abad -- Peter Manser \ No newline at end of file +- Peter Manser +- Jeremy Selie +- Jens Diemer +- Alex <@alopatin> +- Tom Hogans diff --git a/HISTORY.rst b/HISTORY.rst index 171dae44..7d98f8f8 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,6 +1,22 @@ History ------- + +0.6.0 (2011-09-??) +++++++++++++++++++ + +* New callback hook system +* New persistient sessions object and context manager +* Transparent Dict-cookie handling +* status code reference object +* Removed Response.cached +* Added Response.request +* all args are kwargs +* Relative redirect support +* HTTPError handling improvements +* Improved https testing +* Bugfixes + 0.5.1 (2011-07-23) ++++++++++++++++++ diff --git a/README.rst b/README.rst index 96a1f537..312dfc3c 100644 --- a/README.rst +++ b/README.rst @@ -83,7 +83,7 @@ If CookieJar object is is passed in (cookies=...), the cookies will be sent with PATCH Requests - >>> requests.post(url, data={}, headers={}, files={}, cookies=None, auth=None, timeout=None, allow_redirects=False, params{}, proxies={}) + >>> requests.patch(url, data={}, headers={}, files={}, cookies=None, auth=None, timeout=None, allow_redirects=False, params{}, proxies={}) DELETE Requests diff --git a/docs/community/faq.rst b/docs/community/faq.rst new file mode 100644 index 00000000..2ccb6a7b --- /dev/null +++ b/docs/community/faq.rst @@ -0,0 +1,43 @@ +.. _faq: + +Frequently Asked Questions +========================== + +This part of the documentation covers common questions about Requests. + +Why not Httplib2? +----------------- + +Chris Adams gave an excellent summary on +`Hacker News `_: + + httplib2 is part of why you should use requests: it's far more respectable + as a client but not as well documented and it still takes way too much code + for basic operations. I appreciate what httplib2 is trying to do, that + there's a ton of hard low-level annoyances in building a modern HTTP + client, but really, just use requests instead. Kenneth Reitz is very + motivated and he gets the degree to which simple things should be simple + whereas httplib2 feels more like an academic exercise than something + people should use to build production systems[1]. + + Disclosure: I'm listed in the requests AUTHORS file but can claim credit + for, oh, about 0.0001% of the awesomeness. + + 1. http://code.google.com/p/httplib2/issues/detail?id=96 is a good example: + an annoying bug which affect many people, there was a fix available for + months, which worked great when I applied it in a fork and pounded a couple + TB of data through it, but it took over a year to make it into trunk and + even longer to make it onto PyPI where any other project which required " + httplib2" would get the working version. + + +Python 3 Support? +----------------- + +It's on the way. + + +Keep-alive Support? +------------------- + +It's on the way. \ No newline at end of file diff --git a/docs/community/support.rst b/docs/community/support.rst new file mode 100644 index 00000000..53f3c819 --- /dev/null +++ b/docs/community/support.rst @@ -0,0 +1,37 @@ +.. _support: + +Support +======= + +If you have a questions or issues about Requests, there are serveral options: + +Send a Tweet +------------ + +If your question is less than 140 characters, feel free to send a tweet to +`@kennethreitz `_. + + +File an Issue +------------- + +If you notice some unexpected behavior in Requests, or want to see support +for a new feature, +`file an issue on GitHub `_. + + +E-mail +------ + +I'm more than happy to answer any personal or in-depth questions about +Requests. Feel free to email +`requests@kennethreitz.com `_. + + +IRC +--- + +The official Freenode channel for Requests is +`#python-requests `_ + +I'm also available as **kennethreitz** on Freenode. \ No newline at end of file diff --git a/docs/community/updates.rst b/docs/community/updates.rst new file mode 100644 index 00000000..942ccac1 --- /dev/null +++ b/docs/community/updates.rst @@ -0,0 +1,31 @@ +.. _updates: + +Updates +======= + +If you'd like to stay up to date on the community and development of Requests, +there are serveral options: + +GitHub +------ + +The best way to track the development of Requests is through +`the GitHub repo `_. + +Twitter +------- + +I often tweet about new features and releases of Requests. + +Follow `@kennethreitz `_ for updates. + + + +Mailing List +------------ + +There's a low-volume mailing list for Requests. To subscribe to the +mailing list, send an email to +`requests@librelist.org `_. + + diff --git a/docs/conf.py b/docs/conf.py index 899bc588..8987bb8c 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -155,7 +155,7 @@ html_sidebars = { #html_split_index = False # If true, links to the reST sources are added to the pages. -html_show_sourcelink = True +html_show_sourcelink = False # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. html_show_sphinx = False diff --git a/docs/dev/authors.rst b/docs/dev/authors.rst new file mode 100644 index 00000000..5b3cc85b --- /dev/null +++ b/docs/dev/authors.rst @@ -0,0 +1,5 @@ +Authors +======= + + +.. include:: ../../AUTHORS \ No newline at end of file diff --git a/docs/index.rst b/docs/index.rst index 0f6d9ca9..8bef62bb 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -13,7 +13,8 @@ Requests is an :ref:`ISC Licensed ` HTTP library, written in Python, for hu Most existing Python modules for sending HTTP requests are extremely verbose and cumbersome. Python's builtin **urllib2** module provides most of the HTTP capabilities you should need, but the api is thoroughly **broken**. -It requires an *enormous* amount of work (even method overrides) to perform the simplest of tasks. +It requires an *enormous* amount of work (even method overrides) to perform +the simplest of tasks. Things shouldn’t be this way. Not in Python. @@ -21,9 +22,11 @@ Things shouldn’t be this way. Not in Python. >>> r = requests.get('https://api.github.com', auth=('user', 'pass')) >>> r.status_code - 200 + 204 >>> r.headers['content-type'] 'application/json' + >>> r.content + ... See `the same code, without Requests `_. @@ -39,32 +42,50 @@ Testimonals `Twitter, Inc `_ uses Requests internally. **Daniel Greenfeld** - Nuked a 1200 LOC spaghetti code library with 10 lines of code thanks to @kennethreitz's request library. Today has been AWESOME. + Nuked a 1200 LOC spaghetti code library with 10 lines of code thanks to + @kennethreitz's request library. Today has been AWESOME. **Kenny Meyers** - Python HTTP: When in doubt, or when not in doubt, use Requests. Beautiful, simple, Pythonic. + Python HTTP: When in doubt, or when not in doubt, use Requests. Beautiful, + simple, Pythonic. **Rich Leland** Requests is awesome. That is all. **Steve Pike** - I can never remember how to do it the regular way. ``import requests; requests.get()`` is just so easy! + I can never remember how to do it the regular way. + ``import requests; requests.get()`` is just so easy! User Guide ---------- -This part of the documentation, which is mostly prose, begins with some background information about Requests, then focuses on step-by-step instructions for getting the most out of Requests. +This part of the documentation, which is mostly prose, begins with some +background information about Requests, then focuses on step-by-step +instructions for getting the most out of Requests. .. toctree:: :maxdepth: 2 user/intro user/install -.. user/quickstart + user/quickstart user/advanced +Community Guide +----------------- + +This part of the documentation, which is mostly prose, details the +Requests ecosystem and community. + +.. toctree:: + :maxdepth: 2 + + community/faq + community/support + community/updates + API Documentation ----------------- @@ -88,3 +109,4 @@ you. dev/internals dev/todo + dev/authors diff --git a/docs/user/advanced.rst b/docs/user/advanced.rst index e69de29b..83792b58 100644 --- a/docs/user/advanced.rst +++ b/docs/user/advanced.rst @@ -0,0 +1,136 @@ +.. _advanced: + +Advanced Usage +============== + +This document covers some of Requests more advanced features. + + +Session Objects +--------------- + +The Session object allows you to persist certain parameters across +requests. It also establishes a CookieJar and passes it along +to any requests made from the Session instance. + +A session object has all the methods of the main Requests API. + +Let's persist some cookies across requests:: + + with requests.session() as s: + + s.get('http://httpbin.org/cookies/set/sessioncookie/123456789') + r = s.get("http://httpbin.org/cookies") + + print r.content + + +Sessions can also be used to provide default data to the request methods:: + + headers = {'x-test': 'true'} + auth = ('user', 'pass') + + with requests.session(auth=auth, headers=headers) as c: + + # both 'x-test' and 'x-test2' are sent + c.get('http://httpbin.org/headers', header={'x-test2', 'true'}) + + +.. admonition:: Global Settings + + Certain parameters are best set at the ``request.config`` level + (e.g.. a global proxy, user agent header). + + +Event Hooks +----------- + +Requests has a hook system that you can use to manipulate portions of +the request process, or signal event handling. + +Available hooks: + +``args``: + A dictionary of the arguments being sent to Request(). + +``pre_request``: + The Request object, directly before being sent. + +``post_request``: + The Request object, directly after being sent. + +``response``: + The response generated from a Request. + + +You can assign a hook function on a per-request basis by passing a +``{hook_name: callback_function}`` dictionary to the ``hooks`` request +paramaeter:: + + hooks=dict(args=print_url) + +That ``callback_function`` will receive a chunk of data as its first +argument. + +:: + + def print_url(args): + print args['url'] + +If an error occurs while executing your callback, a warning is given. + +If the callback function returns a value, it is assumed that it is to +replace the data that was passed in. If the function doesn't return +anything, nothing else is effected. + +Let's print some request method arguments at runtime:: + + >>> requests.get('http://httpbin', hooks=dict(args=print_url)) + http://httpbin + + +Let's hijack some arguments this time with a new callback:: + + def hack_headers(args): + if not args[headers]: + args['headers'] = dict() + + args['headers'].update({'X-Testing': 'True'}) + + return args + + hooks = dict(args=hack_headers) + headers = dict(yo=dawg) + +And give it a try:: + + >>> requests.get('http://httpbin/headers', hooks=hooks, headers=headers) + { + "headers": { + "Content-Length": "", + "Accept-Encoding": "gzip", + "Yo": "dawg", + "X-Forwarded-For": "::ffff:24.127.96.129", + "Connection": "close", + "User-Agent": "python-requests.org", + "Host": "httpbin.org", + "X-Testing": "True", + "X-Forwarded-Protocol": "", + "Content-Type": "" + } + } + + + +Verbose Logging +--------------- + +If you want to get a good look at what HTTP requests are being sent +by your application, you can turn on verbose logging. + +To do so, just configure Requests with a stream to write to:: + + >>> requests.settings.verbose = sys.stderr + >>> requests.get('http://httpbin.org/headers') + 2011-08-17T03:04:23.380175 GET http://httpbin.org/headers + \ No newline at end of file diff --git a/docs/user/install.rst b/docs/user/install.rst index cd811ab7..cdc6d86c 100644 --- a/docs/user/install.rst +++ b/docs/user/install.rst @@ -15,7 +15,7 @@ Installing requests is simple with `pip `_:: or, with `easy_install `_:: - $ easy_install install requests + $ easy_install requests But, you really `shouldn't do that `_. @@ -24,7 +24,7 @@ But, you really `shouldn't do that `_:: +If the Cheeseshop is down, you can also install Requests from Kenneth Reitz's personal `Cheeseshop mirror `_:: $ pip install -i http://pip.kreitz.co/simple requests @@ -32,7 +32,7 @@ If the Cheeseshop is down, you can also install Requests from Kenneth Reitz's pe Get the Code ------------ -Requsts is actively developed on GitHub, where the code is +Requests is actively developed on GitHub, where the code is `always available `_. You can either clone the public repository:: diff --git a/docs/user/quickstart.rst b/docs/user/quickstart.rst index fea07662..60402f91 100644 --- a/docs/user/quickstart.rst +++ b/docs/user/quickstart.rst @@ -1,84 +1,134 @@ -Feature Overview -================ - -Requests is designed to solve a 90% use case — making simple requests. While most -HTTP libraries are extremely extensible, they often attempt to support the entire HTTP Spec. -This often leads to extremely messy and cumbersome APIs, as is the case with urllib2. Requests abandons support for edge-cases, and focuses on the essentials. - - -.. _features: - -Requests Can: -------------- - -- Make **GET**, **POST**, **PUT**, **DELETE**, and **HEAD** requests. -- Handle HTTP and HTTPS Requests -- Add Request headers (with a simple dictionary) -- URLEncode your Form Data (with a simple dictionary) -- Add Multi-part File Uploads (with a simple dictionary) -- Handle CookieJars (with a single parameter) -- Add HTTP Authentication (with a single parameter) -- Handle redirects (with history) -- Automatically decompress GZip'd responses -- Support Unicode URLs -- Gracefully timeout -- Interface with Eventlet & Gevent - - -Requests Can't: ---------------- - -- Handle Caching -- Handle Keep-Alives - +.. _quickstart: Quickstart ========== +.. module:: requests.models -GET Request ------------ +Eager to get started? This page gives a good introduction in how to get started with Requests. This assumes you already have Requests installed. If you do not, head over to the :ref:`Installation ` section. + +First, make sure that: + +* Requests is :ref:`installed ` +* Requests is :ref:`up-to-date ` -Adding Parameters ------------------ +Lets gets started with some simple use cases and examples. - -Adding Headers --------------- - - - -HTTP Basic Auth ---------------- - - -Tracking Redirects +Make a GET Request ------------------ +Making a standard request with Requests is very simple. + +Let's get GitHub's public timeline :: + + r = requests.get('https://github.com/timeline.json') + +Now, we have a :class:`Response` object called ``r``. We can get all the information we need from this. +Response Content +---------------- -HTTP POST (Form Data) +We can read the content of the server's response:: + + >>> r.content + '[{"repository":{"open_issues":0,"url":"https://github.com/... + + +Response Status Codes --------------------- +We can check the response status code:: -HTTP POST (Binary Data) ------------------------ + >>> r.status_code + 200 + +Requests also comes with a built-in status code lookup object for easy +reference:: + + >>> r.status_code == requests.codes.ok + True + +If we made a bad request, we can raise it with +:class:`Response.raise_for_status()`:: + + >>> _r = requests.get('http://httpbin.org/status/404') + >>> _r.status_code + 404 + + >>> _r.raise_for_status() + Traceback (most recent call last): + File "requests/models.py", line 394, in raise_for_status + raise self.error + urllib2.HTTPError: HTTP Error 404: NOT FOUND + +But, sice our ``status_code`` was ``200``, when we call it:: + + >>> r.raise_for_status() + None + +All is well. -HTTP POST (Multipart Files) ---------------------------- +Response Headers +---------------- + +We can view the server's response headers with a simple Python dictionary +interface:: + + >>> r.headers + { + 'status': '200 OK', + 'content-encoding': 'gzip', + 'transfer-encoding': 'chunked', + 'connection': 'close', + 'server': 'nginx/1.0.4', + 'x-runtime': '148ms', + 'etag': '"e1ca502697e5c9317743dc078f67693f"', + 'content-type': 'application/json; charset=utf-8' + } + +The dictionary is special, though: it's made just for HTTP headers. According to `RFC 2616 `_, HTTP +Headers are case-insensitive. + +So, we can access the headers using any capitalization we want:: + + >>> r.headers['Content-Type'] + 'application/json; charset=utf-8' + + >>> r.headers.get('content-type') + 'application/json; charset=utf-8' + +If a header doesn't exist in the Response, its value defaults to ``None``:: + + >>> r.headers['X-Random'] + None -HTTP PUT --------- +Cookies +------- +If a response contains some Cookies, you can get quick access to them:: -HTTP DELETE ------------ + >>> url = 'http://httpbin.org/cookies/set/requests-is/awesome' + >>> r = requests.get(url) + >>> print r.cookies + {'requests-is': 'awesome'} -HTTP HEAD ---------- +The underlying CookieJar is also available for more advanced handing:: + + >>> r.request.cookiejar + + +To send your own cookies to the server, you can use the ``cookies`` +parameter:: + + >>> url = 'http://httpbin.org/cookies' + >>> cookies = dict(cookies_are='working') + + >>> r = requests.get(url, cookies=cookies) + >>> r.content + '{"cookies": {"cookies_are": "working"}}' diff --git a/requests/api.py b/requests/api.py index c4690f2b..9c923e50 100644 --- a/requests/api.py +++ b/requests/api.py @@ -12,16 +12,21 @@ This module impliments the Requests API. """ import config -from .models import Request, Response, AuthManager, AuthObject, auth_manager +from .models import Request, Response, AuthObject +from .status_codes import codes +from .hooks import dispatch_hook +from .utils import cookiejar_from_dict +from urlparse import urlparse __all__ = ('request', 'get', 'head', 'post', 'patch', 'put', 'delete') def request(method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None, - timeout=None, allow_redirects=False, proxies=None): + timeout=None, allow_redirects=False, proxies=None, hooks=None): - """Constructs and sends a :class:`Request `. Returns :class:`Response ` object. + """Constructs and sends a :class:`Request `. + Returns :class:`Response ` object. :param method: method for the new :class:`Request` object. :param url: URL for the new :class:`Request` object. @@ -36,7 +41,12 @@ def request(method, url, :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. """ - r = Request( + if cookies is None: + cookies = {} + + cookies = cookiejar_from_dict(cookies) + + args = dict( method = method, url = url, data = data, @@ -44,20 +54,33 @@ def request(method, url, headers = headers, cookiejar = cookies, files = files, - auth = auth or auth_manager.get_auth(url), + auth = auth, timeout = timeout or config.settings.timeout, allow_redirects = allow_redirects, - proxies = proxies or config.settings.proxies + proxies = proxies or config.settings.proxies, ) + # Arguments manipulation hook. + args = dispatch_hook('args', hooks, args) + + r = Request(hooks=hooks, **args) + + # Pre-request hook. + r = dispatch_hook('pre_request', hooks, r) + + # Send the HTTP Request. r.send() + # Post-request hook. + r = dispatch_hook('post_request', hooks, r) + + # Response manipulation hook. + r.response = dispatch_hook('response', hooks, r.response) + return r.response -def get(url, - params=None, headers=None, cookies=None, auth=None, timeout=None, - proxies=None): +def get(url, **kwargs): """Sends a GET request. Returns :class:`Response` object. @@ -70,14 +93,10 @@ def get(url, :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. """ - return request('GET', url, - params=params, headers=headers, cookies=cookies, auth=auth, - timeout=timeout, proxies=proxies) + return request('GET', url, **kwargs) -def head(url, - params=None, headers=None, cookies=None, auth=None, timeout=None, - proxies=None): +def head(url, **kwargs): """Sends a HEAD request. Returns :class:`Response` object. @@ -90,14 +109,10 @@ def head(url, :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. """ - return request('HEAD', url, - params=params, headers=headers, cookies=cookies, auth=auth, - timeout=timeout, proxies=proxies) + return request('HEAD', url, **kwargs) -def post(url, - data='', headers=None, files=None, cookies=None, auth=None, timeout=None, - allow_redirects=False, params=None, proxies=None): +def post(url, data='', **kwargs): """Sends a POST request. Returns :class:`Response` object. @@ -113,14 +128,10 @@ def post(url, :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. """ - return request('POST', url, - params=params, data=data, headers=headers, files=files, - cookies=cookies, auth=auth, timeout=timeout, - allow_redirects=allow_redirects, proxies=proxies) + return request('POST', url, data=data, **kwargs) -def put(url, data='', headers=None, files=None, cookies=None, auth=None, - timeout=None, allow_redirects=False, params=None, proxies=None): +def put(url, data='', **kwargs): """Sends a PUT request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. @@ -135,14 +146,10 @@ def put(url, data='', headers=None, files=None, cookies=None, auth=None, :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. """ - return request('PUT', url, - params=params, data=data, headers=headers, files=files, - cookies=cookies, auth=auth, timeout=timeout, - allow_redirects=allow_redirects, proxies=proxies) + return request('PUT', url, data=data, **kwargs) -def patch(url, data='', headers=None, files=None, cookies=None, auth=None, - timeout=None, allow_redirects=False, params=None, proxies=None): +def patch(url, data='', **kwargs): """Sends a PATCH request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. @@ -157,15 +164,10 @@ def patch(url, data='', headers=None, files=None, cookies=None, auth=None, :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. """ - return request('PATCH', url, - params=params, data=data, headers=headers, files=files, - cookies=cookies, auth=auth, timeout=timeout, - allow_redirects=allow_redirects, proxies=proxies) + return request('PATCH', url, **kwargs) -def delete(url, - params=None, headers=None, cookies=None, auth=None, timeout=None, - allow_redirects=False, proxies=None): +def delete(url, **kwargs): """Sends a DELETE request. Returns :class:`Response` object. @@ -179,6 +181,4 @@ def delete(url, :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. """ - return request('DELETE', url, - params=params, headers=headers, cookies=cookies, auth=auth, - timeout=timeout, allow_redirects=allow_redirects, proxies=proxies) + return request('DELETE', url, **kwargs) diff --git a/requests/config.py b/requests/config.py index 1ba438fd..a42f15d2 100644 --- a/requests/config.py +++ b/requests/config.py @@ -12,7 +12,7 @@ class Settings(object): _singleton = {} # attributes with defaults - __attrs__ = ('timeout', 'verbose') + __attrs__ = [] def __init__(self, **kwargs): super(Settings, self).__init__() @@ -53,7 +53,14 @@ class Settings(object): return None return object.__getattribute__(self, key) + settings = Settings() + settings.base_headers = {'User-Agent': 'python-requests.org'} settings.accept_gzip = True settings.proxies = None +settings.verbose = None +settings.timeout = None + +#: Use socket.setdefaulttimeout() as fallback? +settings.timeout_fallback = True diff --git a/requests/core.py b/requests/core.py index 5215fca2..68e53923 100644 --- a/requests/core.py +++ b/requests/core.py @@ -12,14 +12,16 @@ This module implements the main Requests system. """ __title__ = 'requests' -__version__ = '0.5.1' -__build__ = 0x000501 +__version__ = '0.6.0' +__build__ = 0x000600 __author__ = 'Kenneth Reitz' __license__ = 'ISC' __copyright__ = 'Copyright 2011 Kenneth Reitz' -from models import HTTPError, auth_manager +from models import HTTPError from api import * from exceptions import * +from sessions import session +from status_codes import codes from config import settings \ No newline at end of file diff --git a/requests/hooks.py b/requests/hooks.py new file mode 100644 index 00000000..2938029b --- /dev/null +++ b/requests/hooks.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- + +""" +requests.hooks +~~~~~~~~~~~~~~ + +This module provides the capabilities for the Requests hooks system. + +Available hooks: + +``args``: + A dictionary of the arguments being sent to Request(). + +``pre_request``: + The Request object, directly before being sent. + +``post_request``: + The Request object, directly after being sent. + +``response``: + The response generated from a Request. + +""" + +import warnings + + +def dispatch_hook(key, hooks, hook_data): + """Dipatches a hook dictionary on a given peice of data.""" + + hooks = hooks or dict() + + if key in hooks: + try: + return hooks.get(key).__call__(hook_data) or hook_data + + except Exception, why: + warnings.warn(str(why)) + + return hook_data diff --git a/requests/models.py b/requests/models.py index a5b641d1..0d7b67cb 100644 --- a/requests/models.py +++ b/requests/models.py @@ -20,6 +20,7 @@ from .monkeys import Request as _Request, HTTPBasicAuthHandler, HTTPForcedBasicA from .structures import CaseInsensitiveDict from .packages.poster.encode import multipart_encode from .packages.poster.streaminghttp import register_openers, get_handlers +from .utils import dict_from_cookiejar from .exceptions import RequestException, AuthenticationError, Timeout, URLRequired, InvalidMethod, TooManyRedirects @@ -36,29 +37,39 @@ class Request(object): def __init__(self, url=None, headers=dict(), files=None, method=None, data=dict(), params=dict(), auth=None, cookiejar=None, timeout=None, redirect=False, - allow_redirects=False, proxies=None): + allow_redirects=False, proxies=None, hooks=None): - socket.setdefaulttimeout(timeout) + #: Float describ the timeout of the request. + # (Use socket.setdefaulttimeout() as fallback) + self.timeout = timeout #: Request URL. self.url = url + #: Dictonary of HTTP Headers to attach to the :class:`Request `. self.headers = headers + #: Dictionary of files to multipart upload (``{filename: content}``). self.files = files + #: HTTP Method to use. Available: GET, HEAD, PUT, POST, DELETE. self.method = method + #: Dictionary or byte of request body data to attach to the #: :class:`Request `. self.data = None + #: Dictionary or byte of querystring data to attach to the #: :class:`Request `. self.params = None + #: True if :class:`Request ` is part of a redirect chain (disables history #: and HTTPError storage). self.redirect = redirect + #: Set to True if full redirects are allowed (e.g. re-POST-ing of data at new ``Location``) self.allow_redirects = allow_redirects + # Dictionary mapping protocol to the URL of the proxy (e.g. {'http': 'foo.bar:3128'}) self.proxies = proxies @@ -73,13 +84,19 @@ class Request(object): auth = AuthObject(*auth) if not auth: auth = auth_manager.get_auth(self.url) + #: :class:`AuthObject` to attach to :class:`Request `. self.auth = auth + #: CookieJar to attach to :class:`Request `. self.cookiejar = cookiejar + #: True if Request has been sent. self.sent = False + #: Dictionary of event hook callbacks. + self.hooks = hooks + # Header manipulation and defaults. @@ -127,6 +144,7 @@ class Request(object): if self.auth: if not isinstance(self.auth.handler, (urllib2.AbstractBasicAuthHandler, urllib2.AbstractDigestAuthHandler)): + # TODO: REMOVE THIS COMPLETELY auth_manager.add_password(self.auth.realm, self.url, self.auth.username, self.auth.password) self.auth.handler = self.auth.handler(auth_manager) auth_manager.add_auth(self.url, self.auth) @@ -158,7 +176,7 @@ class Request(object): return opener.open - def _build_response(self, resp): + def _build_response(self, resp, is_error=False): """Build internal :class:`Response ` object from given response.""" def build(resp): @@ -171,9 +189,18 @@ class Request(object): response.read = resp.read response._resp = resp response._close = resp.close + + if self.cookiejar: + + response.cookies = dict_from_cookiejar(self.cookiejar) + + except AttributeError: pass + if is_error: + response.error = resp + response.url = getattr(resp, 'url', None) return response @@ -203,8 +230,7 @@ class Request(object): # Facilitate non-RFC2616-compliant 'location' headers # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource') - if not urlparse(url).netloc: - url = urljoin(r.url, urllib.quote(urllib.unquote(url))) + url = urljoin(r.url, urllib.quote(urllib.unquote(url))) # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4 if r.status_code is 303: @@ -223,6 +249,7 @@ class Request(object): r.history = history self.response = r + self.response.request = self @staticmethod @@ -249,7 +276,7 @@ class Request(object): def _build_url(self): - """Build the actual URL to use""" + """Build the actual URL to use.""" # Support for unicode domain names and paths. scheme, netloc, path, params, query, fragment = urlparse(self.url) @@ -278,6 +305,7 @@ class Request(object): :param anyway: If True, request will be sent, even if it has already been sent. """ + self._checks() success = False @@ -306,13 +334,32 @@ class Request(object): req = _Request(url, data=self._enc_data, method=self.method) if self.headers: - req.headers.update(self.headers) + for k,v in self.headers.iteritems(): + req.add_header(k, v) if not self.sent or anyway: try: opener = self._get_opener() - resp = opener(req) + try: + + resp = opener(req, timeout=self.timeout) + + except TypeError, err: + # timeout argument is new since Python v2.6 + if not 'timeout' in str(err): + raise + + if settings.timeout_fallback: + # fall-back and use global socket timeout (This is not thread-safe!) + old_timeout = socket.getdefaulttimeout() + socket.setdefaulttimeout(self.timeout) + + resp = opener(req) + + if settings.timeout_fallback: + # restore gobal timeout + socket.setdefaulttimeout(old_timeout) if self.cookiejar is not None: self.cookiejar.extract_cookies(resp, req) @@ -322,21 +369,16 @@ class Request(object): if isinstance(why.reason, socket.timeout): why = Timeout(why) - self._build_response(why) - if not self.redirect: - self.response.error = why + self._build_response(why, is_error=True) + else: self._build_response(resp) self.response.ok = True - self.response.cached = False - else: - self.response.cached = True self.sent = self.response.ok - return self.sent @@ -365,12 +407,13 @@ class Response(object): self.ok = False #: Resulting :class:`HTTPError` of request, if one occured. self.error = None - #: True, if the response :attr:`content` is cached locally. - self.cached = False #: A list of :class:`Response ` objects from #: the history of the Request. Any redirect responses will end #: up here. self.history = [] + #: The Request that created the Response. + self.request = None + self.cookies = None def __repr__(self): @@ -480,8 +523,10 @@ class AuthManager(object): def reduce_uri(self, uri, default_port=True): """Accept authority or URI and extract only the authority and path.""" + # note HTTP URLs do not have a userinfo component parts = urllib2.urlparse.urlsplit(uri) + if parts[1]: # URI scheme = parts[0] @@ -492,7 +537,9 @@ class AuthManager(object): scheme = None authority = uri path = '/' + host, port = urllib2.splitport(authority) + if default_port and port is None and scheme is not None: dport = {"http": 80, "https": 443, diff --git a/requests/sessions.py b/requests/sessions.py new file mode 100644 index 00000000..50b09f61 --- /dev/null +++ b/requests/sessions.py @@ -0,0 +1,84 @@ +# -*- coding: utf-8 -*- + +""" +requests.session +~~~~~~~~~~~~~~~ + +This module provides a Session object to manage and persist settings across +requests (cookies, auth, proxies). + +""" + +import cookielib + +from . import api +from .utils import add_dict_to_cookiejar + + + +class Session(object): + """A Requests session.""" + + __attrs__ = ['headers', 'cookies', 'auth', 'timeout', 'proxies', 'hooks'] + + + def __init__(self, **kwargs): + + # Set up a CookieJar to be used by default + self.cookies = cookielib.FileCookieJar() + + # Map args from kwargs to instance-local variables + map(lambda k, v: (k in self.__attrs__) and setattr(self, k, v), + kwargs.iterkeys(), kwargs.itervalues()) + + # Map and wrap requests.api methods + self._map_api_methods() + + + def __repr__(self): + return '' % (id(self)) + + def __enter__(self): + return self + + def __exit__(self, *args): + # print args + pass + + + def _map_api_methods(self): + """Reads each available method from requests.api and decorates + them with a wrapper, which inserts any instance-local attributes + (from __attrs__) that have been set, combining them with **kwargs. + """ + + def pass_args(func): + def wrapper_func(*args, **kwargs): + inst_attrs = dict((k, v) for k, v in self.__dict__.iteritems() + if k in self.__attrs__) + # Combine instance-local values with kwargs values, with + # priority to values in kwargs + kwargs = dict(inst_attrs.items() + kwargs.items()) + + # If a session request has a cookie_dict, inject the + # values into the existing CookieJar instead. + if isinstance(kwargs.get('cookies', None), dict): + kwargs['cookies'] = add_dict_to_cookiejar( + inst_attrs['cookies'], kwargs['cookies'] + ) + + if kwargs.get('headers', None) and inst_attrs.get('headers', None): + kwargs['headers'].update(inst_attrs['headers']) + + return func(*args, **kwargs) + return wrapper_func + + # Map and decorate each function available in requests.api + map(lambda fn: setattr(self, fn, pass_args(getattr(api, fn))), + api.__all__) + + +def session(**kwargs): + """Returns a :class:`Session` for context-managment.""" + + return Session(**kwargs) \ No newline at end of file diff --git a/requests/status_codes.py b/requests/status_codes.py new file mode 100644 index 00000000..e8023142 --- /dev/null +++ b/requests/status_codes.py @@ -0,0 +1,83 @@ +# -*- coding: utf-8 -*- + +from .structures import LookupDict + +_codes = { + + # Informational. + 100: ('continue',), + 101: ('switching_protocols',), + 102: ('processing',), + 103: ('checkpoint',), + 122: ('uri_too_long', 'request_uri_too_long'), + 200: ('ok', 'okay', 'all_ok', 'all_okay', 'all_good', '\\o/'), + 201: ('created',), + 202: ('accepted',), + 203: ('non_authoritative_info', 'non_authoritative_information'), + 204: ('no_content',), + 205: ('reset_content', 'reset'), + 206: ('partial_content', 'partial'), + 207: ('multi_status', 'multiple_status', 'multi_stati', 'multiple_stati'), + 208: ('im_used',), + + # Redirection. + 300: ('multiple_choices',), + 301: ('moved_permanently', 'moved'), + 302: ('found',), + 302: ('see_other', 'other'), + 304: ('not_modified',), + 305: ('use_proxy',), + 306: ('switch_proxy',), + 307: ('temporary_redirect', 'temporary_moved', 'temporary'), + 308: ('resume_incomplete', 'resume'), + + # Client Error. + 400: ('bad_request', 'bad'), + 401: ('unauthorized',), + 402: ('payment_required', 'payment'), + 403: ('forbidden',), + 404: ('not_found',), + 405: ('method_not_allowed', 'not_allowed'), + 406: ('not_acceptable',), + 407: ('proxy_authentication_required', 'proxy_auth', 'proxy_authentication'), + 408: ('request_timeout', 'timeout'), + 409: ('conflict',), + 410: ('gone',), + 411: ('length_required',), + 412: ('precondition_failed', 'precondition'), + 413: ('request_entity_too_large',), + 414: ('request_uri_too_large',), + 415: ('unspported_media_type', 'unspported_media', 'media_type'), + 416: ('requested_range_not_satisfiable', 'requested_range', 'range_not_satisfiable'), + 417: ('expectation_failed',), + 418: ('im_a_teapot', 'teapot', 'i_am_a_teapot'), + 422: ('unprocessable_entity', 'unprocessable'), + 423: ('locked',), + 424: ('failed_depdendency', 'depdendency'), + 425: ('unordered_collection', 'unordered'), + 426: ('upgrade_required', 'upgrade'), + 444: ('no_response', 'none'), + 449: ('retry_with', 'retry'), + 450: ('blocked_by_windows_parental_controls', 'parental_controls'), + 499: ('client_closed_request',), + + # Server Error. + 500: ('internal_server_error', 'server_error'), + 501: ('not_implemented',), + 502: ('bad_gateway',), + 503: ('service_unavailable', 'unavailable'), + 504: ('gateway_timeout',), + 505: ('http_version_not_supported', 'http_version'), + 506: ('variant_also_negotiates',), + 507: ('insufficient_storage',), + 509: ('bandwidth_limit_exceeded', 'bandwidth'), + 510: ('not_extended',), +} + +codes = LookupDict(name='status_codes') + +for (code, titles) in _codes.items(): + for title in titles: + setattr(codes, title, code) + if not title.startswith('\\'): + setattr(codes, title.upper(), code) \ No newline at end of file diff --git a/requests/structures.py b/requests/structures.py index dd5168cf..d068bf9c 100644 --- a/requests/structures.py +++ b/requests/structures.py @@ -45,3 +45,21 @@ class CaseInsensitiveDict(dict): return self[key] else: return default + +class LookupDict(dict): + """Dictionary lookup object.""" + + def __init__(self, name=None): + self.name = name + super(LookupDict, self).__init__() + + def __repr__(self): + return '' % (self.name) + + def __getitem__(self, key): + # We allow fall-through here, so values default to None + + return self.__dict__.get(key, None) + + def get(self, key, default=None): + return self.__dict__.get(key, default) \ No newline at end of file diff --git a/requests/utils.py b/requests/utils.py new file mode 100644 index 00000000..8ac78b4e --- /dev/null +++ b/requests/utils.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- + +""" +requests.utils +~~~~~~~~~~~~~~ + +This module provides utlity functions that are used within Requests +that are also useful for external consumption. + +""" + +import cookielib + + +def dict_from_cookiejar(cookiejar): + """Returns a key/value dictionary from a CookieJar.""" + + cookie_dict = {} + + for _, cookies in cookiejar._cookies.items(): + for _, cookies in cookies.items(): + for cookie in cookies.values(): + # print cookie + cookie_dict[cookie.name] = cookie.value + + return cookie_dict + + +def cookiejar_from_dict(cookie_dict): + """Returns a CookieJar from a key/value dictionary.""" + + # return cookiejar if one was passed in + if isinstance(cookie_dict, cookielib.CookieJar): + return cookie_dict + + # create cookiejar + cj = cookielib.CookieJar() + + cj = add_dict_to_cookiejar(cj, cookie_dict) + + return cj + + +def add_dict_to_cookiejar(cj, cookie_dict): + """Returns a CookieJar from a key/value dictionary.""" + + for k, v in cookie_dict.items(): + + cookie = cookielib.Cookie( + version=0, + name=k, + value=v, + port=None, + port_specified=False, + domain='', + domain_specified=False, + domain_initial_dot=False, + path='/', + path_specified=True, + secure=False, + expires=None, + discard=True, + comment=None, + comment_url=None, + rest={'HttpOnly': None}, + rfc2109=False + ) + + # add cookie to cookiejar + cj.set_cookie(cookie) + + return cj diff --git a/test_requests.py b/test_requests.py index 8c137fad..dd923471 100755 --- a/test_requests.py +++ b/test_requests.py @@ -13,6 +13,7 @@ except ImportError: import requests +from requests.sessions import Session HTTPBIN_URL = 'http://httpbin.org/' @@ -34,6 +35,9 @@ def httpsbin(*suffix): return HTTPSBIN_URL + '/'.join(suffix) +SERVICES = (httpbin, httpsbin) + + class RequestsTestSuite(unittest.TestCase): """Requests test cases.""" @@ -128,62 +132,81 @@ class RequestsTestSuite(unittest.TestCase): self.assertEqual(r.status_code, 200) - def test_AUTH_HTTPS_200_OK_GET(self): - auth = ('user', 'pass') - url = httpsbin('basic-auth', 'user', 'pass') - r = requests.get(url, auth=auth) + def test_AUTH_HTTP_200_OK_GET(self): - self.assertEqual(r.status_code, 200) + for service in SERVICES: - r = requests.get(url) - self.assertEqual(r.status_code, 200) + auth = ('user', 'pass') + url = service('basic-auth', 'user', 'pass') - # reset auto authentication - requests.auth_manager.empty() + r = requests.get(url, auth=auth) + # print r.__dict__ + self.assertEqual(r.status_code, 200) + + + r = requests.get(url) + self.assertEqual(r.status_code, 200) def test_POSTBIN_GET_POST_FILES(self): - url = httpbin('post') - post = requests.post(url).raise_for_status() - post = requests.post(url, data={'some': 'data'}) - self.assertEqual(post.status_code, 200) + for service in SERVICES: - post2 = requests.post(url, files={'some': open('test_requests.py')}) - self.assertEqual(post2.status_code, 200) + url = service('post') + post = requests.post(url).raise_for_status() - post3 = requests.post(url, data='[{"some": "json"}]') - self.assertEqual(post3.status_code, 200) + post = requests.post(url, data={'some': 'data'}) + self.assertEqual(post.status_code, 200) + + post2 = requests.post(url, files={'some': open('test_requests.py')}) + self.assertEqual(post2.status_code, 200) + + post3 = requests.post(url, data='[{"some": "json"}]') + self.assertEqual(post3.status_code, 200) def test_POSTBIN_GET_POST_FILES_WITH_PARAMS(self): - url = httpbin('post') - post = requests.post(url, files={'some': open('test_requests.py')}, data={'some': 'data'}) - self.assertEqual(post.status_code, 200) + for service in SERVICES: + + url = service('post') + post = requests.post(url, + files={'some': open('test_requests.py')}, + data={'some': 'data'}) + + self.assertEqual(post.status_code, 200) def test_POSTBIN_GET_POST_FILES_WITH_HEADERS(self): - url = httpbin('post') + for service in SERVICES: - post2 = requests.post(url, files={'some': open('test_requests.py')}, - headers = {'User-Agent': 'requests-tests'}) + url = service('post') - self.assertEqual(post2.status_code, 200) + post2 = requests.post(url, + files={'some': open('test_requests.py')}, + headers = {'User-Agent': 'requests-tests'}) + + self.assertEqual(post2.status_code, 200) def test_nonzero_evaluation(self): - r = requests.get(httpbin('status', '500')) - self.assertEqual(bool(r), False) - r = requests.get(httpbin('/')) - self.assertEqual(bool(r), True) + for service in SERVICES: + + r = requests.get(service('status', '500')) + self.assertEqual(bool(r), False) + + r = requests.get(service('/')) + self.assertEqual(bool(r), True) def test_request_ok_set(self): - r = requests.get(httpbin('status', '404')) - self.assertEqual(r.ok, False) + + for service in SERVICES: + + r = requests.get(service('status', '404')) + self.assertEqual(r.ok, False) def test_status_raising(self): @@ -221,31 +244,26 @@ class RequestsTestSuite(unittest.TestCase): r.content.decode('ascii') - def test_autoauth(self): - - http_auth = ('user', 'pass') - requests.auth_manager.add_auth('httpbin.org', http_auth) - - r = requests.get(httpbin('basic-auth', 'user', 'pass')) - self.assertEquals(r.status_code, 200) - - def test_unicode_get(self): - url = httpbin('/') + for service in SERVICES: - requests.get(url, params={'foo': u'føø'}) - requests.get(url, params={u'føø': u'føø'}) - requests.get(url, params={'føø': 'føø'}) - requests.get(url, params={'foo': u'foo'}) - requests.get(httpbin('ø'), params={'foo': u'foo'}) + url = service('/') + + requests.get(url, params={'foo': u'føø'}) + requests.get(url, params={u'føø': u'føø'}) + requests.get(url, params={'føø': 'føø'}) + requests.get(url, params={'foo': u'foo'}) + requests.get(service('ø'), params={'foo': u'foo'}) def test_httpauth_recursion(self): + http_auth = ('user', 'BADpass') - r = requests.get(httpbin('basic-auth', 'user', 'pass'), auth=http_auth) - self.assertEquals(r.status_code, 401) + for service in SERVICES: + r = requests.get(service('basic-auth', 'user', 'pass'), auth=http_auth) + self.assertEquals(r.status_code, 401) def test_settings(self): @@ -262,105 +280,196 @@ class RequestsTestSuite(unittest.TestCase): def test_urlencoded_post_data(self): - r = requests.post(httpbin('post'), data=dict(test='fooaowpeuf')) - self.assertEquals(r.status_code, 200) - self.assertEquals(r.headers['content-type'], 'application/json') - self.assertEquals(r.url, httpbin('post')) - rbody = json.loads(r.content) - self.assertEquals(rbody.get('form'), dict(test='fooaowpeuf')) - self.assertEquals(rbody.get('data'), '') + + for service in SERVICES: + + r = requests.post(service('post'), data=dict(test='fooaowpeuf')) + + self.assertEquals(r.status_code, 200) + self.assertEquals(r.headers['content-type'], 'application/json') + self.assertEquals(r.url, service('post')) + + rbody = json.loads(r.content) + + self.assertEquals(rbody.get('form'), dict(test='fooaowpeuf')) + self.assertEquals(rbody.get('data'), '') def test_nonurlencoded_post_data(self): - r = requests.post(httpbin('post'), data='fooaowpeuf') - self.assertEquals(r.status_code, 200) - self.assertEquals(r.headers['content-type'], 'application/json') - self.assertEquals(r.url, httpbin('post')) - rbody = json.loads(r.content) - # Body wasn't valid url encoded data, so the server returns None as - # "form" and the raw body as "data". - self.assertEquals(rbody.get('form'), None) - self.assertEquals(rbody.get('data'), 'fooaowpeuf') + + for service in SERVICES: + + r = requests.post(service('post'), data='fooaowpeuf') + + self.assertEquals(r.status_code, 200) + self.assertEquals(r.headers['content-type'], 'application/json') + self.assertEquals(r.url, service('post')) + + rbody = json.loads(r.content) + # Body wasn't valid url encoded data, so the server returns None as + # "form" and the raw body as "data". + self.assertEquals(rbody.get('form'), None) + self.assertEquals(rbody.get('data'), 'fooaowpeuf') def test_urlencoded_post_querystring(self): - r = requests.post(httpbin('post'), params=dict(test='fooaowpeuf')) - self.assertEquals(r.status_code, 200) - self.assertEquals(r.headers['content-type'], 'application/json') - self.assertEquals(r.url, httpbin('post?test=fooaowpeuf')) - rbody = json.loads(r.content) - self.assertEquals(rbody.get('form'), {}) # No form supplied - self.assertEquals(rbody.get('data'), '') + + for service in SERVICES: + + r = requests.post(service('post'), params=dict(test='fooaowpeuf')) + + self.assertEquals(r.status_code, 200) + self.assertEquals(r.headers['content-type'], 'application/json') + self.assertEquals(r.url, service('post?test=fooaowpeuf')) + + rbody = json.loads(r.content) + self.assertEquals(rbody.get('form'), {}) # No form supplied + self.assertEquals(rbody.get('data'), '') def test_nonurlencoded_post_querystring(self): - r = requests.post(httpbin('post'), params='fooaowpeuf') - self.assertEquals(r.status_code, 200) - self.assertEquals(r.headers['content-type'], 'application/json') - self.assertEquals(r.url, httpbin('post?fooaowpeuf')) - rbody = json.loads(r.content) - self.assertEquals(rbody.get('form'), {}) # No form supplied - self.assertEquals(rbody.get('data'), '') + + for service in SERVICES: + + r = requests.post(service('post'), params='fooaowpeuf') + + self.assertEquals(r.status_code, 200) + self.assertEquals(r.headers['content-type'], 'application/json') + self.assertEquals(r.url, service('post?fooaowpeuf')) + + rbody = json.loads(r.content) + self.assertEquals(rbody.get('form'), {}) # No form supplied + self.assertEquals(rbody.get('data'), '') def test_urlencoded_post_query_and_data(self): - r = requests.post(httpbin('post'), params=dict(test='fooaowpeuf'), - data=dict(test2="foobar")) - self.assertEquals(r.status_code, 200) - self.assertEquals(r.headers['content-type'], 'application/json') - self.assertEquals(r.url, httpbin('post?test=fooaowpeuf')) - rbody = json.loads(r.content) - self.assertEquals(rbody.get('form'), dict(test2='foobar')) - self.assertEquals(rbody.get('data'), '') + + for service in SERVICES: + + r = requests.post( + service('post'), + params=dict(test='fooaowpeuf'), + data=dict(test2="foobar")) + + self.assertEquals(r.status_code, 200) + self.assertEquals(r.headers['content-type'], 'application/json') + self.assertEquals(r.url, service('post?test=fooaowpeuf')) + + rbody = json.loads(r.content) + self.assertEquals(rbody.get('form'), dict(test2='foobar')) + self.assertEquals(rbody.get('data'), '') def test_nonurlencoded_post_query_and_data(self): - r = requests.post(httpbin('post'), params='fooaowpeuf', - data="foobar") - self.assertEquals(r.status_code, 200) - self.assertEquals(r.headers['content-type'], 'application/json') - self.assertEquals(r.url, httpbin('post?fooaowpeuf')) - rbody = json.loads(r.content) - self.assertEquals(rbody.get('form'), None) - self.assertEquals(rbody.get('data'), 'foobar') + + for service in SERVICES: + + r = requests.post(service('post'), + params='fooaowpeuf', data="foobar") + + self.assertEquals(r.status_code, 200) + self.assertEquals(r.headers['content-type'], 'application/json') + self.assertEquals(r.url, service('post?fooaowpeuf')) + + rbody = json.loads(r.content) + + self.assertEquals(rbody.get('form'), None) + self.assertEquals(rbody.get('data'), 'foobar') + def test_idna(self): r = requests.get(u'http://➡.ws/httpbin') assert 'httpbin' in r.url + def test_urlencoded_get_query_multivalued_param(self): - r = requests.get(httpbin('get'), params=dict(test=['foo','baz'])) - self.assertEquals(r.status_code, 200) - self.assertEquals(r.url, httpbin('get?test=foo&test=baz')) + + for service in SERVICES: + + r = requests.get(service('get'), params=dict(test=['foo','baz'])) + self.assertEquals(r.status_code, 200) + self.assertEquals(r.url, service('get?test=foo&test=baz')) + def test_urlencoded_post_querystring_multivalued(self): - r = requests.post(httpbin('post'), params=dict(test=['foo','baz'])) - self.assertEquals(r.status_code, 200) - self.assertEquals(r.headers['content-type'], 'application/json') - self.assertEquals(r.url, httpbin('post?test=foo&test=baz')) - rbody = json.loads(r.content) - self.assertEquals(rbody.get('form'), {}) # No form supplied - self.assertEquals(rbody.get('data'), '') + + for service in SERVICES: + + r = requests.post(service('post'), params=dict(test=['foo','baz'])) + self.assertEquals(r.status_code, 200) + self.assertEquals(r.headers['content-type'], 'application/json') + self.assertEquals(r.url, service('post?test=foo&test=baz')) + + rbody = json.loads(r.content) + self.assertEquals(rbody.get('form'), {}) # No form supplied + self.assertEquals(rbody.get('data'), '') + def test_urlencoded_post_query_multivalued_and_data(self): - r = requests.post(httpbin('post'), params=dict(test=['foo','baz']), - data=dict(test2="foobar",test3=['foo','baz'])) - self.assertEquals(r.status_code, 200) - self.assertEquals(r.headers['content-type'], 'application/json') - self.assertEquals(r.url, httpbin('post?test=foo&test=baz')) - rbody = json.loads(r.content) - self.assertEquals(rbody.get('form'), dict(test2='foobar',test3='foo')) - self.assertEquals(rbody.get('data'), '') + + for service in SERVICES: + + r = requests.post( + service('post'), + params=dict(test=['foo','baz']), + data=dict(test2="foobar",test3=['foo','baz'])) + + self.assertEquals(r.status_code, 200) + self.assertEquals(r.headers['content-type'], 'application/json') + self.assertEquals(r.url, service('post?test=foo&test=baz')) + rbody = json.loads(r.content) + self.assertEquals(rbody.get('form'), dict(test2='foobar',test3='foo')) + self.assertEquals(rbody.get('data'), '') def test_redirect_history(self): - r = requests.get(httpbin('redirect', '3')) - self.assertEquals(r.status_code, 200) - self.assertEquals(len(r.history), 3) - r = requests.get(httpsbin('redirect', '3')) - self.assertEquals(r.status_code, 200) - self.assertEquals(len(r.history), 3) + for service in SERVICES: + + r = requests.get(service('redirect', '3')) + self.assertEquals(r.status_code, 200) + self.assertEquals(len(r.history), 3) + + + def test_relative_redirect_history(self): + + for service in SERVICES: + + r = requests.get(service('relative-redirect', '3')) + self.assertEquals(r.status_code, 200) + self.assertEquals(len(r.history), 3) + + + def test_session_HTTP_200_OK_GET(self): + + s = Session() + r = s.get(httpbin('/')) + self.assertEqual(r.status_code, 200) + + + def test_session_HTTPS_200_OK_GET(self): + + s = Session() + r = s.get(httpsbin('/')) + self.assertEqual(r.status_code, 200) + + + def test_session_persistent_headers(self): + + heads = {'User-agent': 'Mozilla/5.0'} + + s = Session() + s.headers = heads + # Make 2 requests from Session object, should send header both times + r1 = s.get(httpbin('user-agent')) + + assert heads['User-agent'] in r1.content + r2 = s.get(httpbin('user-agent')) + + assert heads['User-agent'] in r2.content + self.assertEqual(r2.status_code, 200) + + if __name__ == '__main__': unittest.main()