In questo approccio, creo un TestHandler e impostarlo come la InnerHandler
di proprietà del gestore in prova.
L'handler in prova può essere passato a un HttpClient
- questo può sembrare non intuitivo se si sta scrivendo un gestore lato server, ma questo è in realtà un modo leggero per testare un gestore - verrà chiamato in allo stesso modo in un server.
TestHandler restituirà solo un HTTP 200 per impostazione predefinita, ma il costruttore accetta una funzione che è possibile utilizzare per fare asserzioni sul messaggio di richiesta passato dal gestore in prova. Finalmente puoi fare affermazioni sul risultato della chiamata SendAsync dal client.
Una volta che tutto è configurato, chiamare SendAsync
sull'istanza client per richiamare il gestore. La richiesta verrà passata al gestore, che passerà a TestHandler (supponendo che passi la chiamata), che restituirà una risposta al gestore.
gestore
Il test è simile al seguente:
public class TestHandler : DelegatingHandler
{
private readonly Func<HttpRequestMessage,
CancellationToken, Task<HttpResponseMessage>> _handlerFunc;
public TestHandler()
{
_handlerFunc = (r, c) => Return200();
}
public TestHandler(Func<HttpRequestMessage,
CancellationToken, Task<HttpResponseMessage>> handlerFunc)
{
_handlerFunc = handlerFunc;
}
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
return _handlerFunc(request, cancellationToken);
}
public static Task<HttpResponseMessage> Return200()
{
return Task.Factory.StartNew(
() => new HttpResponseMessage(HttpStatusCode.OK));
}
}
Esempio di utilizzo con un immaginario MyHandler
in prova. Utilizza NUnit per il asserisce .:
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://test.com");
httpRequestMessage.Headers.Add("username", "test");
var handler = new MyHandler()
{
InnerHandler = new TestHandler((r,c) =>
{
Assert.That(r.Headers.Contains("username"));
return TestHandler.Return200();
})
};
var client = new HttpClient(handler);
var result = client.SendAsync(httpRequestMessage).Result;
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK));
Il comportamento predefinito di TestHandler è probabilmente bene per molti test e rende il codice più semplice. La configurazione del gestore in prova si presenta quindi come questo:
var handler = new MyHandler();
handler.InnerHandler = new TestHandler();
Mi piace questo approccio perché mantiene tutte le affermazioni nel metodo di prova, e il TestHandler
è molto riutilizzabile.
come si gestisce l'oggetto richiesta qui. request.createresponse non riesce poiché non vi è alcuna httpconfiguration.quindi se ne aggiungi uno puoi farlo, ma cosa succede se sei interessato ad altre proprietà di richiesta associate al messaggio di richiesta http che hai creato? – Steve
Il mio obiettivo qui è puramente per testare il gestore; quindi non è il tuo suggerimento di aggiungere la HttpConfiguration sufficiente? Qual è il tuo scenario? –
Ho dovuto aggiungere wrapper attorno a request.content, request.properties e request.getroutedata. tutto va bene in test landia – Steve