Ejemplo n.º 1
0
    def test_arg_handlersuccess_part_deux(self):
        def handler(*args, **kwargs):
            return dict(arg1=1)

        sp = ServicePublisher()
        sp.add_endpoint(
            Endpoint(
                name="",
                method="POST",
                uri="/location",
                args=([], {"test1": args.String("test", optional=True)}),
                function=handler,
            )
        )
        req = create_req(
            "POST",
            "/location",
            arguments=dict(test="blah"),
            headers={"Content-Type": "application/json"},
            body='{"test1":"foo"}',
        )
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(req._buffer, response_buf(200, '{"arg1": 1}'))
Ejemplo n.º 2
0
    def test_renderer_fail(self):
        def handler():
            return "new_location"

        def renderer(result):
            return Result(
                content='moved',
                content_type='text/html',
                headers={'Location': result},
                http_status=302,
            )

        sp = ServicePublisher()
        sp.add_endpoint(
            Endpoint(name='',
                     method='GET',
                     uri='/location',
                     function=handler,
                     renderer=renderer))
        req = create_req('GET', '/location')
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(
            req._buffer,
            response_buf(302,
                         'moved',
                         content_type='text/html',
                         headers={'Location': 'new_location'}))
Ejemplo n.º 3
0
    def test_multiuri_handler_query_arg(self):
        def handler(user):
            return dict(name=user)

        sp = ServicePublisher()
        sp.add_endpoint(
            Endpoint(name='',
                     method='GET',
                     uris=[
                         '/(?P<user>.*)/profile',
                         '/profiles/(?P<user>.*)',
                     ],
                     args=([args.String('user')], {}),
                     function=handler))
        req = create_req('GET', '/oneuser/profile')
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(req._buffer, response_buf(200, '{"name": "oneuser"}'))

        req = create_req('GET', '/profiles/other_user')
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(req._buffer,
                         response_buf(200, '{"name": "other_user"}'))
Ejemplo n.º 4
0
    def test_multiuri_handler_query_arg(self):
        def handler(user): return dict(name=user)

        sp = ServicePublisher()
        sp.add_endpoint(Endpoint(
            name='',
            method='GET',
            uris=[
                '/(?P<user>.*)/profile',
                '/profiles/(?P<user>.*)',
            ],
            args=([args.String('user')],{}),
            function=handler
        ))
        req = create_req('GET', '/oneuser/profile')
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(req._buffer,response_buf(200, '{"name": "oneuser"}'))

        req = create_req('GET', '/profiles/other_user')
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(req._buffer,response_buf(200, '{"name": "other_user"}'))
Ejemplo n.º 5
0
 def test_arg_handlersuccess_part_tre(self):
     def handler(*args, **kwargs): return dict(arg1=1)
     sp = ServicePublisher()
     sp.add_endpoint(Endpoint(name='', method='POST', uri='/location', args=([args.String('test')],{}), function=handler))
     req = create_req('POST', '/location', arguments=dict(test="blah"), headers={"Content-Type":"application/json"}, body='{"test"="foo"}')
     resp = MockResponse(req, 500)
     result = sp(req, resp.start_response)
     resp.write(result)
     self.assertEqual(req._buffer,response_buf(400, '{"message": "body is not JSON", "code": 400}'))
