Problem mocking HttpResponseMessage.CreateErrorResponse?

3 posts, 0 answers
  1. Andy
    Andy avatar
    2 posts
    Member since:
    Nov 2013

    Posted 04 Apr 2014 Link to this post

    I have a unit test trying to mock the generation of an HttpResponseMessage. It fails with what appears to be an internal JustMock error, but I'm not sure what to try. If anybody can recommend a solution or has seen anything similar, I'd appreciate the help. Thanks!

    Here's the test:

    var actionContext = Mock.Create<HttpActionContext>();
     
    var actionDescriptor = Mock.Create<HttpActionDescriptor>();
    actionContext.ActionDescriptor = actionDescriptor;
    actionDescriptor.Arrange(a => a.GetCustomAttributes<AllowAnonymousAttribute>())
        .Returns(new Collection<AllowAnonymousAttribute>());
     
    var controllerContext = Mock.Create<HttpControllerContext>();
    actionContext.ControllerContext = controllerContext;
     
    var controllerDescriptor = Mock.Create<HttpControllerDescriptor>();
    controllerContext.ControllerDescriptor = controllerDescriptor;
    controllerDescriptor.Arrange(a => a.GetCustomAttributes<AllowAnonymousAttribute>())
        .Returns(new Collection<AllowAnonymousAttribute>());
     
    var requestContext = Mock.Create<HttpRequestContext>();
    controllerContext.RequestContext = requestContext;
     
    var request = Mock.Create<HttpRequestMessage>();
    request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
     
    actionContext.Arrange(x => x.Request).Returns(request);
    var principal = Mock.Create<IPrincipal>();
    requestContext.Principal = principal;
     
    var identity = Mock.Create<IIdentity>();
    principal.Arrange(x => x.Identity).Returns(identity);
    identity.Arrange(x => x.IsAuthenticated).Returns(true);
     
    var attribute = new PermissionsAttribute("TEST_ROLE");
    attribute.OnAuthorization(actionContext);
     
    Assert.IsNotNull(actionContext.Response);
    Assert.AreEqual(HttpStatusCode.Forbidden, actionContext.Response.StatusCode);




    There is an error in "OnAuthorization", which just calls HttpRequestMessage.CreateErrorResponse. Here is the error:




    Test method Tests.Filters.PermissionsAttributeTests.Test_403 threw exception: <br>System.NullReferenceException: Object reference not set to an instance of an object.<br>    at System.Net.Http.Headers.MediaTypeHeaderValue.GetHashCode()<br>   at System.ValueType.GetHashCode()<br>   at System.Collections.Generic.ObjectEqualityComparer`1.GetHashCode(T obj)<br>   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)<br>   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)<br>   at Telerik.JustMock.Core.Behaviors.RecursiveMockingBehavior.Process(Invocation invocation)<br>   at Telerik.JustMock.Core.MocksRepository.DispatchInvocation(Invocation invocation)<br>   at Telerik.JustMock.Core.ProfilerInterceptor.Intercept(RuntimeTypeHandle typeHandle, RuntimeMethodHandle methodHandle, Object[] data)<br>   at System.Net.Http.Headers.MediaTypeHeaderValue.get_MediaType()<br>   at System.Net.Http.Formatting.ParsedMediaTypeHeaderValue..ctor(MediaTypeHeaderValue mediaType)<br>   at System.Net.Http.Formatting.MediaTypeHeaderValueExtensions.IsSubsetOf(MediaTypeHeaderValue mediaType1, MediaTypeHeaderValue mediaType2, ref MediaTypeHeaderValueRange mediaType2Range)<br>   at System.Net.Http.Formatting.MediaTypeHeaderValueExtensions.IsSubsetOf(MediaTypeHeaderValue mediaType1, MediaTypeHeaderValue mediaType2)<br>   at System.Net.Http.Formatting.DefaultContentNegotiator.MatchRequestMediaType(HttpRequestMessage request, MediaTypeFormatter formatter)<br>   at System.Net.Http.Formatting.DefaultContentNegotiator.ComputeFormatterMatches(Type type, HttpRequestMessage request, IEnumerable`1 formatters)<br>   at System.Net.Http.Formatting.DefaultContentNegotiator.Negotiate(Type type, HttpRequestMessage request, IEnumerable`1 formatters)<br>   at System.Web.Http.Results.NegotiatedContentResult`1.Execute(HttpStatusCode statusCode, T content, IContentNegotiator contentNegotiator, HttpRequestMessage request, IEnumerable`1 formatters)<br>   at System.Net.Http.HttpRequestMessageExtensions.CreateResponse(HttpRequestMessage request, HttpStatusCode statusCode, T value, HttpConfiguration configuration)<br>   at System.Net.Http.HttpRequestMessageExtensions.CreateErrorResponse(HttpRequestMessage request, HttpStatusCode statusCode, Func`2 errorCreator)<br>   at System.Net.Http.HttpRequestMessageExtensions.CreateErrorResponse(HttpRequestMessage request, HttpStatusCode statusCode, HttpError error)<br>   at System.Net.Http.HttpRequestMessageExtensions.CreateErrorResponse(HttpRequestMessage request, HttpStatusCode statusCode, String message)<br>   at PermissionsAttribute.HandleUnauthorizedRequest(HttpActionContext actionContext) in PermissionsAttribute.cs: line 48<br>   at System.Web.Http.AuthorizeAttribute.OnAuthorization(HttpActionContext actionContext)<br>   at Tests.Filters.PermissionsAttributeTests.Test_403() in PermissionsAttributeTests.cs: line 91

  2. Andy
    Andy avatar
    2 posts
    Member since:
    Nov 2013

    Posted 04 Apr 2014 in reply to Andy Link to this post

    I forgot an important detail - this worked before upgrading to the latest version of JustMock.
  3. DevCraft R3 2016 release webinar banner
  4. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 09 Apr 2014 Link to this post

    Hi Andy,

    As replied in your support ticket:
     "We have introduced a breaking change in the latest version of JustMock which changes the default mocking behaviour to RecursiveLoose. This shouldn't impact most unit tests, but in your case it changes the test's behaviour and ultimately leads to the exception inside JustMock. I managed to fix it by changing the mocking behavior of HttpRequestMessage to Loose, and by adding an arrangement for controllerContext.Request like this:
     
    var request = Mock.Create<HttpRequestMessage>(Behavior.Loose);
    request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
      
    actionContext.Arrange(x => x.Request).Returns(request);
    controllerContext.Arrange(x => x.Request).Returns(request);
      We've logged a bug about this and it should be fixed for some of our next public releases."

    Thank you for reporting this once again.

    Regards,
    Kaloyan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top