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
Provenance
- Commit
428fc9c- Tree
ca96b75- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
d04f31b
2 files changed+56 added−73 removed
modified:requests/models.py+55 added-53 removed
260 unmodified lines261 262 r.history = history263 264 self.response = r265-266- try:267- r.raise_for_status()268- self.response.ok = True269- except HTTPError:270- self.response.ok = False271-265 self.response.request = self266 267 268 @staticmethod7 unmodified lines276 Otherwise, assumes the data is already encoded appropriately, and277 returns it twice.278 """279 280+ if hasattr(data, '__iter__'):281+ data = dict(data)282+283 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 lines293 294 def _build_url(self):295 """Build the actual URL to use."""296 297+ if not self.url:298+ raise URLRequired()299+300 # Support for unicode domain names and paths.301 scheme, netloc, path, params, query, fragment = urlparse(self.url)302+303+ if not scheme:304+ raise ValueError()305+306 netloc = netloc.encode('idna')307+308 if isinstance(path, unicode):309 path = path.encode('utf-8')310+311 path = urllib.quote(urllib.unquote(path))312 self.url = str(urlunparse([ scheme, netloc, path, params, query, fragment ]))313 314 if self._enc_params:15 unmodified lines330 :param anyway: If True, request will be sent, even if it has331 already been sent.332 """333 329- # Some people...330- if not self.url:331- raise URLRequired332-334 # Logging335 if self.config.get('verbose'):336 self.config.get('verbose').write('%s %s %s\n' % (337 datetime.now().isoformat(), self.method, self.url10 unmodified lines348 349 # Multi-part file uploads.350 if self.files:351 if not isinstance(self.data, basestring):352+353+ try:351- fields = self.data.copy()354+ fields = self.data.copy()355+ except AttributeError:356+ fields = dict(self.data)357+358 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 lines384 conn = self._poolmanager.connection_from_url(url)385 386 if not self.sent or anyway:387 382- try:383- if self.cookies:388+ if self.cookies:389 385- # 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:392 388- # 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)397 393- # 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()400 396- # Attach Cookie header to request.397- self.headers['Cookie'] = cookie_header401+ # Attach Cookie header to request.402+ self.headers['Cookie'] = cookie_header403 399- # 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+ )415 411- # resp = {}416+ self._build_response(r)417 413-414- except ArithmeticError:415- pass416-417- else:418- self._build_response(r)419- self.response.ok = True420-421- # self.sent = self.response.ok422-418 # Response manipulation hook.419 self.response = dispatch_hook('response', self.hooks, self.response)420 421 # Post-request hook.28 unmodified lines450 451 #: Final URL location of Response.452 self.url = None453 459- #: True if no :attr:`error` occurred.460- self.ok = False461-454 #: Resulting :class:`HTTPError` of request, if one occurred.455 self.error = None456 457 #: A list of :class:`Response <Response>` objects from15 unmodified lines473 return '<Response [%s]>' % (self.status_code)474 475 def __nonzero__(self):476 """Returns true if :attr:`status_code` is 'OK'."""477+ return self.ok478 479+ @property480+ def ok(self):481 try:482 self.raise_for_status()483 except HTTPError:484 return False490- finally:491- return True485+ return True486 487+488 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+ )498 499 def generate():500 while 1:501 chunk = self.raw.read(chunk_size)502 if not chunk:503 break504 yield chunk505 self._content_consumed = True506+507 gen = generate()508+509 if 'gzip' in self.headers.get('content-encoding', ''):510 gen = stream_decode_gzip(gen)511+512 if decode_unicode is None:513 decode_unicode = self.config.get('decode_unicode')514+515 if decode_unicode:516 gen = stream_decode_response_unicode(gen, self)517+518 return gen519 520+521 @property522 def content(self):523 """Content of the response, in bytes or unicode524 (if available).43 unmodified linesmodified:test_requests.py+1 added-20 removed
232 unmodified lines233 234 for service in SERVICES:235 236 r = requests.get(service('status', '404'))237+ print r.status_code238 self.assertEqual(r.ok, False)239 240 241 def test_status_raising(self):4 unmodified lines246 self.assertFalse(r.error)247 r.raise_for_status()248 249 249- def test_cookie_jar(self):250-251- jar = cookielib.CookieJar()252- self.assertFalse(jar)253-254- url = httpbin('cookies', 'set', 'requests_cookie', 'awesome')255- r = requests.get(url, cookies=jar)256- self.assertTrue(jar)257-258- 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)264-265- r = requests.get(httpbin('cookies'), cookies=jar)266- self.assertTrue('awesome' in r.content)267-268-250 def test_decompress_gzip(self):251 252 r = requests.get(httpbin('gzip'))253 r.content.decode('ascii')261 unmodified lines