Last active
September 12, 2017 16:23
-
-
Save AlbertoMonteiro/242578fbd2b110cb59fe6d49dbc586d6 to your computer and use it in GitHub Desktop.
Web API Testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Text; | |
using System.Web.Http; | |
using System.Web.Http.Description; | |
namespace MeuProjeto.Web.Controllers.Api | |
{ | |
[RoutePrefix("api/v2/inventario")] | |
public class InventarioApiController : BaseApiController | |
{ | |
private readonly IInventarioAppService _inventarioAppService; | |
public InventarioApiController(IInventarioAppService inventarioAppService) | |
{ | |
_inventarioAppService = inventarioAppService; | |
} | |
[HttpPost, Route("")] | |
public HttpResponseMessage IncluirInventario(InventarioInputModel inventario) | |
{ | |
if (!ModelState.IsValid) return BadRequestResult(ModelState); | |
var operationResult = _inventarioAppService.IncluirInventario(inventario); | |
return operationResult.IsSuccess | |
? OkResult() | |
: ResultForException(operationResult.Exception); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Testes | |
{ | |
public async Task TesteDeDadosIncorretos() | |
{ | |
//Ambiente | |
var inputDeDados = default(InventarioInputModel); | |
//Ação | |
var response = await WebApiTestServer.PostAsync(Kernel, @"api/v2/graficos/", inputDeDados); | |
//Assertiva | |
Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); | |
//Outros asserts.... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using System.Net.Http; | |
using System.Net.Http.Formatting; | |
using System.Reflection; | |
using System.Text.RegularExpressions; | |
using System.Threading.Tasks; | |
using System.Web.Http; | |
using Ninject; | |
using Ninject.Web.WebApi; | |
namespace MeuProjeto.Teste.Web.API.Utils | |
{ | |
public static class WebApiTestServer | |
{ | |
public static async Task<HttpResponseMessage> PutAsync(IKernel kernel, string url) | |
{ | |
var httpClient = CreateTestServer(kernel); | |
return await httpClient.PutAsync($"http://webapp.com.br/{url}", null); | |
} | |
public static async Task<HttpResponseMessage> GetAsync(IKernel kernel, string url) | |
{ | |
var httpClient = CreateTestServer(kernel); | |
return await httpClient.GetAsync($"http://webapp.com.br/{url}"); | |
} | |
public static async Task<HttpResponseMessage> PostAsync<T>(IKernel kernel, string url, T content) | |
where T : class | |
{ | |
var httpClient = CreateTestServer(kernel); | |
return await httpClient.PostAsync($"http://webapp.com.br/{url}", new ObjectContent<T>(content, new JsonMediaTypeFormatter())); | |
} | |
public static async Task<HttpResponseMessage> GetAsync(IKernel kernel, string url, object obj) | |
{ | |
var uri = new UriBuilder($"http://webapp.com.br/{url}") | |
{ | |
Query = string.Join("&", obj.GetType().GetProperties().Select(p => $"{p.Name}={p.GetValue(obj)}")) | |
}; | |
var httpClient = CreateTestServer(kernel); | |
return await httpClient.GetAsync(uri.Uri); | |
} | |
private static HttpClient CreateTestServer(IKernel kernel) | |
{ | |
var httpServer = new HttpServer(new HttpConfiguration | |
{ | |
IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always, | |
DependencyResolver = new NinjectDependencyResolver(kernel) | |
}); | |
WebApiConfig.Register(httpServer.Configuration); | |
var httpClient = new HttpClient(httpServer); | |
return httpClient; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment