I am trying to use N-UNIT to test my web API application, but I cannot find a suitable way to test the way files are uploaded. Which method is best for testing a method?
Web API Controller:
[AcceptVerbs("post")] public async Task<HttpResponseMessage> Validate() { // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { return Request.CreateErrorResponse(HttpStatusCode.UnsupportedMediaType,"please submit a valid request"); } var provider = new MultipartMemoryStreamProvider(); // this loads the file into memory for later on processing try { await Request.Content.ReadAsMultipartAsync(provider); var resp = new HttpResponseMessage(HttpStatusCode.OK); foreach (var item in provider.Contents) { if (item.Headers.ContentDisposition.FileName != null) { Stream stream = item.ReadAsStreamAsync().Result; // do some stuff and return response resp.Content = new StringContent(result, Encoding.UTF8, "application/xml"); //text/plain "application/xml" return resp; } } return resp; } catch (System.Exception e) { return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); } }
Muhammad
source share