Ejemplo n.º 1
0
 def test_it_dumps_practical_json_for_machines(self):
     content = ["a", "b", "c"]
     render = renderers.bind(renderers.JSON(), to=lambda _: content)
     request = Request(
         url=URL(path=[u""]),
         headers=Headers([("Accept", ["application/json"])]),
     )
     self.assertNotPretty(content, render(request))
Ejemplo n.º 2
0
 def test_dict_sort_keys(self):
     content = OrderedDict([("foo", "bar"), ("baz", "quux")])
     render = renderers.bind(renderers.JSON(), to=lambda _: content)
     request = Request(
         url=URL(path=[u""]),
         headers=Headers([("Accept", ["*/*"])]),
     )
     self.assertPretty(content, render(request))
Ejemplo n.º 3
0
 def test_it_dumps_pretty_json_for_humans(self):
     content = ["a", "b", "c"]
     render = renderers.bind(renderers.JSON(), to=lambda _: content)
     request = Request(
         url=URL(path=[u""]),
         headers=Headers([("Accept", ["*/*"])]),
     )
     self.assertPretty(content, render(request))
Ejemplo n.º 4
0
 def test_separators(self):
     render = renderers.bind(renderers.JSON(), to=lambda _ : dict(a=1, b=2))
     request = Request(
         url=URL(path=b"/"),
         headers=Headers([("Accept", ["application/json"])]),
     )
     self.assertEqual(
         render(request), Response(
             content=b'{"a":1,"b":2}',
             headers=Headers([("Content-Type", ["application/json"])]),
         ),
     )
Ejemplo n.º 5
0
 def test_separators(self):
     render = renderers.bind(renderers.JSON(), to=lambda _: dict(a=1, b=2))
     request = Request(
         url=URL(path=[u""]),
         headers=Headers([("Accept", ["application/json"])]),
     )
     response = render(request)
     self.assertEqual(
         response, Response(
             content=response.content,
             headers=Headers([("Content-Type", ["application/json"])]),
         ),
     )
     self.assertNotIn(response.content, " ")
     self.assertEqual(json.loads(response.content), {"a": 1, "b": 2})
Ejemplo n.º 6
0
 def add(
     self,
     route,
     fn,
     renderer=False,
     route_name=None,
     methods=(b"GET", b"HEAD"),
     **kw
 ):
     if renderer is False:
         renderer = self.default_renderer
     if renderer is not None:
         fn = bind(renderer=renderer, to=fn)
     self.mapper.add(
         route, fn, route_name=route_name, methods=methods, **kw
     )
Ejemplo n.º 7
0
    def test_customized_dumps(self):
        """
        A customized dumps applies to both pretty and non-pretty responses.

        """

        renderer = renderers.JSON(default=lambda obj: 23)
        render = renderers.bind(renderer, to=lambda _: {"foo": object()})
        request = Request(
            url=URL(path=[u""]),
            headers=Headers([("Accept", ["application/json"])]),
        )
        self.assertEqual(
            render(request), Response(
                content=b'{"foo":23}',
                headers=Headers([("Content-Type", ["application/json"])]),
            ),
        )
        self.assertEqual(
            render(Request(url=URL(path=[u""]))), Response(
                content=b'{\n  "foo": 23\n}',
                headers=Headers([("Content-Type", ["application/json"])]),
            ),
        )
Ejemplo n.º 8
0
 def test_it_dumps_pretty_json_for_humans_no_accept_header(self):
     content = ["a", "b", "c"]
     render = renderers.bind(renderers.JSON(), to=lambda _: content)
     self.assertPretty(content, render(Request(url=URL(path=[u""]))))
Ejemplo n.º 9
0
 def test_UTF8(self):
     render = renderers.bind(renderers.UTF8, to=lambda _: u"שלום")
     self.assertEqual(
         render(self.request), Response(u"שלום".encode("utf-8")),
     )
Ejemplo n.º 10
0
 def test_encoding_errors_by_default(self):
     renderer = renderers.Unicode(encoding="ascii")
     render = renderers.bind(renderer, to=lambda _: u"שלום")
     with self.assertRaises(UnicodeEncodeError):
         render(self.request)
Ejemplo n.º 11
0
 def test_encoding_ignoring_errors(self):
     renderer = renderers.Unicode(encoding="ascii", errors="ignore")
     render = renderers.bind(renderer, to=lambda _: u"שלום")
     self.assertEqual(render(self.request), Response(b""))
Ejemplo n.º 12
0
 def test_it_renders_via_the_given_encoding(self):
     renderer = renderers.Unicode(encoding="utf-8")
     render = renderers.bind(renderer, to=lambda _: u"שלום")
     self.assertEqual(
         render(self.request), Response(u"שלום".encode("utf-8")),
     )
Ejemplo n.º 13
0
 def test_customized_dumps(self):
     renderer = renderers.SimpleJSON(separators=",:")
     render = renderers.bind(renderer, to=lambda _: {"foo": "bar"})
     self.assertEqual(render(self.request), Response(b'{"foo":"bar"}'))
Ejemplo n.º 14
0
 def test_it_dumps_json(self):
     renderer = renderers.SimpleJSON()
     render = renderers.bind(renderer, to=lambda _: {"foo": "bar"})
     self.assertEqual(render(self.request), Response(b'{"foo": "bar"}'))