public async Task Write_multiple_responses() { var responses = new List <StreamResponse>(); var response = new Mock <HttpResponse>(); response .Setup(x => x.Body.WriteAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CancellationToken>())) .Callback((byte[] payload, int _1, int _2, CancellationToken _3) => { responses.Add(JsonConvert.DeserializeObject <StreamResponse>(Revert(Encoding.UTF8.GetString(payload)))); }) .Returns(Task.CompletedTask) .Verifiable(); CliResponse cliResponse = new MultipleResponses(async yield => { await yield.ReturnAsync("1"); await yield.ReturnAsync("2"); }); await HttpOutputWriter.Write(cliResponse, response.Object); response.Verify(x => x.Body.WriteAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CancellationToken>()), Times.Exactly(2)); responses.Count.ShouldBe(2); responses[0].IsDone.ShouldBe(false); responses[1].IsDone.ShouldBe(false); }
public async Task Write_single_response() { var responses = new List <CliResponse>(); var response = new Mock <HttpResponse>(); response .Setup(x => x.Body.WriteAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CancellationToken>())) .Callback((byte[] payload, int _1, int _2, CancellationToken _3) => { responses.Add(JsonConvert.DeserializeObject <TextResponse>(Revert(Encoding.UTF8.GetString(payload)))); }) .Returns(Task.CompletedTask) .Verifiable(); await HttpOutputWriter.Write(new TextResponse("a"), response.Object); response.Verify(x => x.Body.WriteAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CancellationToken>()), Times.Once); responses[0].IsDone.ShouldBe(true); responses[0].Content.ShouldBe("a"); }