public async Task When_resetting_invoked_requests_it_should_reset_sequence() { HttpStatusCode[] statusCodeSequence = { HttpStatusCode.OK, HttpStatusCode.Accepted, HttpStatusCode.BadRequest }; IResponds <IResponseResult> result = _sut.When(m => { }); statusCodeSequence.Aggregate(result, (current, next) => (IResponds <IResponseResult>)current.Respond(next)); // Act foreach (HttpStatusCode expectedStatus in statusCodeSequence #if NETCOREAPP2_1 || NETCOREAPP3_1 || NET5_0 .SkipLast(1) #else .Reverse().Skip(1).Reverse() // Ugly, does the job #endif ) { HttpResponseMessage response = await _httpClient.GetAsync(""); response.Should().HaveStatusCode(expectedStatus); } _sut.InvokedRequests.Clear(); // Assert foreach (HttpStatusCode expectedStatus in statusCodeSequence) { HttpResponseMessage response = await _httpClient.GetAsync(""); response.Should().HaveStatusCode(expectedStatus); } }
/// <summary> /// Specifies the <paramref name="statusCode"/>, <paramref name="content"/>, <paramref name="encoding"/> and <paramref name="mediaType"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="statusCode">The status code response for given request.</param> /// <param name="content">The response content.</param> /// <param name="encoding">The encoding.</param> /// <param name="mediaType">The media type.</param> public static TResult Respond <TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, string content, Encoding encoding, string mediaType) where TResult : IResponseResult { return(responds.Respond(statusCode, content, new MediaTypeHeaderValue(mediaType ?? "text/plain") { CharSet = (encoding ?? Encoding.UTF8).WebName })); }
public JsonRespondsExtensionsTests() { _httpMock = new MockHttpHandler(); _httpClient = new HttpClient(_httpMock) { BaseAddress = new Uri("http://0.0.0.0") }; _sut = _httpMock.When(_ => { }); }
/// <summary> /// Specifies to throw a <see cref="TaskCanceledException"/> after a specified amount of time, simulating a HTTP request timeout. /// </summary> /// <param name="responds"></param> /// <param name="timeoutAfter">The time after which the timeout occurs.</param> public static TResult TimesOutAfter <TResult>(this IResponds <TResult> responds, TimeSpan timeoutAfter) where TResult : IResponseResult { if (responds is null) { throw new ArgumentNullException(nameof(responds)); } return(responds.RespondUsing(new TimeoutStrategy(timeoutAfter))); }
/// <summary> /// Specifies the <paramref name="statusCode"/> and <paramref name="value"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="statusCode">The status code response for given request.</param> /// <param name="value">The response value.</param> /// <param name="formatter">The media type formatter</param> /// <param name="mediaType">The media type. Can be null, in which case the <paramref name="formatter"/> default content type will be used.</param> public static TResult RespondObject <T, TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, Func <HttpRequestMessage, T> value, MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType) where TResult : IResponseResult { if (responds is null) { throw new ArgumentNullException(nameof(responds)); } return(responds.RespondUsing(new MediaTypeFormatterObjectResponseStrategy <T>(statusCode, value, mediaType, formatter))); }
/// <summary> /// Specifies a strategy that returns the response for a request. /// </summary> /// <param name="responds"></param> internal static TResult RespondUsing <TStrategy, TResult>(this IResponds <TResult> responds) where TStrategy : IResponseStrategy, new() where TResult : IResponseResult { if (responds is null) { throw new ArgumentNullException(nameof(responds)); } return(responds.RespondUsing(new TStrategy())); }
/// <summary> /// Specifies the <paramref name="statusCode"/> and <paramref name="content"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="statusCode">The status code response for given request.</param> /// <param name="content">The response content.</param> /// <param name="mediaType">The media type. Can be null, in which case the default JSON content type will be used.</param> /// <param name="serializerSettings">The serializer settings.</param> public static TResult RespondJson <T, TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, Func <HttpRequestMessage, T> content, MediaTypeHeaderValue mediaType, JsonSerializerSettings serializerSettings) where TResult : IResponseResult { if (responds is null) { throw new ArgumentNullException(nameof(responds)); } MediaTypeHeaderValue mt = mediaType ?? MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); return(responds.RespondUsing(new JsonResponseStrategy <T>(statusCode, content, mt, serializerSettings))); }
/// <summary> /// Specifies a function that returns the response for a request. /// </summary> /// <param name="responds"></param> /// <param name="response">The function that provides the response message to return for given request.</param> public static TResult Respond <TResult>(this IResponds <TResult> responds, Func <HttpRequestMessage, HttpResponseMessage> response) where TResult : IResponseResult { if (responds is null) { throw new ArgumentNullException(nameof(responds)); } if (response is null) { throw new ArgumentNullException(nameof(response)); } return(responds.Respond((request, _) => Task.FromResult(response(request)))); }
/// <summary> /// Specifies the <paramref name="statusCode"/> and <paramref name="streamContent"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="statusCode">The status code response for given request.</param> /// <param name="streamContent">The factory to create the response stream with.</param> /// <param name="mediaType">The media type.</param> public static TResult Respond <TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, Func <Stream> streamContent, MediaTypeHeaderValue mediaType) where TResult : IResponseResult { if (responds is null) { throw new ArgumentNullException(nameof(responds)); } if (streamContent is null) { throw new ArgumentNullException(nameof(streamContent)); } return(responds.RespondUsing(new FromStreamStrategy(statusCode, streamContent, mediaType))); }
/// <summary> /// Specifies the <paramref name="statusCode"/> and <paramref name="content"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="statusCode">The status code response for given request.</param> /// <param name="content">The response content.</param> public static TResult Respond <TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, HttpContent content) where TResult : IResponseResult { if (responds is null) { throw new ArgumentNullException(nameof(responds)); } if (content is null) { throw new ArgumentNullException(nameof(content)); } return(responds.Respond(async() => new HttpResponseMessage(statusCode) { Content = await content.CloneAsByteArrayContentAsync().ConfigureAwait(false) })); }
/// <summary> /// Specifies the <paramref name="statusCode"/>, <paramref name="content"/> and <paramref name="mediaType"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="statusCode">The status code response for given request.</param> /// <param name="content">The response content.</param> /// <param name="mediaType">The media type.</param> public static TResult Respond <TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, string content, MediaTypeHeaderValue mediaType) where TResult : IResponseResult { if (content is null) { throw new ArgumentNullException(nameof(content)); } return(responds.Respond(() => new HttpResponseMessage(statusCode) { Content = new StringContent(content) { Headers = { ContentType = mediaType } } })); }
/// <summary> /// Specifies the <paramref name="statusCode"/> and <paramref name="streamContent"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="statusCode">The status code response for given request.</param> /// <param name="streamContent">The response stream.</param> /// <param name="mediaType">The media type.</param> public static TResult Respond <TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, Stream streamContent, MediaTypeHeaderValue mediaType) where TResult : IResponseResult { if (streamContent is null) { throw new ArgumentNullException(nameof(streamContent)); } if (!streamContent.CanRead) { throw new ArgumentException("Cannot read from stream.", nameof(streamContent)); } byte[] buffer; using (var ms = new MemoryStream()) { streamContent.CopyTo(ms); buffer = ms.ToArray(); } return(responds.Respond(statusCode, () => new MemoryStream(buffer), mediaType)); }
/// <summary> /// Specifies the <paramref name="statusCode"/> and <paramref name="content"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="statusCode">The status code response for given request.</param> /// <param name="content">The response content.</param> public static TResult RespondJson <T, TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, T content) where TResult : IResponseResult { return(responds.RespondJson(statusCode, _ => content)); }
/// <summary> /// Specifies the <paramref name="statusCode"/> and <paramref name="content"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="statusCode">The status code response for given request.</param> /// <param name="content">The response content.</param> /// <param name="mediaType">The media type. Can be null, in which case the default JSON content type will be used.</param> public static TResult RespondJson <T, TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, Func <HttpRequestMessage, T> content, string mediaType) where TResult : IResponseResult { return(responds.RespondJson(statusCode, content, mediaType is null ? null : new MediaTypeHeaderValue(mediaType))); }
/// <summary> /// Specifies the <see cref="HttpStatusCode.OK"/> and <paramref name="content"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="content">The response content.</param> public static TResult RespondJson <T, TResult>(this IResponds <TResult> responds, T content) where TResult : IResponseResult { return(responds.RespondJson(_ => content)); }
/// <summary> /// Specifies the <paramref name="statusCode"/> response for a request. /// </summary> /// <param name="responds"></param> /// <param name="statusCode">The status code response for given request.</param> public static TResult Respond <TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode) where TResult : IResponseResult { return(responds.Respond(() => new HttpResponseMessage(statusCode))); }
/// <summary> /// Specifies the <see cref="HttpStatusCode.OK"/> and <paramref name="value"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="value">The response value.</param> /// <param name="formatter">The media type formatter</param> /// <param name="mediaType">The media type. Can be null, in which case the <paramref name="formatter"/> default content type will be used.</param> public static TResult RespondObject <T, TResult>(this IResponds <TResult> responds, Func <HttpRequestMessage, T> value, MediaTypeFormatter formatter, string mediaType) where TResult : IResponseResult { return(responds.RespondObject(HttpStatusCode.OK, value, formatter, mediaType)); }
/// <summary> /// Specifies the <paramref name="statusCode"/> and <paramref name="value"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="statusCode">The status code response for given request.</param> /// <param name="value">The response value.</param> /// <param name="formatter">The media type formatter</param> public static TResult RespondObject <T, TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, T value, MediaTypeFormatter formatter) where TResult : IResponseResult { return(responds.RespondObject(statusCode, _ => value, formatter)); }
/// <summary> /// Specifies the <see cref="HttpStatusCode.OK"/> and <paramref name="content"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="content">The response content.</param> /// <param name="mediaType">The media type. Can be null, in which case the default JSON content type will be used.</param> /// <param name="serializerSettings">The serializer settings.</param> public static TResult RespondJson <T, TResult>(this IResponds <TResult> responds, T content, MediaTypeHeaderValue mediaType, JsonSerializerSettings serializerSettings) where TResult : IResponseResult { return(responds.RespondJson(_ => content, mediaType, serializerSettings)); }
/// <summary> /// Specifies the <paramref name="statusCode"/> and <paramref name="value"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="statusCode">The status code response for given request.</param> /// <param name="value">The response value.</param> /// <param name="formatter">The media type formatter</param> /// <param name="mediaType">The media type. Can be null, in which case the <paramref name="formatter"/> default content type will be used.</param> public static TResult RespondObject <T, TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, Func <HttpRequestMessage, T> value, MediaTypeFormatter formatter, string mediaType) where TResult : IResponseResult { return(responds.RespondObject(statusCode, value, formatter, mediaType is null ? null : MediaTypeHeaderValue.Parse(mediaType))); }
/// <summary> /// Specifies to throw a <see cref="TaskCanceledException"/> simulating a HTTP request timeout. /// </summary> /// <param name="responds"></param> public static TResult TimesOut <TResult>(this IResponds <TResult> responds) where TResult : IResponseResult { return(responds.TimesOutAfter(0)); }
/// <summary> /// Specifies to throw a <see cref="TaskCanceledException"/> after a specified amount of time, simulating a HTTP request timeout. /// </summary> /// <param name="responds"></param> /// <param name="timeoutAfterMilliseconds">The number of milliseconds after which the timeout occurs.</param> public static TResult TimesOutAfter <TResult>(this IResponds <TResult> responds, int timeoutAfterMilliseconds) where TResult : IResponseResult { return(responds.TimesOutAfter(TimeSpan.FromMilliseconds(timeoutAfterMilliseconds))); }
public static IEnumerable <object[]> TestCases() { var streamMock = new Mock <Stream> { CallBase = true }; streamMock.SetReturnsDefault(true); using var content = new StringContent(""); IResponds <IResponseResult> responds = Mock.Of <IResponds <IResponseResult> >(); DelegateTestCase[] testCases = { DelegateTestCase.Create <IResponds <IResponseResult>, Func <HttpResponseMessage>, IResponseResult>( IRespondsExtensions.Respond, responds, () => new HttpResponseMessage()), DelegateTestCase.Create <IResponds <IResponseResult>, Func <HttpRequestMessage, HttpResponseMessage>,IResponseResult>( IRespondsExtensions.Respond, responds, _ => new HttpResponseMessage()), DelegateTestCase.Create( IRespondsExtensions.RespondUsing <FakeResponseStrategy, IResponseResult>, responds), DelegateTestCase.Create( IRespondsExtensions.Respond, responds, HttpStatusCode.OK), DelegateTestCase.Create( IRespondsExtensions.Respond, responds, "test content"), DelegateTestCase.Create( IRespondsExtensions.Respond, responds, HttpStatusCode.OK, "test content"), DelegateTestCase.Create( IRespondsExtensions.Respond, responds, "test content", (string)null), DelegateTestCase.Create( IRespondsExtensions.Respond, responds, HttpStatusCode.OK, "test content", (string)null), DelegateTestCase.Create( IRespondsExtensions.Respond, responds, "test content", (MediaTypeHeaderValue)null), DelegateTestCase.Create( IRespondsExtensions.Respond, responds, HttpStatusCode.OK, "test content", (MediaTypeHeaderValue)null), DelegateTestCase.Create( IRespondsExtensions.Respond, responds, "test content", (Encoding)null, (string)null), DelegateTestCase.Create( IRespondsExtensions.Respond, responds, HttpStatusCode.OK, "test content", (Encoding)null, (string)null), DelegateTestCase.Create( IRespondsExtensions.Respond, responds, streamMock.Object), DelegateTestCase.Create( IRespondsExtensions.Respond, responds, streamMock.Object, (string)null), DelegateTestCase.Create( IRespondsExtensions.Respond, responds, HttpStatusCode.OK, streamMock.Object), DelegateTestCase.Create( IRespondsExtensions.Respond, responds, HttpStatusCode.OK, streamMock.Object, (string)null), DelegateTestCase.Create( IRespondsExtensions.Respond, responds, streamMock.Object, (MediaTypeHeaderValue)null), DelegateTestCase.Create( IRespondsExtensions.Respond, responds, HttpStatusCode.OK, streamMock.Object, (MediaTypeHeaderValue)null), DelegateTestCase.Create <IResponds <IResponseResult>, HttpStatusCode, Func <Stream>, MediaTypeHeaderValue, IResponseResult>( IRespondsExtensions.Respond, responds, HttpStatusCode.OK, () => streamMock.Object, null), DelegateTestCase.Create( IRespondsExtensions.Respond, responds, content), DelegateTestCase.Create( IRespondsExtensions.Respond, responds, HttpStatusCode.OK, content), DelegateTestCase.Create( IRespondsExtensions.TimesOut, responds), DelegateTestCase.Create( IRespondsExtensions.TimesOutAfter, responds, 1), DelegateTestCase.Create( IRespondsExtensions.TimesOutAfter, responds, TimeSpan.FromMilliseconds(1)), }; return(testCases.SelectMany(tc => tc.GetNullArgumentTestCases())); }
public IRespondsExtensionsTests() { _httpCall = new HttpCall(); _sut = new HttpRequestSetupPhrase(_httpCall); }
/// <summary> /// Specifies the <paramref name="statusCode"/> and <paramref name="content"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="statusCode">The status code response for given request.</param> /// <param name="content">The response content.</param> public static TResult RespondJson <T, TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, Func <HttpRequestMessage, T> content) where TResult : IResponseResult { return(responds.RespondJson(statusCode, content, (MediaTypeHeaderValue)null)); }
/// <summary> /// Specifies the <paramref name="statusCode"/> and <paramref name="value"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="statusCode">The status code response for given request.</param> /// <param name="value">The response value.</param> /// <param name="formatter">The media type formatter</param> public static TResult RespondObject <T, TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, Func <HttpRequestMessage, T> value, MediaTypeFormatter formatter) where TResult : IResponseResult { return(responds.RespondObject(statusCode, value, formatter, (MediaTypeHeaderValue)null)); }
/// <summary> /// Specifies the <see cref="HttpStatusCode.OK"/> and <paramref name="content"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="content">The response content.</param> /// <param name="mediaType">The media type. Can be null, in which case the default JSON content type will be used.</param> public static TResult RespondJson <T, TResult>(this IResponds <TResult> responds, T content, MediaTypeHeaderValue mediaType) where TResult : IResponseResult { return(responds.RespondJson(_ => content, mediaType)); }
/// <summary> /// Specifies the <see cref="HttpStatusCode.OK"/> and <paramref name="value"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="value">The response value.</param> /// <param name="formatter">The media type formatter</param> /// <param name="mediaType">The media type. Can be null, in which case the <paramref name="formatter"/> default content type will be used.</param> public static TResult RespondObject <T, TResult>(this IResponds <TResult> responds, T value, MediaTypeFormatter formatter, string mediaType) where TResult : IResponseResult { return(responds.RespondObject(_ => value, formatter, mediaType)); }
/// <summary> /// Specifies the <see cref="HttpStatusCode.OK"/> and <paramref name="content"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="content">The response content.</param> /// <param name="mediaType">The media type. Can be null, in which case the default JSON content type will be used.</param> public static TResult RespondJson <T, TResult>(this IResponds <TResult> responds, Func <HttpRequestMessage, T> content, MediaTypeHeaderValue mediaType) where TResult : IResponseResult { return(responds.RespondJson(HttpStatusCode.OK, content, mediaType)); }
/// <summary> /// Specifies the <see cref="HttpStatusCode.OK"/> and <paramref name="content"/> to respond with for a request. /// </summary> /// <param name="responds"></param> /// <param name="content">The response content.</param> public static TResult Respond <TResult>(this IResponds <TResult> responds, string content) where TResult : IResponseResult { return(responds.Respond(HttpStatusCode.OK, content)); }