public void ThrowingOnActionIncludesErrorDetail(bool isLocal, IncludeErrorDetailPolicy includeErrorDetail, bool?customErrors, bool expectErrorDetail) { string controllerName = "Exception"; string requestUrl = String.Format("{0}/{1}/{2}", "http://www.foo.com", controllerName, "ArgumentNull"); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl); request.Properties["MS_IsLocal"] = new Lazy <bool>(() => isLocal); if (customErrors != null) { request.Properties["MS_IncludeErrorDetail"] = new Lazy <bool>(() => !(bool)customErrors); } ScenarioHelper.RunTest( controllerName, "/{action}", request, (response) => { if (expectErrorDetail) { AssertResponseIncludesErrorDetail(response); } else { AssertResponseDoesNotIncludeErrorDetail(response); } }, (config) => { config.IncludeErrorDetailPolicy = includeErrorDetail; } ); }
internal static bool LegacyShouldIncludeErrorDetail(this HttpRequestMessage request) { HttpConfiguration configuration = request.GetConfiguration(); IncludeErrorDetailPolicy includeErrorDetailPolicy = IncludeErrorDetailPolicy.Default; if (configuration != null) { includeErrorDetailPolicy = configuration.IncludeErrorDetailPolicy; } switch (includeErrorDetailPolicy) { case IncludeErrorDetailPolicy.Default: Lazy <bool> includeErrorDetail = request.GetProperty <Lazy <bool> >(HttpPropertyKeys.IncludeErrorDetailKey); if (includeErrorDetail != null) { // If we are on webhost and the user hasn't changed the IncludeErrorDetailPolicy // look up into the Request's property bag else default to LocalOnly. return(includeErrorDetail.Value); } goto case IncludeErrorDetailPolicy.LocalOnly; case IncludeErrorDetailPolicy.LocalOnly: return(request.IsLocal()); case IncludeErrorDetailPolicy.Always: return(true); case IncludeErrorDetailPolicy.Never: default: return(false); } }
public void IncludeErrorDetailSet_OverridesPolicy( bool expected, IncludeErrorDetailPolicy policy ) { // Arrange HttpContextBase webContext = CreateDummyWebContext(); HttpRequestBase webRequest = CreateDummyWebRequest(); using (HttpRequestMessage request = CreateRequest()) using (HttpConfiguration configuration = CreateConfiguration()) { HttpRequestContext context = CreateProductUnderTest( webContext, webRequest, request ); context.Configuration = configuration; configuration.IncludeErrorDetailPolicy = policy; // Act context.IncludeErrorDetail = expected; // Assert bool includeErrorDetail = context.IncludeErrorDetail; Assert.Equal(expected, includeErrorDetail); } }
private void SetErrorDetailPolicy(IncludeErrorDetailPolicy errorDetailPolicy) { if (this.httpConfiguration != null) { this.httpConfiguration.IncludeErrorDetailPolicy = errorDetailPolicy; } }
public void IncludeErrorDetailSet_OverridesPolicy( bool expected, IncludeErrorDetailPolicy policy ) { // Arrange using (RequestContext serviceModelContext = CreateStubServiceModelContext()) using (HttpConfiguration configuration = CreateConfiguration()) using (HttpRequestMessage request = CreateRequest()) { HttpRequestContext context = CreateProductUnderTest( serviceModelContext, configuration, request ); configuration.IncludeErrorDetailPolicy = policy; // Act context.IncludeErrorDetail = expected; // Assert bool includeErrorDetail = context.IncludeErrorDetail; Assert.Equal(expected, includeErrorDetail); } }
public void ThrowingOnActionIncludesErrorDetail(bool isLocal, IncludeErrorDetailPolicy includeErrorDetail, bool? customErrors, bool expectErrorDetail) { string controllerName = "Exception"; string requestUrl = String.Format("{0}/{1}/{2}", "http://www.foo.com", controllerName, "ArgumentNull"); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl); request.Properties[HttpPropertyKeys.IsLocalKey] = new Lazy<bool>(() => isLocal); if (customErrors != null) { request.Properties[HttpPropertyKeys.IncludeErrorDetailKey] = new Lazy<bool>(() => !(bool)customErrors); } ScenarioHelper.RunTest( controllerName, "/{action}", request, (response) => { if (expectErrorDetail) { AssertResponseIncludesErrorDetail(response); } else { AssertResponseDoesNotIncludeErrorDetail(response); } }, (config) => { config.IncludeErrorDetailPolicy = includeErrorDetail; } ); }
public void ThrowingOnActionIncludesErrorDetail(string hostName, IncludeErrorDetailPolicy? includeErrorDetail, bool shouldIncludeErrorDetail) { string controllerName = "Exception"; string requestUrl = String.Format("{0}/{1}/{2}", "http://" + hostName, controllerName, "ArgumentNull"); ScenarioHelper.RunTest( controllerName, "/{action}", new HttpRequestMessage(HttpMethod.Post, requestUrl), (response) => { if (shouldIncludeErrorDetail) { AssertResponseIncludesErrorDetail(response); } else { AssertResponseDoesNotIncludeErrorDetail(response); } }, (config) => { if (includeErrorDetail.HasValue) { config.IncludeErrorDetailPolicy = includeErrorDetail.Value; } } ); }
public static void ConfigureErrorHandling( this HttpConfiguration config, ILog logger, IncludeErrorDetailPolicy includeErrorDetailPolicy = IncludeErrorDetailPolicy.LocalOnly) { config.Services.Add(typeof(IExceptionLogger), new LogProviderExceptionLogger(logger)); config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.LocalOnly; }
private void SetErrorDetailPolicyAndInitialize(IncludeErrorDetailPolicy errorDetailPolicy) { if (this.httpConfiguration != null) { this.httpConfiguration.IncludeErrorDetailPolicy = errorDetailPolicy; this.httpConfiguration.EnsureInitialized(); } }
public void ShouldIncludeErrorDetail(IncludeErrorDetailPolicy errorDetail, bool isLocal, bool includeErrorDetail, bool expectedResult) { // Arrange _config.IncludeErrorDetailPolicy = errorDetail; _request.Properties.Add(HttpPropertyKeys.IsLocalKey, new Lazy <bool>(() => isLocal)); _request.Properties.Add(HttpPropertyKeys.IncludeErrorDetailKey, new Lazy <bool>(() => includeErrorDetail)); // Act bool includeError = _request.ShouldIncludeErrorDetail(); // Assert Assert.Equal(includeError, expectedResult); }
public void ShouldIncludeErrorDetail_WhenContextIsAbsent(IncludeErrorDetailPolicy errorDetail, bool isLocal, bool includeErrorDetail, bool expectedResult) { // Arrange HttpRequestMessage request = CreateRequest(); HttpConfiguration config = CreateAndAddConfiguration(request); config.IncludeErrorDetailPolicy = errorDetail; request.Properties.Add(HttpPropertyKeys.IsLocalKey, new Lazy <bool>(() => isLocal)); request.Properties.Add(HttpPropertyKeys.IncludeErrorDetailKey, new Lazy <bool>(() => includeErrorDetail)); // Act bool includeError = request.ShouldIncludeErrorDetail(); // Assert Assert.Equal(includeError, expectedResult); }
public void CreateErrorResponse_Respects_IncludeErrorDetail(IncludeErrorDetailPolicy errorDetail, bool?isLocal, bool?includeErrorDetail, bool detailIncluded) { HttpConfiguration config = new HttpConfiguration() { IncludeErrorDetailPolicy = errorDetail }; HttpRequestMessage request = new HttpRequestMessage(); request.SetConfiguration(config); if (isLocal.HasValue) { request.Properties.Add(HttpPropertyKeys.IsLocalKey, new Lazy <bool>(() => isLocal.Value)); } if (includeErrorDetail.HasValue) { request.Properties.Add(HttpPropertyKeys.IncludeErrorDetailKey, new Lazy <bool>(() => includeErrorDetail.Value)); } ODataError error = new ODataError() { ErrorCode = "36", Message = "Bad stuff", MessageLanguage = "en-US", InnerError = new ODataInnerError() { Message = "Exception message" } }; HttpResponseMessage response = request.CreateErrorResponse(HttpStatusCode.BadRequest, error); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); ODataError contentError; Assert.True(response.TryGetContentValue <ODataError>(out contentError)); Assert.Equal("36", contentError.ErrorCode); Assert.Equal("Bad stuff", contentError.Message); Assert.Equal("en-US", contentError.MessageLanguage); if (detailIncluded) { Assert.NotNull(contentError.InnerError); Assert.Equal("Exception message", contentError.InnerError.Message); } else { Assert.Null(contentError.InnerError); } }
private void SetErrorDetailPolicyAndInitialize(IncludeErrorDetailPolicy errorDetailPolicy) { if (this.httpConfiguration != null) { this.httpConfiguration.IncludeErrorDetailPolicy = errorDetailPolicy; try { this.httpConfiguration.EnsureInitialized(); } catch (InvalidOperationException ex) { if (!ex.IsRouteConstraintRelatedException()) { throw ex; } } } }
public void CreateODataError_Respects_IncludeErrorDetail(IncludeErrorDetailPolicy errorDetail, bool? isLocal, bool? includeErrorDetail, bool detailIncluded) { HttpConfiguration config = new HttpConfiguration() { IncludeErrorDetailPolicy = errorDetail }; HttpRequestMessage request = new HttpRequestMessage(); request.Properties.Add("MS_HttpConfiguration", config); if (isLocal.HasValue) { request.Properties.Add("MS_IsLocal", new Lazy<bool>(() => isLocal.Value)); } if (includeErrorDetail.HasValue) { request.Properties.Add("MS_IncludeErrorDetail", new Lazy<bool>(() => includeErrorDetail.Value)); } ODataError error = new ODataError() { ErrorCode = "36", Message = "Bad stuff", MessageLanguage = "en-US", InnerError = new ODataInnerError() { Message = "Exception message" } }; HttpResponseMessage response = request.CreateODataErrorResponse(HttpStatusCode.BadRequest, error); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); ODataError contentError; Assert.True(response.TryGetContentValue<ODataError>(out contentError)); Assert.Equal("36", contentError.ErrorCode); Assert.Equal("Bad stuff", contentError.Message); Assert.Equal("en-US", contentError.MessageLanguage); if (detailIncluded) { Assert.NotNull(contentError.InnerError); Assert.Equal("Exception message", contentError.InnerError.Message); } else { Assert.Null(contentError.InnerError); } }
public void IncludeErrorDetailGet_ForPolicy(bool expected, IncludeErrorDetailPolicy policy, bool isLocal) { // Arrange IOwinContext owinContext = CreateStubOwinContext(); using (HttpConfiguration configuration = CreateConfiguration()) using (HttpRequestMessage request = CreateRequest()) { HttpRequestContext context = CreateProductUnderTest(owinContext, request); context.Configuration = configuration; context.IsLocal = isLocal; configuration.IncludeErrorDetailPolicy = policy; // Act bool includeErrorDetail = context.IncludeErrorDetail; // Assert Assert.Equal(expected, includeErrorDetail); } }
/// <summary> /// Sets the error detail policy used in the testing. Default is 'Always'. /// </summary> /// <param name="errorDetailPolicy">Error details policy to use.</param> /// <returns>The same HTTP configuration builder.</returns> public IHttpConfigurationBuilder WithErrorDetailPolicy(IncludeErrorDetailPolicy errorDetailPolicy) { this.SetErrorDetailPolicyAndInitialize(errorDetailPolicy); return this; }
public virtual OwinHttpServer SetErrorPolicy(IncludeErrorDetailPolicy errorDetailPolicy) { this.HttpConfiguration.IncludeErrorDetailPolicy = errorDetailPolicy; return(this); }
public void IncludeErrorDetailGet_ForPolicy(bool expected, IncludeErrorDetailPolicy policy, bool isLocal) { // Arrange using (RequestContext serviceModelContext = CreateStubServiceModelContext()) using (HttpConfiguration configuration = CreateConfiguration()) using (HttpRequestMessage request = CreateRequest()) { HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request); context.IsLocal = isLocal; configuration.IncludeErrorDetailPolicy = policy; // Act bool includeErrorDetail = context.IncludeErrorDetail; // Assert Assert.Equal(expected, includeErrorDetail); } }
public void ShouldIncludeErrorDetail(IncludeErrorDetailPolicy errorDetail, bool isLocal, bool includeErrorDetail, bool expectedResult) { // Arrange _config.IncludeErrorDetailPolicy = errorDetail; _request.Properties.Add(HttpPropertyKeys.IsLocalKey, new Lazy<bool>(() => isLocal)); _request.Properties.Add(HttpPropertyKeys.IncludeErrorDetailKey, new Lazy<bool>(() => includeErrorDetail)); // Act bool includeError = _request.ShouldIncludeErrorDetail(); // Assert Assert.Equal(includeError, expectedResult); }
public void ShouldIncludeErrorDetail_WhenContextIsAbsent(IncludeErrorDetailPolicy errorDetail, bool isLocal, bool includeErrorDetail, bool expectedResult) { // Arrange HttpRequestMessage request = CreateRequest(); HttpConfiguration config = CreateAndAddConfiguration(request); config.IncludeErrorDetailPolicy = errorDetail; request.Properties.Add(HttpPropertyKeys.IsLocalKey, new Lazy<bool>(() => isLocal)); request.Properties.Add(HttpPropertyKeys.IncludeErrorDetailKey, new Lazy<bool>(() => includeErrorDetail)); // Act bool includeError = request.ShouldIncludeErrorDetail(); // Assert Assert.Equal(includeError, expectedResult); }
internal HttpServerConfiguration() { _configurers = new List <Action <IOwinConfiguration> >(); _errorDetailPolicy = IncludeErrorDetailPolicy.Always; }
/// <summary> /// By default Integration Service will set the IncludeErrorDetailPolicy to Always. Use this method to override that behaviour. /// </summary> public HttpServerConfiguration ErrorDetailPolicy(IncludeErrorDetailPolicy policy) { _errorDetailPolicy = policy; return(this); }
public void IncludeErrorDetailSet_OverridesPolicy(bool expected, IncludeErrorDetailPolicy policy) { // Arrange IOwinContext owinContext = CreateStubOwinContext(); using (HttpConfiguration configuration = CreateConfiguration()) using (HttpRequestMessage request = CreateRequest()) { HttpRequestContext context = CreateProductUnderTest(owinContext, request); context.Configuration = configuration; configuration.IncludeErrorDetailPolicy = policy; // Act context.IncludeErrorDetail = expected; // Assert bool includeErrorDetail = context.IncludeErrorDetail; Assert.Equal(expected, includeErrorDetail); } }
/// <summary> /// Sets the error detail policy used in the testing. Default is 'Always'. /// </summary> /// <param name="errorDetailPolicy">Error details policy to use.</param> /// <returns>The same HTTP configuration builder.</returns> public IHttpConfigurationBuilder WithErrorDetailPolicy(IncludeErrorDetailPolicy errorDetailPolicy) { this.SetErrorDetailPolicy(errorDetailPolicy); return(this); }