public void ExceptionFilterTest_BusinessError() { using (var server = new HttpServerWrapper()) using (HttpClient client = new HttpClientWrapper()) { var exceptionFilter = new ExceptionFilter(); exceptionFilter.Exception += (sender, e) => { e.Handled = true; e.ReturnCode = "7533967"; e.Message = "中毒太深"; }; server.Configuration.Filters.Add(exceptionFilter); try { client.PostJson<object, object>( "/api/ApiServerTest/BusinessErrorTest", null, null ); Assert.Fail("Did not throw expected exception InvocationNotAcceptableException."); } catch (InvocationNotAcceptableException ex) { Assert.AreEqual("中毒太深", ex.Message); Assert.AreEqual("7533967", ex.ErrorCode); } } }
public void ModelStateFilterTest_InvalidModel() { using (var server = new HttpServerWrapper()) using (HttpClient client = new HttpClientWrapper()) { server.Configuration.Filters.Add(new ModelStateFilter()); try { client.PostJson<InvalidModelRequest, object>( "/api/ApiServerTest/InvalidModelTest", () => { return new InvalidModelRequest { UserId = null, BackyardId = null, CatSubIds = null, }; }, null ); Assert.Fail("Did not throw expected exception BadInvocationException."); } catch (InvalidModelException ex) { Assert.IsNotNull(ex.ModelState); Assert.AreEqual("The UserId field is required.", ex.ModelState["request.UserId"][0]); Assert.AreEqual("The BackyardId field is required.", ex.ModelState["request.BackyardId"][0]); Assert.AreEqual("The CatSubIds field is required.", ex.ModelState["request.CatSubIds"][0]); } } }
public void ExceptionFilterTest_ProgramError() { using (var server = new HttpServerWrapper()) using (HttpClient client = new HttpClientWrapper()) { server.Configuration.Filters.Add(new ExceptionFilter()); try { client.PostJson<object, object>( "/api/ApiServerTest/ProgramErrorTest", null, null ); Assert.Fail("Did not throw expected exception HttpServiceException."); } catch (HttpServiceException ex) { Assert.AreEqual("模擬非商業邏輯錯誤。", ex.ExceptionMessage); Assert.AreEqual(typeof(InvalidOperationException).FullName, ex.ExceptionType); } } }