我从我的前任那里继承了一个项目,该项目使用 OpenRasta 为我的 OpenSource 同事托管一个 Web 服务,以访问他们的应用程序。这是我第一次涉足 OpenRasta,我添加了许多附加功能,所有这些功能都通过手动浏览器请求工作,虽然不是 100% 可靠,但这可能是稍后的另一个问题。所以我已经着手创建一组单元测试来测试功能,无论如何我都应该这样做。我已经成功地为每个 GET 请求创建了一个或两个单元测试,所有这些请求都通过了,但是我被困在项目中的单个 POST 的测试中。
我收到 HTTP 415 错误“8-[2012-12-07 11:23:19Z] Information(0) Executing OperationResult OperationResult: type=RequestM ediaTypeUnsupported, statusCode=415。” 从输出窗口。我从 Nate Taylor 的帖子http://taylonr.com/integration-testing-openrasta中获得灵感,并问了他同样的问题,他很友好地回答了这个问题。我仍在尝试破译他的答案,也许有人可以扩展并填补我的理解空白?这是我一直在尝试的代码:
[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<AuthenticationResult, AuthenticationStructure>("http://localhost/user", content);
}
[Test]
public static void POST<T, U>(string uri, object content)
{
const string LocalHost = "http://localhost/";
if (uri.Contains(LocalHost))
POST<T, U>(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<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 = 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 > 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;
}
}
}
提前致谢。