10 thoughts on “Integration testing OpenRasta”

  • 1
    scottlittlewood on April 13, 2012 Reply
  • 2
    Matthew Radford on December 6, 2012 Reply

    Nate, I’m trying to create a almost idential test method. However, I’m unable to get anything other than a 415 error from the response in the statuscode property. Do you have any ideas what it could be?[Test, WebInvoke(Method = “POST”, RequestFormat = WebMessageFormat.Json)] public static void POST<t,u>(Uri serviceuri, object content) { using (var host = new InMemoryHost(new Configuration())) { var request = new InMemoryRequest() { Uri = serviceuri, HttpMethod = “POST”};request.Entity.ContentType = MediaType.Json; request.Entity.Headers[“Accept”] = “application/json”; var serializer = new DataContractJsonSerializer(typeof(T), new [] { typeof(AuthenticationStructure) }); serializer.WriteObject(request.Entity.Stream, content); request.Entity.Stream.Seek(0, SeekOrigin.Begin); request.Entity.ContentLength = request.Entity.Stream.Length;//Just a read test, not necessary for the output byte[] readbyte = new byte[(int)request.Entity.ContentLength]; request.Entity.Stream.Read(readbyte, 0, (int)request.Entity.ContentLength); request.Entity.Stream.Seek(0, SeekOrigin.Begin);U readObject = (U)serializer.ReadObject(request.Entity.Stream); request.Entity.Stream.Seek(0, SeekOrigin.Begin);NUnit.Framework.Assert.AreEqual(content, readObject);var response = host.ProcessRequest(request); int statusCode = response.StatusCode;NUnit.Framework.Assert.AreEqual(200, statusCode, string.Format(“Http StatusCode Error: {0}”, statusCode)); } }Thanks.

  • 3
    Nate Taylor on December 6, 2012 Reply

    415 is “Unsupported Media Type” Are you sure that for the route you’re testing that you have it configured to return JSON?

  • 4
    Matthew Radford on December 6, 2012 Reply

    By route, your referring to Uri configured in the resourcespace?This is the call to the POST test:OpenRastaJSONTestMehods.POST<authenticationresult,>(“http://localhost/user”, content);and registered as:ResourceSpace.Has.ResourcesOfType<authenticationresult>() .AtUri(“/user”).HandledBy<userhandler>().AsJsonDataContract();This is the only POST I have handled in the system at the moment via UserHandler.public OperationResult Post(string username, string password, int appid) { try { var response = UserManager.AuthenticateUser(username, password, appid); if (response.IsAuthenticated) return new OperationResult.OK { ResponseResource = response }; else return new OperationResult.OK { ResponseResource = response }; //{ Description = response.ErrorMessage }; } catch (Exception ex) { return new OperationResult.InternalServerError { Title = “Unknown Error”, Description = ex.Message }; } }I have plenty of GETs all of which I have now built Unit Tests for.My predecessor, who started the project, crated a basic aspx test page to check the post functionality works:<%@><%@> <form> Enter a  username:  password:  appid: <input type=”text” /> <input type=”password” /> <input type=”text” /> <input type=”submit” /> <iframe /> </form>It does the job, but a proper test routine would be better. So when I run this from the testpage uriResourceSpace.Has.ResourcesOfType<testpageresource>() .AtUri(“/testpage”).HandledBy<testpagehandler>().RenderedByAspx(“~/Views/DummyView.aspx”);I get the following response displayed which is in the JSON format (I’ve taken data out obviously) oh and I’m not authenticated against this system, so the ErrorMessage is correct and what we/I would expect.{“ErrorMessage”:”User does not have permission to use this application”,”IsAuthenticated”:false,”User”:{“__type”:”AppUser:#Poppy.Models”,”Agency”:null,”AgencyId”:0,”AppUserId”:0,”ScreenName”:”Matt Radford”,”UserAppVersions”:null,”UserClients”:null,”WinLogin”:”#######”}}This is the AuthenticationResult class:[KnownType(typeof(AppUser))] public class AuthenticationResult { public bool IsAuthenticated { get; set; } public string ErrorMessage { get; set; } public IAppUser User { get; set; } }and I created an AuthenticationStructure class to build up a clr object to serialize into json:[DataContract(Name=”AuthenticationStructure”), Serializable, KnownType(typeof(AuthenticationStructure))] public class AuthenticationStructure: Object { [DataMember(Name=”username”, Order=1)] public string Username { get; set; } [DataMember(Name=”password”, Order=2)] public string Password { get; set; } [DataMember(Name=”appid”, Order=2)] public int AppId { get; set; }public override bool Equals(object obj) { // // See the full list of guidelines at // http://go.microsoft.com/fwlink/?LinkID=85237 // and also the guidance for operator== at // http://go.microsoft.com/fwlink/?LinkId=85238 //AuthenticationStructure aut = (AuthenticationStructure)obj;if (GetType() != obj.GetType()) { return false; } if (this.Username == aut.Username && this.Password == aut.Password && this.AppId == aut.AppId) return true; else return false; } }So finally in answer to your question, yes, I’m pretty confident it should return JSON?P.s. since I sent you the message I’ve been playing with adding in ResponseFormat = WebMessageFormat.Json into the WebInvoke attribute.I’ve also just tried specifically setting the ContentType and Accept header in the response as well, not that you did:var response = new InMemoryResponse();response.Entity.ContentType = MediaType.Json; response.Entity.Headers[“Accept”] = “application/json”;response = (InMemoryResponse)host.ProcessRequest(request);I’ve also been reading about configuring endpoint behaviors (behaviours in English ;-)) in the web.config and tried a few additions to that, with no success.Thanks for your reply, really appreciate it, been struggling with this for a couple of days now and need to get past it and onto the next inevitable problem!

  • 5
    Nate Taylor on December 6, 2012 Reply

    Sadly, it’s been several months since I’ve touched OpenRasta, but the 415 error is familiar. I assume you’ve just googled OpenRasta 415? I’ll try to think about it some and see if I can get back to you.

  • 6
    Matthew Radford on December 6, 2012 Reply

    That’s good of you thanks.I’ve googled all sorts of things but not specifically ‘OpenRasta 415’ so that’s the next one to try see if it throws up any more pages. http://stackoverflow.com/questions/5006195/wcf-rest-service-not-accepting-jso… this one seems to have it answered, but doesn’t work for me.I hate giving up and failures not really an option so I’ll keep trying.P.s. you are missing a ‘t’ from your kids guest appearance on your profile blurb.

  • 7
    Nate Taylor on December 6, 2012 Reply

    Here are a couple of possible answers from another blog post(http://blogs.7digital.com/dev/2011/02/02/rest-in-practice-and-openrasta/)*415′s can occur when the handler has a parameter that isn’t accounted for in the IConfigSource, ie when you set up the URI’s.*The 415 media Not Supported is probably because you were not providing a Content-Type HttpHeader of Contact-Type: application/x-www-form-urlencodedSince you’re getting 415 on posts, I would guess it has to do with OR not knowing how to accept your posted object. For example, if you had an object of Person with parametersName AgeAnd you were posting an object with something likeFirstNameBirthdateIt wouldn’t know that you’re trying to send a person object and might return a 415.Finally, OR has a decent amount of logging that gets spit out into the output window (stating what step of the pipeline it’s on etc.) You might check that to see if any errors pop out.

  • 8
    Matthew Radford on December 7, 2012 Reply

    Read and re-read both your and the linked blog, im really still non the wiser what I need to do.I agree with you about the object being passed being the problem and OR (OpenRasta) not knowning which method to select. I only have the 1 method on the Handler which could possibly be for POST and I’ve added in the HTTPOperation attribute to the method because the blog post has done.I appreciated from the work I have done on the GET side the parameter mapping is all down to what OR thnks is the best match, not what the developer might want.This is an extract from the Output window but it doesn’t specify any ‘errors’ listed like I’ve seen before ‘Error(0):8-[2012-12-07 16:48:56Z] Verbose(0) Incoming host request for http://localhost/user8-%5B2012-12-07 16:48:56Z] Verbose(0) Adding communication context data8-[2012-12-07 16:48:56Z] Warning(0) Contributor call for BootstrapperContributor had a null Action.8-[2012-12-07 16:48:56Z] Start(1) Entering PipelineRunner: Executing contributor HttpMethodOverriderContributor.OverrideHttpVerb8-[2012-12-07 16:48:56Z] Stop(1) Exiting PipelineRunner8-[2012-12-07 16:48:56Z] Start(1) Entering PipelineRunner: Executing contributor DigestAuthorizerContributor.ReadCredentials8-[2012-12-07 16:48:56Z] Stop(1) Exiting PipelineRunner8-[2012-12-07 16:48:56Z] Start(1) Entering PipelineRunner: Executing contributor UriDecoratorsContributor.ProcessDecorators8-[2012-12-07 16:48:56Z] Stop(1) Exiting PipelineRunner8-[2012-12-07 16:48:56Z] Start(1) Entering PipelineRunner: Executing contributor ResourceTypeResolverContributor.ResolveResource8-[2012-12-07 16:48:56Z] Stop(1) Exiting PipelineRunner8-[2012-12-07 16:48:56Z] Start(1) Entering PipelineRunner: Executing contributor HandlerResolverContributor.ResolveHandler8-[2012-12-07 16:48:56Z] Stop(1) Exiting PipelineRunner8-[2012-12-07 16:48:56Z] Start(1) Entering PipelineRunner: Executing contributor OperationCreatorContributor.CreateOperations 8-[2012-12-07 16:48:56Z] Verbose(0) Created operation named Get wth signature UserHandler::Get(Int32 appuserid) 8-[2012-12-07 16:48:56Z] Verbose(0) Created operation named Post wth signature UserHandler::Post(String username, String password, Int32 appid)8-[2012-12-07 16:48:56Z] Stop(1) Exiting PipelineRunner8-[2012-12-07 16:48:56Z] Start(1) Entering PipelineRunner: Executing contributor OperationFilterContributor.ProcessOperations 8-[2012-12-07 16:48:56Z] Verbose(0) Found 1 operation(s) with a matching name. 8-[2012-12-07 16:48:56Z] Verbose(0) Found 1 operation(s) with matching [HttpOperation] attribute. 8-[2012-12-07 16:48:56Z] Verbose(0) No resource or no uri name. Not filtering.8-[2012-12-07 16:48:56Z] Stop(1) Exiting PipelineRunner8-[2012-12-07 16:48:56Z] Start(1) Entering PipelineRunner: Executing contributor OperationCodecSelectorContributor.ProcessOperations8-[2012-12-07 16:48:56Z] Stop(1) Exiting PipelineRunner8-[2012-12-07 16:48:56Z] Verbose(0) Pipeline is in RenderNow mode.8-[2012-12-07 16:48:56Z] Start(1) Entering PipelineRunner: Executing contributor OperationResultInvokerContributor.RunOperationResult 8-[2012-12-07 16:48:56Z] Information(0) Executing OperationResult OperationResult: type=RequestMediaTypeUnsupported, statusCode=415.8-[2012-12-07 16:48:56Z] Stop(1) Exiting PipelineRunner8-[2012-12-07 16:48:56Z] Start(1) Entering PipelineRunner: Executing contributor ResponseEntityCodecResolverContributor.FindResponseCodec 8-[2012-12-07 16:48:56Z] Information(0) No response codec was searched for. The response entity is null or a response codec is already set.8-[2012-12-07 16:48:56Z] Stop(1) Exiting PipelineRunner8-[2012-12-07 16:48:56Z] Start(1) Entering PipelineRunner: Executing contributor DigestAuthorizerContributor.WriteCredentialRequest8-[2012-12-07 16:48:56Z] Stop(1) Exiting PipelineRunner8-[2012-12-07 16:48:56Z] Start(1) Entering PipelineRunner: Executing contributor ResponseEntityWriterContributor.WriteResponse 8-[2012-12-07 16:48:56Z] Verbose(0) There was no response entity, not rendering. 8-[2012-12-07 16:48:56Z] Verbose(0) Writing http headers.8-[2012-12-07 16:48:56Z] Stop(1) Exiting PipelineRunner8-[2012-12-07 16:48:56Z] Information(0) Pipeline finished.8-[2012-12-07 16:48:56Z] Verbose(0) Request finished.

  • 9
    Nate Taylor on December 7, 2012 Reply

    At this point, I'd suggest posting your question on stack overflow. There are a lot of people who use OR there and might be able to help you.  Sorry.

  • 10
    Matthew Radford on December 10, 2012 Reply

    Don’t be sorry, you gave me the initial code in the first place, so you’ve already made an excellent contribution.I’ve done just that:http://stackoverflow.com/questions/13763224/openrasta-unit-test-response-http…Thanks again.

Leave a Reply

Your email address will not be published. Required fields are marked *