Skip to content

Commit

Permalink
StaticRequest content is bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
craigahobbs committed Jul 12, 2021
1 parent f2269b7 commit de1f231
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/chisel/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


# The chisel-doc application's HTML stub
CHISEL_DOC_HTML = '''\
CHISEL_DOC_HTML = b'''\
<!DOCTYPE html>
<html lang="en">
<head>
Expand Down
6 changes: 2 additions & 4 deletions src/chisel/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class StaticRequest(Request):
A static resource request
:param str name: The request name. The default name is the callback function's name.
:param str content: The static content
:param bytes content: The static content
:param str content_type: Optional content type string. If None, the content type is auto-determined.
:param list(tuple) urls: The list of URL method/path tuples. The first value is the HTTP request method (e.g. 'GET')
or None to match any. The second value is the URL path or None to use the default path.
Expand Down Expand Up @@ -222,9 +222,7 @@ def __init__(
if urls is None:
urls = (('GET', f'/{name}'),)
super().__init__(name=name, urls=urls, doc=doc, doc_group=doc_group)

# Encode the content
self.content = content.encode('utf-8')
self.content = content

# Compute the etag
md5 = hashlib.md5()
Expand Down
15 changes: 8 additions & 7 deletions src/tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,11 @@ def test_not_permanent(self):
])
self.assertEqual(response, b'/new')


class TestStatic(TestCase):

def test_init(self):
static = StaticRequest('index.html', '<!DOCTYPE html>')
static = StaticRequest('index.html', b'<!DOCTYPE html>')
self.assertEqual(static.name, 'index.html')
self.assertEqual(static.urls, (('GET', '/index.html'),))
self.assertEqual(static.doc, ('The static resource "index.html"',))
Expand All @@ -354,7 +355,7 @@ def test_init(self):
self.assertEqual(static.etag, 'fe364450e1391215f596d043488f989f')

def test_init_doc(self):
static = StaticRequest('index.html', '<!DOCTYPE html>', doc=('This is the doc!',))
static = StaticRequest('index.html', b'<!DOCTYPE html>', doc=('This is the doc!',))
self.assertEqual(static.name, 'index.html')
self.assertEqual(static.urls, (('GET', '/index.html'),))
self.assertEqual(static.doc, ('This is the doc!',))
Expand All @@ -363,7 +364,7 @@ def test_init_doc(self):
self.assertEqual(static.etag, 'fe364450e1391215f596d043488f989f')

def test_init_urls(self):
static = StaticRequest('index', '<!DOCTYPE html>', urls=(('GET', '/index.html'),))
static = StaticRequest('index', b'<!DOCTYPE html>', urls=(('GET', '/index.html'),))
self.assertEqual(static.name, 'index')
self.assertEqual(static.urls, (('GET', '/index.html'),))
self.assertEqual(static.doc, ('The static resource "index"',))
Expand All @@ -372,7 +373,7 @@ def test_init_urls(self):
self.assertEqual(static.etag, 'fe364450e1391215f596d043488f989f')

def test_content_type(self):
static = StaticRequest('index', '<!DOCTYPE html>', content_type='text/html')
static = StaticRequest('index', b'<!DOCTYPE html>', content_type='text/html')
self.assertEqual(static.name, 'index')
self.assertEqual(static.urls, (('GET', '/index'),))
self.assertEqual(static.doc, ('The static resource "index"',))
Expand All @@ -382,12 +383,12 @@ def test_content_type(self):

def test_content_type_unknown(self):
with self.assertRaises(AssertionError) as cm_exc:
StaticRequest('index.unknown', '<!DOCTYPE html>')
StaticRequest('index.unknown', b'<!DOCTYPE html>')
self.assertEqual(str(cm_exc.exception), 'Unknown content type for static resource "index.unknown"')

def test_request(self):
app = Application()
static = StaticRequest('chisel-doc', '<!DOCTYPE html>', urls=(('GET', '/doc/index.html'),))
static = StaticRequest('chisel-doc', b'<!DOCTYPE html>', urls=(('GET', '/doc/index.html'),))
app.add_request(static)
status, headers, response = app.request('GET', '/doc/index.html')
self.assertEqual(status, '200 OK')
Expand All @@ -396,7 +397,7 @@ def test_request(self):

def test_request_not_modified(self):
app = Application()
static = StaticRequest('chisel-doc', '<!DOCTYPE html>', urls=(('GET', '/doc/index.html'),))
static = StaticRequest('chisel-doc', b'<!DOCTYPE html>', urls=(('GET', '/doc/index.html'),))
app.add_request(static)
status, headers, response = app.request(
'GET',
Expand Down

0 comments on commit de1f231

Please sign in to comment.