Skip to content

Commit

Permalink
Use named aliases instead of integers for status codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dashea committed May 18, 2015
1 parent dda2927 commit b3bab49
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions requests_file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from requests.adapters import BaseAdapter
from requests.compat import urlparse, unquote
from requests import Response
from requests import Response, codes
import errno
import os
import stat
Expand Down Expand Up @@ -55,11 +55,11 @@ def send(self, request, **kwargs):
resp.raw.release_conn = resp.raw.close
except IOError as e:
if e.errno == errno.EACCES:
resp.status_code = 403
resp.status_code = codes.forbidden
elif e.errno == errno.ENOENT:
resp.status_code = 404
resp.status_code = codes.not_found
else:
resp.status_code = 400
resp.status_code = codes.bad_request

# Wrap the error message in a file-like object
# The error message will be localized, try to convert the string
Expand All @@ -71,7 +71,7 @@ def send(self, request, **kwargs):
# Add release_conn to the BytesIO object
resp.raw.release_conn = resp.raw.close
else:
resp.status_code = 200
resp.status_code = codes.ok

# If it's a regular file, set the Content-Length
resp_stat = os.fstat(resp.raw.fileno())
Expand Down
16 changes: 8 additions & 8 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ def test_fetch_regular(self):
testdata = f.read()
response = self._session.get("file://%s" % os.path.abspath(__file__))

self.assertEqual(response.status_code, 200)
self.assertEqual(response.status_code, requests.codes.ok)
self.assertEqual(response.headers['Content-Length'], len(testdata))
self.assertEqual(response.content, testdata)

def test_fetch_missing(self):
# Fetch a file that (hopefully) doesn't exist, look for a 404
response = self._session.get("file:///no/such/path")
self.assertEqual(response.status_code, 404)
self.assertEqual(response.status_code, requests.codes.not_found)
self.assertTrue(response.text)

def test_fetch_no_access(self):
Expand All @@ -34,7 +34,7 @@ def test_fetch_no_access(self):
os.chmod(tmp.name, 0)
response = self._session.get("file://%s" % os.path.abspath(tmp.name))

self.assertEqual(response.status_code, 403)
self.assertEqual(response.status_code, requests.codes.forbidden)
self.assertTrue(response.text)

def test_fetch_missing_localized(self):
Expand All @@ -45,7 +45,7 @@ def test_fetch_missing_localized(self):
try:
locale.setlocale(locale.LC_MESSAGES, 'ru_RU.UTF-8')
response = self._session.get("file:///no/such/path")
self.assertEqual(response.status_code, 404)
self.assertEqual(response.status_code, requests.codes.not_found)
self.assertTrue(response.text)
finally:
locale.setlocale(locale.LC_MESSAGES, saved_locale)
Expand All @@ -55,7 +55,7 @@ def test_head(self):
testlen = os.stat(__file__).st_size
response = self._session.head("file://%s" % os.path.abspath(__file__))

self.assertEqual(response.status_code, 200)
self.assertEqual(response.status_code, requests.codes.ok)
self.assertEqual(response.headers['Content-Length'], testlen)

def test_fetch_post(self):
Expand Down Expand Up @@ -85,20 +85,20 @@ def test_funny_names(self):
space_file.write(testdata)
space_file.flush()
response = self._session.get("file://%s/spa%%20ces" % tmpdir)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.status_code, requests.codes.ok)
self.assertEqual(response.content, testdata)

with open(os.path.join(tmpdir, 'per%cent'), "w+b") as percent_file:
percent_file.write(testdata)
percent_file.flush()
response = self._session.get("file://%s/per%%25cent" % tmpdir)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.status_code, requests.codes.ok)
self.assertEqual(response.content, testdata)

# percent-encoded directory separators should be rejected
with open(os.path.join(tmpdir, 'badname'), "w+b") as bad_file:
response = self._session.get("file://%s%%2Fbadname" % tmpdir)
self.assertEqual(response.status_code, 404)
self.assertEqual(response.status_code, requests.codes.not_found)

finally:
shutil.rmtree(tmpdir)
Expand Down

0 comments on commit b3bab49

Please sign in to comment.