OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Collections.Generic; |
| 36 using System.Diagnostics; |
| 37 using System.Linq; |
| 38 using System.Threading; |
| 39 using System.Threading.Tasks; |
| 40 |
| 41 using Grpc.Core; |
| 42 using Grpc.Core.Internal; |
| 43 using Grpc.Core.Utils; |
| 44 |
| 45 using NUnit.Framework; |
| 46 |
| 47 namespace Grpc.Core.Tests |
| 48 { |
| 49 /// <summary> |
| 50 /// Tests for response headers support. |
| 51 /// </summary> |
| 52 public class ResponseHeadersTest |
| 53 { |
| 54 MockServiceHelper helper; |
| 55 Server server; |
| 56 Channel channel; |
| 57 |
| 58 Metadata headers; |
| 59 |
| 60 [SetUp] |
| 61 public void Init() |
| 62 { |
| 63 helper = new MockServiceHelper(); |
| 64 |
| 65 server = helper.GetServer(); |
| 66 server.Start(); |
| 67 channel = helper.GetChannel(); |
| 68 |
| 69 headers = new Metadata { { "ascii-header", "abcdefg" } }; |
| 70 } |
| 71 |
| 72 [TearDown] |
| 73 public void Cleanup() |
| 74 { |
| 75 channel.ShutdownAsync().Wait(); |
| 76 server.ShutdownAsync().Wait(); |
| 77 } |
| 78 |
| 79 [Test] |
| 80 public async Task ResponseHeadersAsync_UnaryCall() |
| 81 { |
| 82 helper.UnaryHandler = new UnaryServerMethod<string, string>(async (r
equest, context) => |
| 83 { |
| 84 await context.WriteResponseHeadersAsync(headers); |
| 85 return "PASS"; |
| 86 }); |
| 87 |
| 88 var call = Calls.AsyncUnaryCall(helper.CreateUnaryCall(), ""); |
| 89 var responseHeaders = await call.ResponseHeadersAsync; |
| 90 |
| 91 Assert.AreEqual(headers.Count, responseHeaders.Count); |
| 92 Assert.AreEqual("ascii-header", responseHeaders[0].Key); |
| 93 Assert.AreEqual("abcdefg", responseHeaders[0].Value); |
| 94 |
| 95 Assert.AreEqual("PASS", await call.ResponseAsync); |
| 96 } |
| 97 |
| 98 [Test] |
| 99 public async Task ResponseHeadersAsync_ClientStreamingCall() |
| 100 { |
| 101 helper.ClientStreamingHandler = new ClientStreamingServerMethod<stri
ng, string>(async (requestStream, context) => |
| 102 { |
| 103 await context.WriteResponseHeadersAsync(headers); |
| 104 return "PASS"; |
| 105 }); |
| 106 |
| 107 var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreami
ngCall()); |
| 108 await call.RequestStream.CompleteAsync(); |
| 109 var responseHeaders = await call.ResponseHeadersAsync; |
| 110 |
| 111 Assert.AreEqual("ascii-header", responseHeaders[0].Key); |
| 112 Assert.AreEqual("PASS", await call.ResponseAsync); |
| 113 } |
| 114 |
| 115 [Test] |
| 116 public async Task ResponseHeadersAsync_ServerStreamingCall() |
| 117 { |
| 118 helper.ServerStreamingHandler = new ServerStreamingServerMethod<stri
ng, string>(async (request, responseStream, context) => |
| 119 { |
| 120 await context.WriteResponseHeadersAsync(headers); |
| 121 await responseStream.WriteAsync("PASS"); |
| 122 }); |
| 123 |
| 124 var call = Calls.AsyncServerStreamingCall(helper.CreateServerStreami
ngCall(), ""); |
| 125 var responseHeaders = await call.ResponseHeadersAsync; |
| 126 |
| 127 Assert.AreEqual("ascii-header", responseHeaders[0].Key); |
| 128 CollectionAssert.AreEqual(new[] { "PASS" }, await call.ResponseStrea
m.ToListAsync()); |
| 129 } |
| 130 |
| 131 [Test] |
| 132 public async Task ResponseHeadersAsync_DuplexStreamingCall() |
| 133 { |
| 134 helper.DuplexStreamingHandler = new DuplexStreamingServerMethod<stri
ng, string>(async (requestStream, responseStream, context) => |
| 135 { |
| 136 await context.WriteResponseHeadersAsync(headers); |
| 137 while (await requestStream.MoveNext()) |
| 138 { |
| 139 await responseStream.WriteAsync(requestStream.Current); |
| 140 } |
| 141 }); |
| 142 |
| 143 var call = Calls.AsyncDuplexStreamingCall(helper.CreateDuplexStreami
ngCall()); |
| 144 var responseHeaders = await call.ResponseHeadersAsync; |
| 145 |
| 146 var messages = new[] { "PASS" }; |
| 147 await call.RequestStream.WriteAllAsync(messages); |
| 148 |
| 149 Assert.AreEqual("ascii-header", responseHeaders[0].Key); |
| 150 CollectionAssert.AreEqual(messages, await call.ResponseStream.ToList
Async()); |
| 151 } |
| 152 |
| 153 [Test] |
| 154 public void WriteResponseHeaders_NullNotAllowed() |
| 155 { |
| 156 helper.UnaryHandler = new UnaryServerMethod<string, string>(async (r
equest, context) => |
| 157 { |
| 158 Assert.Throws(typeof(ArgumentNullException), async () => await c
ontext.WriteResponseHeadersAsync(null)); |
| 159 return "PASS"; |
| 160 }); |
| 161 |
| 162 Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCa
ll(), "")); |
| 163 } |
| 164 |
| 165 [Test] |
| 166 public void WriteResponseHeaders_AllowedOnlyOnce() |
| 167 { |
| 168 helper.UnaryHandler = new UnaryServerMethod<string, string>(async (r
equest, context) => |
| 169 { |
| 170 await context.WriteResponseHeadersAsync(headers); |
| 171 try |
| 172 { |
| 173 await context.WriteResponseHeadersAsync(headers); |
| 174 Assert.Fail(); |
| 175 } |
| 176 catch (InvalidOperationException expected) |
| 177 { |
| 178 } |
| 179 return "PASS"; |
| 180 }); |
| 181 |
| 182 Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCa
ll(), "")); |
| 183 } |
| 184 |
| 185 [Test] |
| 186 public async Task WriteResponseHeaders_NotAllowedAfterWrite() |
| 187 { |
| 188 helper.ServerStreamingHandler = new ServerStreamingServerMethod<stri
ng, string>(async (request, responseStream, context) => |
| 189 { |
| 190 await responseStream.WriteAsync("A"); |
| 191 try |
| 192 { |
| 193 await context.WriteResponseHeadersAsync(headers); |
| 194 Assert.Fail(); |
| 195 } |
| 196 catch (InvalidOperationException expected) |
| 197 { |
| 198 } |
| 199 await responseStream.WriteAsync("B"); |
| 200 }); |
| 201 |
| 202 var call = Calls.AsyncServerStreamingCall(helper.CreateServerStreami
ngCall(), ""); |
| 203 var responses = await call.ResponseStream.ToListAsync(); |
| 204 CollectionAssert.AreEqual(new[] { "A", "B" }, responses); |
| 205 } |
| 206 } |
| 207 } |
OLD | NEW |