Sto cercando di utilizzare N-UNIT per testare la mia applicazione API Web ma non riesco a trovare un modo corretto per testare il metodo di caricamento del mio file. Quale sarebbe l'approccio migliore per testare il metodo?Test di ASP.NET Web API Caricamento file dati modulo multiplo
Web controller API:
[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);
}
}
perché non utilizzare HttpClient da System.Net.Http? –
Non penso che HttpClient abbia un file di upload progettato specificamente per il caricamento di file. – Muhammad