title: OpenRasta Unit Test Response HTTP Error 415
body: <p>I have inherited a project from my predecessor which uses OpenRasta to host a webservice for my OpenSource colleagues to access for their applications. This is my first foray into OpenRasta
  I've added a lot of additional features all of which is working via manual browser requests, although not 100% reliably but that's perhaps another question later. So I have embarked on creating a set of Unit Tests to test the functionality, which I should be doing anyway. I have successfully created a unit test or two for each GET request all of which are passing, but I am stuck on the test for the single POST I have in the project.</p>
  
  <p>I'm getting a HTTP 415 Error '8-[2012-12-07 11:23:19Z] Information(0) Executing OperationResult OperationResult: type=RequestM ediaTypeUnsupported, statusCode=415.' from the output window. I've taken inspiration from a post by Nate Taylor <a href="http://taylonr.com/integration-testing-openrasta" rel="nofollow">http://taylonr.com/integration-testing-openrasta</a> and have asked him the same question, which he has kindly replied to. I'm still trying to decipher his answer, and perhaps someone might be able to expand and fill in the gaps in my understanding?
  Here is the code which I have been trying:</p>
  
  <pre><code>[Test]
  public void AuthenticateUserJSONPOSTTest()
      {
          object content = new AuthenticationStructure { Username = "matthew.radford", Password = "obviously not going to tell you that bit and will replace with a domain test user when the time comes", AppId = 4 };
  
          OpenRastaJSONTestMehods.POST&lt;AuthenticationResult, AuthenticationStructure&gt;("http://localhost/user", content);
      }
  
  [Test]
      public static void POST&lt;T, U&gt;(string uri, object content)
      {
          const string LocalHost = "http://localhost/";
          if (uri.Contains(LocalHost))
              POST&lt;T, U&gt;(new Uri(uri), content);
          else
              throw new UriFormatException(string.Format("The uri doesn't contain {0}", LocalHost));
      }
      [Test, WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
      public static void POST&lt;T,U&gt;(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 = new InMemoryResponse();
  
              response.Entity.ContentType = MediaType.Json;
              response.Entity.Headers["Accept"] = "application/json";
  
              response = (InMemoryResponse)host.ProcessRequest(request);
              int statusCode = response.StatusCode;
  
  //this is where the test fails because the above response isn't correct and gives the 415 statusCode
              NUnit.Framework.Assert.AreEqual(201, statusCode, string.Format("Http StatusCode Error: {0}", statusCode));
  
              object returnedObject;
              if (response.Entity.ContentLength &gt; 0)
              {
                  response.Entity.Stream.Seek(0, SeekOrigin.Begin);
  
                  //Just a read test, not necessary for the output
                  readbyte = new byte[(int)response.Entity.ContentLength];
                  response.Entity.Stream.Read(readbyte, 0, (int)response.Entity.ContentLength);
                  response.Entity.Stream.Seek(0, SeekOrigin.Begin);
  
                  returnedObject = serializer.ReadObject(response.Entity.Stream);
                  //return returnedObject;
              }
          }
      }
  </code></pre>
  
  <p>Thanks in advance.</p>