Ejemplo n.º 1
0
    def test_call_once(self):
        # We define an API call that increments a counter each time the
        # function is called, then invoke it via HTTP. Finally, we verify that
        # the counter is equal to 1, indicating that the function was called
        # the correct number of times.

        @rest.rest_call('POST', '/increment')
        def increment():
            """Increment a counter each time this function is called."""
            self.num_calls += 1

        rest.request_handler(Request(wsgi_mkenv('POST', '/increment')))
        assert self.num_calls == 1
Ejemplo n.º 2
0
    def test_equivalence(self):
        """Calling `api_call` directly should be the same as via http."""

        # First invoke the call over http. This should never raise exceptions.
        self.api_setup()
        req = Request(self.request())
        resp = rest.request_handler(req)
        body = resp.get_data()
        self.api_teardown()

        # Now call it directly.
        try:
            self.api_setup()
            ret = self.api_call()
            assert resp.status_code == 200
            if ret == '':
                assert body == ''
            else:
                assert json.loads(body) == json.loads(ret)
        except rest.APIError, e:
            assert resp.status_code == e.status_code
            assert json.loads(body) == {
                'type': e.__class__.__name__,
                'msg': e.message,
            }
Ejemplo n.º 3
0
    def test_equivalence(self):
        """Calling `api_call` directly should be the same as via http."""

        # First invoke the call over http. This should never raise exceptions.
        self.api_setup()
        req = Request(self.request())
        resp = rest.request_handler(req)
        body = resp.get_data()
        self.api_teardown()

        # Now call it directly.
        try:
            self.api_setup()
            ret = self.api_call()
            if ret is None:
                ret_body, ret_status = '', 200
            elif type(ret) is tuple:
                ret_body, ret_status = ret
            else:
                ret_body, ret_status = ret, 200
            assert resp.status_code == ret_status
            if ret_body == '':
                assert body == ''
            else:
                assert json.loads(body) == json.loads(ret_body)
        except rest.APIError, e:
            assert resp.status_code == e.status_code
            assert json.loads(body) == {'type': e.__class__.__name__,
                                        'msg': e.message,
                                        }
Ejemplo n.º 4
0
    def _do_request(self, data):
        """Make a request to the endpoint with `data` in the body.

        `data` should be a string -- the server will expect valid json, but
        we want to write test cases with invalid input as well.
        """
        req = Request(wsgi_mkenv('POST', '/give-me-an-e', data=data))
        return rest.request_handler(req)
Ejemplo n.º 5
0
    def _do_request(self, data):
        """Make a request to the endpoint with `data` in the body.

        `data` should be a string -- the server will expect valid json, but
        we want to write test cases with invalid input as well.
        """
        req = Request(wsgi_mkenv('POST', '/give-me-an-e', data=data))
        return rest.request_handler(req)
Ejemplo n.º 6
0
def do_request(method, path, data=None):
    """Submit a fake HTTP api call.

    This invokes the request handler with an HTTP request corresponding to
    the given arguments.
    """
    response = rest.request_handler(Request(wsgi_mkenv(method, path, data)))
    rest.local_manager.cleanup()
    return response