Ejemplo n.º 6
0
    def test_matchfailure(self):
        def handler(): pass

        sp = ServicePublisher()
        sp.add_endpoint(Endpoint(name='', method='GET', uri='/location', function=handler))
        req = create_req('GET', '/blah')
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(req._buffer,response_buf(404, '{"message": "Not Found", "code": 404}'))
Ejemplo n.º 7
0
    def test_noargs_but_method_handlersuccess(self):
        def handler(): return dict(arg1=1)

        sp = ServicePublisher()
        sp.add_endpoint(Endpoint(name='', method='DELETE', uri='/location', function=handler))
        req = create_req('DELETE', '/location', arguments=dict(_method=['delete']))
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(req._buffer,response_buf(200, '{"arg1": 1}'))
Ejemplo n.º 8
0
    def test_noargs_handlersuccess(self):
        def handler(): return dict(called=1)

        sp = ServicePublisher()
        sp.add_endpoint(Endpoint(name='', method='GET', uri='/location', function=handler))
        req = create_req('GET', '/location')
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(req._buffer,response_buf(200, '{"called": 1}'))
Ejemplo n.º 9
0
    def test_prevent_json_array(self):
        def handler():
            return [1, 2, 3]

        sp = ServicePublisher()
        sp.add_endpoint(Endpoint(name="", method="POST", uri="/location", function=handler))
        req = create_req("POST", "/location")
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(req._buffer, response_buf(500, '{"message": "Internal Server Error", "code": 500}'))
Ejemplo n.º 10
0
    def test_noargs_handlersuccess_empty(self):
        def handler():
            return None

        sp = ServicePublisher()
        sp.add_endpoint(Endpoint(name="", method="GET", uri="/location", function=handler))
        req = create_req("GET", "/location")
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(req._buffer, response_buf(404, '{"message": "Not Found", "code": 404}'))
Ejemplo n.º 11
0
    def test_noargs_handlersuccess(self):
        def handler():
            return dict(called=1)

        sp = ServicePublisher()
        sp.add_endpoint(
            Endpoint(name='', method='GET', uri='/location', function=handler))
        req = create_req('GET', '/location')
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(req._buffer, response_buf(200, '{"called": 1}'))
Ejemplo n.º 12
0
    def test_matchfailure(self):
        def handler():
            pass

        sp = ServicePublisher()
        sp.add_endpoint(
            Endpoint(name='', method='GET', uri='/location', function=handler))
        req = create_req('GET', '/blah')
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(
            req._buffer,
            response_buf(404, '{"message": "Not Found", "code": 404}'))
Ejemplo n.º 13
0
 def test_arg_handlersuccess_nested_json(self):
     def handler(*args, **kwargs): return dict(arg1=1)
     sp = ServicePublisher()
     sp.add_endpoint(Endpoint(name='', method='POST', uri='/location', args=([args.String('test')],{}), function=handler))
     req = create_req(
         'POST',
         '/location',
         arguments=dict(test="blah"),
         headers={"Content-Type":"application/json"},
         body='{"test":"foo", "spam":{"wonderful":true}}'
     )
     resp = MockResponse(req, 200)
     result = sp(req, resp.start_response)
     resp.write(result)
     self.assertEqual(req._buffer,response_buf(200, '{"arg1": 1}'))
Ejemplo n.º 14
0
    def test_renderer_fail(self):
        def handler():
            return "new_location"

        def renderer(result):
            return Result(content="moved", content_type="text/html", headers={"Location": result}, http_status=302)

        sp = ServicePublisher()
        sp.add_endpoint(Endpoint(name="", method="GET", uri="/location", function=handler, renderer=renderer))
        req = create_req("GET", "/location")
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(
            req._buffer, response_buf(302, "moved", content_type="text/html", headers={"Location": "new_location"})
        )
Ejemplo n.º 15
0
    def test_multiuri_handler_noarg(self):
        def handler():
            return dict(called=1)

        sp = ServicePublisher()
        sp.add_endpoint(Endpoint(name="", method="GET", uris=["/location", "/otherlocation"], function=handler))
        req = create_req("GET", "/location")
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(req._buffer, response_buf(200, '{"called": 1}'))

        req = create_req("GET", "/otherlocation")
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(req._buffer, response_buf(200, '{"called": 1}'))
Ejemplo n.º 16
0
    def test_noargs_but_method_handlersuccess(self):
        def handler():
            return dict(arg1=1)

        sp = ServicePublisher()
        sp.add_endpoint(
            Endpoint(name='',
                     method='DELETE',
                     uri='/location',
                     function=handler))
        req = create_req('DELETE',
                         '/location',
                         arguments=dict(_method=['delete']))
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(req._buffer, response_buf(200, '{"arg1": 1}'))
Ejemplo n.º 17
0
    def test_cookie(self):
        def handler(chocolate, hazel):
            return {"chocolate": chocolate, "hazel": hazel}

        sp = ServicePublisher()
        sp.add_endpoint(
            Endpoint(
                name="",
                method="GET",
                uri="/cooookies",
                function=handler,
                args=Args(chocolate=args.Cookie("chocolate"), hazel=args.Cookie("hazel")),
            )
        )
        req = create_req("GET", "/cooookies", headers={"cookie": "chocolate=chip;hazel=nut"})
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)

        self.assertEqual({"chocolate": "chip", "hazel": "nut"}, json.json_decode(result[0]))
