Skip to content
Myr

PASS ALL TEH TESTS

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kenneth Reitz · committed by Kenneth Reitz · parent d04f31b · Browse files · 2 files+56 added−73 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+56 added−73 removed

  • other:+56 added−73 removed

2 files+56 added−73 removed

modified:requests/models.py+55 added-53 removed
260 unmodified lines
261262 r.history = history263264 self.response = r265266 try:267 r.raise_for_status()268 self.response.ok = True269 except HTTPError:270 self.response.ok = False271265 self.response.request = self266267268 @staticmethod
7 unmodified lines
276 Otherwise, assumes the data is already encoded appropriately, and277 returns it twice.278 """279280 if hasattr(data, '__iter__'):281 data = dict(data)282283 if hasattr(data, 'items'):284 result = []285 for k, vs in data.items():286 for v in isinstance(vs, list) and vs or [vs]:
6 unmodified lines
293294 def _build_url(self):295 """Build the actual URL to use."""296297 if not self.url:298 raise URLRequired()299300 # Support for unicode domain names and paths.301 scheme, netloc, path, params, query, fragment = urlparse(self.url)302303 if not scheme:304 raise ValueError()305306 netloc = netloc.encode('idna')307308 if isinstance(path, unicode):309 path = path.encode('utf-8')310311 path = urllib.quote(urllib.unquote(path))312 self.url = str(urlunparse([ scheme, netloc, path, params, query, fragment ]))313314 if self._enc_params:
15 unmodified lines
330 :param anyway: If True, request will be sent, even if it has331 already been sent.332 """333329 # Some people...330 if not self.url:331 raise URLRequired332334 # Logging335 if self.config.get('verbose'):336 self.config.get('verbose').write('%s %s %s\n' % (337 datetime.now().isoformat(), self.method, self.url
10 unmodified lines
348349 # Multi-part file uploads.350 if self.files:351 if not isinstance(self.data, basestring):352353 try:351 fields = self.data.copy()354 fields = self.data.copy()355 except AttributeError:356 fields = dict(self.data)357358 for (k, v) in self.files.items():359 fields.update({k: (k, v.read())})360 (body, content_type) = encode_multipart_formdata(fields)361 else:
22 unmodified lines
384 conn = self._poolmanager.connection_from_url(url)385386 if not self.sent or anyway:387382 try:383 if self.cookies:388 if self.cookies:389385 # Skip if 'cookie' header is explicitly set.386 if 'cookie' not in self.headers:390 # Skip if 'cookie' header is explicitly set.391 if 'cookie' not in self.headers:392388 # Simple cookie with our dict.389 # TODO: Multi-value headers.390 c = SimpleCookie()391 c.load(self.cookies)393 # Simple cookie with our dict.394 # TODO: Multi-value headers.395 c = SimpleCookie()396 c.load(self.cookies)397393 # Turn it into a header.394 cookie_header = c.output(header='').strip()398 # Turn it into a header.399 cookie_header = c.output(header='').strip()400396 # Attach Cookie header to request.397 self.headers['Cookie'] = cookie_header401 # Attach Cookie header to request.402 self.headers['Cookie'] = cookie_header403399 # Create the connection.400 r = conn.urlopen(401 method=self.method,402 url=url,403 body=body,404 headers=self.headers,405 redirect=False,406 assert_same_host=False,407 preload_content=False,408 decode_content=False409 )404 # Create the connection.405 r = conn.urlopen(406 method=self.method,407 url=url,408 body=body,409 headers=self.headers,410 redirect=False,411 assert_same_host=False,412 preload_content=False,413 decode_content=False414 )415411 # resp = {}416 self._build_response(r)417413414 except ArithmeticError:415 pass416417 else:418 self._build_response(r)419 self.response.ok = True420421 # self.sent = self.response.ok422418 # Response manipulation hook.419 self.response = dispatch_hook('response', self.hooks, self.response)420421 # Post-request hook.
28 unmodified lines
450451 #: Final URL location of Response.452 self.url = None453459 #: True if no :attr:`error` occurred.460 self.ok = False461454 #: Resulting :class:`HTTPError` of request, if one occurred.455 self.error = None456457 #: A list of :class:`Response <Response>` objects from
15 unmodified lines
473 return '<Response [%s]>' % (self.status_code)474475 def __nonzero__(self):476 """Returns true if :attr:`status_code` is 'OK'."""477 return self.ok478479 @property480 def ok(self):481 try:482 self.raise_for_status()483 except HTTPError:484 return False490 finally:491 return True485 return True486487488 def iter_content(self, chunk_size=10 * 1024, decode_unicode=None):489 """Iterates over the response data. This avoids reading the content490 at once into memory for large responses. The chunk size is the number491 of bytes it should read into memory. This is not necessarily the492 length of each item returned as decoding can take place.493 """494 if self._content_consumed:500 raise RuntimeError('The content for this response was '501 'already consumed')495 raise RuntimeError(496 'The content for this response was already consumed'497 )498499 def generate():500 while 1:501 chunk = self.raw.read(chunk_size)502 if not chunk:503 break504 yield chunk505 self._content_consumed = True506507 gen = generate()508509 if 'gzip' in self.headers.get('content-encoding', ''):510 gen = stream_decode_gzip(gen)511512 if decode_unicode is None:513 decode_unicode = self.config.get('decode_unicode')514515 if decode_unicode:516 gen = stream_decode_response_unicode(gen, self)517518 return gen519520521 @property522 def content(self):523 """Content of the response, in bytes or unicode524 (if available).
43 unmodified lines
modified:test_requests.py+1 added-20 removed
232 unmodified lines
233234 for service in SERVICES:235236 r = requests.get(service('status', '404'))237 print r.status_code238 self.assertEqual(r.ok, False)239240241 def test_status_raising(self):
4 unmodified lines
246 self.assertFalse(r.error)247 r.raise_for_status()248249249 def test_cookie_jar(self):250251 jar = cookielib.CookieJar()252 self.assertFalse(jar)253254 url = httpbin('cookies', 'set', 'requests_cookie', 'awesome')255 r = requests.get(url, cookies=jar)256 self.assertTrue(jar)257258 cookie_found = False259 for cookie in jar:260 if cookie.name == 'requests_cookie':261 self.assertEquals(cookie.value, 'awesome')262 cookie_found = True263 self.assertTrue(cookie_found)264265 r = requests.get(httpbin('cookies'), cookies=jar)266 self.assertTrue('awesome' in r.content)267268250 def test_decompress_gzip(self):251252 r = requests.get(httpbin('gzip'))253 r.content.decode('ascii')
261 unmodified lines