Beispiel #1
0
        public async Task SayHello_ValidRequest_SuccessResponse()
        {
            // Arrange
            var requestMessage = new HelloRequest
            {
                Name = "World"
            };

            var stream = new MemoryStream();
            await MessageHelpers.WriteMessageAsync(stream, requestMessage).DefaultTimeout();

            // Act
            var response = await Fixture.Client.PostAsync(
                "Greet.Greeter/SayHello",
                new StreamContent(stream)).DefaultTimeout();

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual("identity", response.Headers.GetValues("grpc-encoding").Single());
            Assert.AreEqual("application/grpc", response.Content.Headers.ContentType.MediaType);

            var responseMessage = MessageHelpers.AssertReadMessage <HelloReply>(await response.Content.ReadAsByteArrayAsync().DefaultTimeout());

            Assert.AreEqual("Hello World", responseMessage.Message);
        }
Beispiel #2
0
        public async Task AccumulateCount_MultipleMessagesThenClose_SuccessResponse()
        {
            // Arrange
            var ms = new MemoryStream();
            await MessageHelpers.WriteMessageAsync(ms, new CounterRequest
            {
                Count = 1
            }).DefaultTimeout();

            var requestStream = new AwaitableMemoryStream();

            var httpRequest = new HttpRequestMessage(HttpMethod.Post, "Count.Counter/AccumulateCount");

            httpRequest.Content = new StreamContent(requestStream);

            // Act
            var responseTask = Fixture.Client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead);

            // Assert
            Assert.IsFalse(responseTask.IsCompleted, "Server should wait for client to finish streaming");

            Fixture.Signal.Reset();
            requestStream.SendData(ms.ToArray());
            await Fixture.Signal.Task.DefaultTimeout();

            Fixture.Signal.Reset();
            requestStream.SendData(ms.ToArray());
            await Fixture.Signal.Task.DefaultTimeout();

            Fixture.Signal.Reset();
            requestStream.SendData(Array.Empty <byte>());
            await Fixture.Signal.Task.DefaultTimeout();

            var response = await responseTask.DefaultTimeout();

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual("identity", response.Headers.GetValues("grpc-encoding").Single());
            Assert.AreEqual("application/grpc", response.Content.Headers.ContentType.MediaType);

            var responseStream = await response.Content.ReadAsStreamAsync().DefaultTimeout();

            var replyTask = MessageHelpers.AssertReadMessageAsync <CounterReply>(responseStream);

            Assert.IsTrue(replyTask.IsCompleted);
            var reply = await replyTask.DefaultTimeout();

            Assert.AreEqual(2, reply.Count);
        }
        public async Task SayHellos_NoBuffering_SuccessResponsesStreamed()
        {
            // Arrange
            var requestMessage = new HelloRequest
            {
                Name = "World"
            };

            var requestStream = new MemoryStream();
            await MessageHelpers.WriteMessageAsync(requestStream, requestMessage).DefaultTimeout();

            var httpRequest = new HttpRequestMessage(HttpMethod.Post, "Greet.Greeter/SayHellos");

            httpRequest.Content = new StreamContent(requestStream);

            // Act
            var response = await Fixture.Client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).DefaultTimeout();

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual("identity", response.Headers.GetValues("grpc-encoding").Single());
            Assert.AreEqual("application/grpc", response.Content.Headers.ContentType.MediaType);

            var responseStream = await response.Content.ReadAsStreamAsync().DefaultTimeout();

            for (var i = 0; i < 3; i++)
            {
                var greetingTask = MessageHelpers.AssertReadMessageAsync <HelloReply>(responseStream);

                // The first response comes with the headers
                // Additional responses are streamed
                Assert.AreEqual(i == 0, greetingTask.IsCompleted);

                var greeting = await greetingTask.DefaultTimeout();

                Assert.AreEqual($"How are you World? {i}", greeting.Message);
            }

            var goodbyeTask = MessageHelpers.AssertReadMessageAsync <HelloReply>(responseStream);

            Assert.False(goodbyeTask.IsCompleted);
            Assert.AreEqual("Goodbye World!", (await goodbyeTask.DefaultTimeout()).Message);

            var finishedTask = MessageHelpers.AssertReadMessageAsync <HelloReply>(responseStream);

            Assert.IsNull(await finishedTask.DefaultTimeout());
        }
        public async Task Chat_MultipleMessagesFromOneClient_SuccessResponses()
        {
            // Arrange
            var ms = new MemoryStream();
            await MessageHelpers.WriteMessageAsync(ms, new ChatMessage
            {
                Name    = "John",
                Message = "Hello Jill"
            }).DefaultTimeout();

            var requestStream = new AwaitableMemoryStream();

            var httpRequest = new HttpRequestMessage(HttpMethod.Post, "Chat.Chatter/Chat");

            httpRequest.Content = new StreamContent(requestStream);

            // Act
            var responseTask = Fixture.Client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead);

            // Assert
            Assert.IsFalse(responseTask.IsCompleted, "Server should wait for first message from client");
            requestStream.SendData(ms.ToArray());

            var response = await responseTask.DefaultTimeout();

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual("identity", response.Headers.GetValues("grpc-encoding").Single());
            Assert.AreEqual("application/grpc", response.Content.Headers.ContentType.MediaType);

            var responseStream = await response.Content.ReadAsStreamAsync().DefaultTimeout();

            var message1Task = MessageHelpers.AssertReadMessageAsync <ChatMessage>(responseStream);

            Assert.IsTrue(message1Task.IsCompleted);
            var message1 = await message1Task.DefaultTimeout();

            Assert.AreEqual("John", message1.Name);
            Assert.AreEqual("Hello Jill", message1.Message);

            var message2Task = MessageHelpers.AssertReadMessageAsync <ChatMessage>(responseStream);

            Assert.IsFalse(message2Task.IsCompleted, "Server is waiting for messages from client");

            ms = new MemoryStream();
            await MessageHelpers.WriteMessageAsync(ms, new ChatMessage
            {
                Name    = "Jill",
                Message = "Hello John"
            }).DefaultTimeout();

            requestStream.SendData(ms.ToArray());
            var message2 = await message2Task.DefaultTimeout();

            Assert.AreEqual("Jill", message2.Name);
            Assert.AreEqual("Hello John", message2.Message);

            var finishedTask = MessageHelpers.AssertReadMessageAsync <ChatMessage>(responseStream);

            Assert.IsFalse(finishedTask.IsCompleted, "Server is waiting for client to end streaming");

            requestStream.SendData(Array.Empty <byte>());
            await finishedTask.DefaultTimeout();
        }