Ejemplo n.º 18
0
    def test_prevent_json_array(self):
        def handler():
            return [1, 2, 3]

        sp = ServicePublisher()
        sp.add_endpoint(
            Endpoint(name='', method='POST', uri='/location',
                     function=handler))
        req = create_req(
            'POST',
            '/location',
        )
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(
            req._buffer,
            response_buf(500,
                         '{"message": "Internal Server Error", "code": 500}'))
Ejemplo n.º 19
0
    def test_renderer_fail(self):
        def handler(): return "new_location"
        def renderer(result):
            return Result(
                content='moved',
                content_type='text/html',
                headers={'Location': result },
                http_status=302,
            )

        sp = ServicePublisher()
        sp.add_endpoint(Endpoint(name='', method='GET', uri='/location', function=handler, renderer=renderer))
        req = create_req('GET', '/location')
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(req._buffer,
            response_buf(302, 'moved', content_type='text/html', headers={'Location': 'new_location' })
        )
Ejemplo n.º 20
0
    def test_arg_handlersuccess_nested_json(self):
        def handler(*args, **kwargs):
            return dict(arg1=1)

        sp = ServicePublisher()
        sp.add_endpoint(
            Endpoint(name='',
                     method='POST',
                     uri='/location',
                     args=([args.String('test')], {}),
                     function=handler))
        req = create_req('POST',
                         '/location',
                         arguments=dict(test="blah"),
                         headers={"Content-Type": "application/json"},
                         body='{"test":"foo", "spam":{"wonderful":true}}')
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(req._buffer, response_buf(200, '{"arg1": 1}'))
Ejemplo n.º 21
0
    def test_arg_handlersuccess_part_tre(self):
        def handler(*args, **kwargs):
            return dict(arg1=1)

        sp = ServicePublisher()
        sp.add_endpoint(
            Endpoint(name='',
                     method='POST',
                     uri='/location',
                     args=([args.String('test')], {}),
                     function=handler))
        req = create_req('POST',
                         '/location',
                         arguments=dict(test="blah"),
                         headers={"Content-Type": "application/json"},
                         body='{"test"="foo"}')
        resp = MockResponse(req, 500)
        result = sp(req, resp.start_response)
        resp.write(result)
        self.assertEqual(
            req._buffer,
            response_buf(400, '{"message": "body is not JSON", "code": 400}'))
Ejemplo n.º 22
0
    def test_cookie(self):
        def handler(chocolate, hazel):
            return {'chocolate': chocolate, 'hazel': hazel}

        sp = ServicePublisher()
        sp.add_endpoint(Endpoint(
            name='',
            method='GET',
            uri='/cooookies',
            function=handler,
            args=Args(
                chocolate=args.Cookie('chocolate'),
                hazel=args.Cookie('hazel'),
            )
        ))
        req = create_req('GET', '/cooookies', headers={'cookie':'chocolate=chip;hazel=nut'})
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)

        self.assertEqual(
            {'chocolate': 'chip', 'hazel': 'nut'},
            json.json_decode(result[0])
        )
Ejemplo n.º 23
0
    def test_cookie(self):
        def handler(chocolate, hazel):
            return {'chocolate': chocolate, 'hazel': hazel}

        sp = ServicePublisher()
        sp.add_endpoint(
            Endpoint(name='',
                     method='GET',
                     uri='/cooookies',
                     function=handler,
                     args=Args(
                         chocolate=args.Cookie('chocolate'),
                         hazel=args.Cookie('hazel'),
                     )))
        req = create_req('GET',
                         '/cooookies',
                         headers={'cookie': 'chocolate=chip;hazel=nut'})
        resp = MockResponse(req, 200)
        result = sp(req, resp.start_response)

        self.assertEqual({
            'chocolate': 'chip',
            'hazel': 'nut'
        }, json.json_decode(result[0]))