Exemple #1
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")),
     )
Exemple #2
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=b"/"),
            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=b"/"))),
            Response(
                content=b'{\n  "foo": 23\n}',
                headers=Headers([("Content-Type", ["application/json"])]),
            ),
        )
Exemple #3
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))
Exemple #4
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=b"/"),
         headers=Headers([("Accept", ["*/*"])]),
     )
     self.assertPretty(content, render(request))
Exemple #5
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=b"/"),
         headers=Headers([("Accept", ["application/json"])]),
     )
     self.assertNotPretty(content, render(request))
Exemple #6
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=b"/"),
         headers=Headers([("Accept", ["*/*"])]),
     )
     self.assertPretty(content, render(request))
Exemple #7
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))
Exemple #8
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))
Exemple #9
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"])]),
         ),
     )
Exemple #10
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"])]),
         ),
     )
Exemple #11
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})
Exemple #12
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)
Exemple #13
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
     )
Exemple #14
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"])]),
            ),
        )
Exemple #15
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=b"/"))))
Exemple #16
0
 def test_UTF8(self):
     render = renderers.bind(renderers.UTF8, to=lambda _: u"שלום")
     self.assertEqual(
         render(self.request),
         Response(u"שלום".encode("utf-8")),
     )
Exemple #17
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)
Exemple #18
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""))
Exemple #19
0
 def test_UTF8(self):
     render = renderers.bind(renderers.UTF8, to=lambda _: u"שלום")
     self.assertEqual(
         render(self.request), Response(u"שלום".encode("utf-8")),
     )
Exemple #20
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"}'))
Exemple #21
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"}'))
Exemple #22
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"}'))
Exemple #23
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")),
     )
Exemple #24
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""]))))
Exemple #25
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""))
Exemple #26
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"}'))
Exemple #27
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)