Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSONResponse #599

Merged
merged 2 commits into from
Oct 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion aiohttp/web_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
from .streams import EOF_MARKER


__all__ = ('ContentCoding', 'Request', 'StreamResponse', 'Response')
__all__ = (
'ContentCoding', 'Request', 'StreamResponse', 'Response', 'JSONResponse'
)


sentinel = object()
Expand Down Expand Up @@ -810,3 +812,18 @@ def write_eof(self):
if body is not None:
self.write(body)
yield from super().write_eof()


class JSONResponse(Response):

def __init__(self, data=sentinel, *, text=None, body=None, status=200,
reason=None, headers=None, content_type='application/json',
dumps=json.dumps):
if data is not sentinel and (text or body):
raise ValueError(
'only one of data, text, or body should be specified'
)
text = text or dumps(data)
content_type = content_type or self.content_type
super().__init__(text=text, body=body, status=status, reason=reason,
content_type=content_type)
48 changes: 47 additions & 1 deletion tests/test_web_response.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import datetime
import json
import pytest
import re
from unittest import mock
from aiohttp import hdrs, signals
from aiohttp.multidict import CIMultiDict
from aiohttp.web import ContentCoding, Request, StreamResponse, Response
from aiohttp.web import (
ContentCoding, Request, StreamResponse, Response, JSONResponse
)
from aiohttp.protocol import HttpVersion, HttpVersion11, HttpVersion10
from aiohttp.protocol import RawRequestMessage

Expand Down Expand Up @@ -836,3 +839,46 @@ def test_text_with_empty_payload():
resp = Response(status=200)
assert resp.body is None
assert resp.text is None


class TestJSONResponse:

def test_content_type_is_application_json_by_default(self):
resp = JSONResponse('')
assert 'application/json' == resp.content_type

def test_passing_text_only(self):
resp = JSONResponse(text=json.dumps('jaysawn'))
assert resp.text == json.dumps('jaysawn')

def test_data_and_text_raises_value_error(self):
with pytest.raises(ValueError) as excinfo:
JSONResponse(data='foo', text='bar')
expected_message = (
'only one of data, text, or body should be specified'
)
assert expected_message == excinfo.value.args[0]

def test_data_and_body_raises_value_error(self):
with pytest.raises(ValueError) as excinfo:
JSONResponse(data='foo', body=b'bar')
expected_message = (
'only one of data, text, or body should be specified'
)
assert expected_message == excinfo.value.args[0]

def test_text_is_json_encoded(self):
resp = JSONResponse({'foo': 42})
assert json.dumps({'foo': 42}) == resp.text

def test_content_type_is_overrideable(self):
resp = JSONResponse({'foo': 42},
content_type='application/vnd.json+api')
assert 'application/vnd.json+api' == resp.content_type

def test_content_type_is_overrideable_as_class_var(self):
class MyJSONResponse(JSONResponse):
content_type = 'application/vnd.json+api'

resp = MyJSONResponse('jaysawhn')
assert 'application/vnd.json+api' == resp.content_type