diff --git a/Test/Flurl.Test/Http/TestingTests.cs b/Test/Flurl.Test/Http/TestingTests.cs index c055757d..58b813d6 100644 --- a/Test/Flurl.Test/Http/TestingTests.cs +++ b/Test/Flurl.Test/Http/TestingTests.cs @@ -3,6 +3,7 @@ using System.Net.Http; using System.Threading.Tasks; using Flurl.Http; +using Flurl.Http.Content; using Flurl.Http.Testing; using NUnit.Framework; @@ -499,5 +500,21 @@ public async Task can_use_response_queue_in_parallel() { } } } + + [Test] // #673 + public async Task can_use_request_in_response() + { + HttpTest.RespondWith(request => + { + var requestBody = request.Content.ReadAsStringAsync().GetAwaiter().GetResult(); + return new CapturedStringContent(requestBody); + }); + + var receivedString = await "http://api.com" + .PostStringAsync("hello world") + .ReceiveString(); + + Assert.AreEqual("hello world", receivedString); + } } } diff --git a/src/Flurl.Http/Testing/FakeHttpMessageHandler.cs b/src/Flurl.Http/Testing/FakeHttpMessageHandler.cs index 4187dcce..28a76eca 100644 --- a/src/Flurl.Http/Testing/FakeHttpMessageHandler.cs +++ b/src/Flurl.Http/Testing/FakeHttpMessageHandler.cs @@ -30,7 +30,7 @@ protected override Task SendAsync(HttpRequestMessage reques HttpTest.Current.LogCall(call); var setup = HttpTest.Current.FindSetup(call); if (setup?.FakeRequest == true) { - var resp = setup.GetNextResponse() ?? new HttpResponseMessage { + var resp = setup.GetNextResponse(request) ?? new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent("") }; diff --git a/src/Flurl.Http/Testing/HttpTestSetup.cs b/src/Flurl.Http/Testing/HttpTestSetup.cs index ef9212f9..71c49c23 100644 --- a/src/Flurl.Http/Testing/HttpTestSetup.cs +++ b/src/Flurl.Http/Testing/HttpTestSetup.cs @@ -15,7 +15,7 @@ namespace Flurl.Http.Testing /// public abstract class HttpTestSetup { - private readonly List> _responses = new List>(); + private readonly List> _responses = new List>(); private int _respIndex = 0; private bool _allowRealHttp = false; @@ -35,12 +35,12 @@ protected HttpTestSetup(TestFlurlHttpSettings settings) { internal bool FakeRequest => !_allowRealHttp; - internal HttpResponseMessage GetNextResponse() { + internal HttpResponseMessage GetNextResponse(HttpRequestMessage request) { if (!_responses.Any()) return null; // atomically get the next response in the list, or the last one if we're past the end - return _responses[Math.Min(Interlocked.Increment(ref _respIndex), _responses.Count) - 1](); + return _responses[Math.Min(Interlocked.Increment(ref _respIndex), _responses.Count) - 1](request); } /// @@ -79,11 +79,25 @@ public HttpTestSetup RespondWithJson(object body, int status = 200, object heade /// The simulated response cookies. Optional. /// If true, underscores in property names of headers will be replaced by hyphens. Default is true. /// The current HttpTest object (so more responses can be chained). - public HttpTestSetup RespondWith(Func buildContent = null, int status = 200, object headers = null, object cookies = null, bool replaceUnderscoreWithHyphen = true) { - _responses.Add(() => { + public HttpTestSetup RespondWith(Func buildContent, int status = 200, object headers = null, object cookies = null, bool replaceUnderscoreWithHyphen = true) + { + return RespondWith(request => buildContent?.Invoke(), status, headers, cookies, replaceUnderscoreWithHyphen); + } + + /// + /// Adds a fake HTTP response to the response queue. + /// + /// A function that builds the simulated response body content. Optional. + /// The simulated HTTP status. Optional. Default is 200. + /// The simulated response headers. Optional. + /// The simulated response cookies. Optional. + /// If true, underscores in property names of headers will be replaced by hyphens. Default is true. + /// The current HttpTest object (so more responses can be chained). + public HttpTestSetup RespondWith(Func buildContent = null, int status = 200, object headers = null, object cookies = null, bool replaceUnderscoreWithHyphen = true) { + return RespondWith(request => { var response = new HttpResponseMessage { StatusCode = (HttpStatusCode)status, - Content = buildContent?.Invoke() + Content = buildContent?.Invoke(request) }; if (headers != null) { @@ -99,6 +113,15 @@ public HttpTestSetup RespondWith(Func buildContent = null, int stat } return response; }); + } + + /// + /// Adds a fake HTTP response to the response queue. + /// + /// A function that builds the simulated response. + /// The current HttpTest object (so more responses can be chained). + public HttpTestSetup RespondWith(Func buildResponse) { + _responses.Add(buildResponse); return this; } @@ -106,7 +129,7 @@ public HttpTestSetup RespondWith(Func buildContent = null, int stat /// Adds a simulated timeout response to the response queue. /// public HttpTestSetup SimulateTimeout() { - _responses.Add(() => new TimeoutResponseMessage()); + _responses.Add(request => new TimeoutResponseMessage()); return this; }