def test_request_missing_argument(self): "Request does not have a Content-Type set" test_request = DummyRequest() outer_wrap = webapi.url_arguments(["arg1"]) inner_wrap = outer_wrap(self.dummy_render_func) self.assertEqual( str(webapi.ValueError(test_request, "arg1", "Argument is missing.")), inner_wrap(self, test_request))
def test_value_error(self): "Validate ValueError" request = DummyRequest() obj_error = webapi.ValueError(request, "arg_name", "extra description") self.assertEquals(obj_error.error_code, 502) self.assertEquals(obj_error.exception_class, "ValueError") self.assertEquals( obj_error.exception_text, "Invalid value for argument 'arg_name'. extra description") self.assertEquals(request.response_code, 409)
def test_page_len_gt_max_page_len_fail(self): "page_len argument in request is > max_page_len." test_request = DummyRequest() test_request.args["arg1"] = ["test"] test_request.args["page_len"] = ["501"] res = self.dummy_render_func2(test_request) self.assertEqual( res, str( webapi.ValueError(test_request, "page_len", "Argument must be 500 or less.")))
def test_page_lt_zero_fail(self): "page argument in request is < 0." test_request = DummyRequest() test_request.args["arg1"] = ["test"] test_request.args["page"] = ["-1"] res = self.dummy_render_func2(test_request) self.assertEqual( res, str( webapi.ValueError(test_request, "page", "Argument must be 0 or greater.")))
def test_page_invalid_int_fail(self): "page argument in request is not a valid integer." test_request = DummyRequest() test_request.args["arg1"] = ["test"] test_request.args["page"] = ["xyz"] res = self.dummy_render_func2(test_request) self.assertEqual( res, str( webapi.ValueError(test_request, "page", "Argument must be an integer.")))
def test_content_required_arg_missing_optional_flag_false(self): "Request JSON arguments missing required argument." test_request = DummyRequest() test_request.setHeader("Content-Type", "application/json; charset=utf-8") test_request.write(json.dumps({"arg2": 1})) outer_wrap = webapi.json_arguments([("arg1", unicode, False)]) inner_wrap = outer_wrap(self.dummy_render_func) self.assertEqual( str(webapi.ValueError(test_request, "arg1", "Argument is missing.")), inner_wrap(self, test_request))
def test_content_arg_type_mismatch_optional_arg(self): "Request JSON optional arguments contain data which doesn't match expected type." test_request = DummyRequest() test_request.setHeader("Content-Type", "application/json; charset=utf-8") test_request.write(json.dumps({"arg1": 1})) outer_wrap = webapi.json_arguments([("arg1", unicode, True)]) inner_wrap = outer_wrap(self.dummy_render_func) self.assertEqual( str( webapi.ValueError(test_request, "arg1", "Must be of type unicode")), inner_wrap(self, test_request))
def render_POST(self, request): """ Calculate the latency of querying example.com. URL Path Arguments: ---------- timestamp (int) - Client supplied UNIX timestamp. JSON Body Arguments: ---------- client_tz (int) - Client's timezone offset from GMT. client_id (unicode) - Client's unique client ID string. new_client (bool) (optional) - Does magic if true... Returns: ---------- Success: "result" : { "latency" (float) - Time it took to complete example.com request in secs. "client_tz" (int) - Supplied client timezone offset. "client_id" (unicode) - Supplied unique client ID. "new_client" (bool) - Whether magic happened... } Failure: Returns a JSON error dictionary. """ # Make sure simple_auth_key is a good key auth_key = request.args["simple_auth_key"][0] if auth_key != "abc": defer.returnValue( str( webapi.ValueError(request, "simple_auth_key", "Key isn't valid!"))) # Test latency to request example.com start_time = time.time() web_agent = Agent(reactor) resp = yield web_agent.request("GET", "http://example.com") end_time = time.time() # new_client is an optional parameter, # so set a default value if it isn't present # in the JSON arguments new_client = False if request.jsonArgs.has_key("new_client"): new_client = request.jsonArgs["new_client"] # Return a JSON dictionary as the API call result. return_dict = { "result": { "latency": end_time - start_time, "client_tz": request.jsonArgs["client_tz"], "client_id": request.jsonArgs["client_id"], "new_client": request.jsonArgs["new_client"] } } defer.returnValue(json.dumps(return_dict))