Skip to content

Commit

Permalink
Merge 83fa288 into 99c492a
Browse files Browse the repository at this point in the history
  • Loading branch information
kgriffs committed Apr 23, 2015
2 parents 99c492a + 83fa288 commit 90bc2a6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
6 changes: 3 additions & 3 deletions doc/api/util.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ Testing
:members:

.. automodule:: falcon.testing
:members: httpnow, rand_string, create_environ
:members: rand_string, create_environ

Miscellaneous
-------------

.. automodule:: falcon.util
:members: deprecated, dt_to_http, http_date_to_dt, to_query_str
.. automodule:: falcon
:members: deprecated, http_now, dt_to_http, http_date_to_dt, to_query_str

.. autoclass:: falcon.util.TimezoneGMT
:members:
6 changes: 5 additions & 1 deletion falcon/testing/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@

import six

from falcon.util import uri
from falcon.util import uri, http_now

# Constants
DEFAULT_HOST = 'falconframework.org'


# NOTE(kgriffs): Alias for backwards-compatibility with Falcon 0.2
httpnow = http_now


def rand_string(min, max):
"""Returns a randomly-generated string, of a random length.
Expand Down
13 changes: 13 additions & 0 deletions falcon/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

__all__ = (
'deprecated',
'http_now',
'dt_to_http',
'http_date_to_dt',
'to_query_str',
Expand All @@ -30,6 +31,7 @@

# PERF(kgriffs): Avoid superfluous namespace lookups
strptime = datetime.datetime.strptime
utcnow = datetime.datetime.utcnow


# NOTE(kgriffs): We don't want our deprecations to be ignored by default,
Expand Down Expand Up @@ -73,6 +75,17 @@ def wrapper(*args, **kwargs):
return decorator


def http_now():
"""Returns the current UTC time as an IMF-fixdate.
Returns:
str: The current UTC time as an IMF-fixdate,
e.g., 'Tue, 15 Nov 1994 12:45:26 GMT'.
"""

return dt_to_http(utcnow())


def dt_to_http(dt):
"""Converts a ``datetime`` instance to an HTTP date string.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ def old_thing():
sys.stderr = old_stderr
self.assertIn(msg, stream.getvalue())

def test_http_now(self):
expected = datetime.utcnow()
actual = falcon.http_date_to_dt(falcon.http_now())

delta = actual - expected
self.assertLessEqual(delta.total_seconds(), 2)

def test_dt_to_http(self):
self.assertEqual(
falcon.dt_to_http(datetime(2013, 4, 4)),
Expand Down Expand Up @@ -286,3 +293,6 @@ def test_none_header_value_in_create_environ(self):
def test_decode_empty_result(self):
body = self.simulate_request('/', decode='utf-8')
self.assertEqual(body, '')

def test_httpnow_alias_for_backwards_compat(self):
self.assertIs(falcon.testing.httpnow, util.http_now)

0 comments on commit 90bc2a6

Please sign in to comment.