Skip to content

Commit 4c92b23

Browse files
authored
Additional channel tests (#1003)
1 parent c42358b commit 4c92b23

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

test/Grpc.Net.Client.Tests/GrpcChannelTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,42 @@ public void Build_HttpClientAndHttpHandler_ThrowsError()
112112
Assert.AreEqual("HttpClient and HttpHandler have been configured. Only one HTTP caller can be specified.", ex.Message);
113113
}
114114

115+
[Test]
116+
public async Task Build_HttpClient_UsedForRequestsAsync()
117+
{
118+
// Arrange
119+
var handler = new ExceptionHttpMessageHandler("HttpClient");
120+
var channel = GrpcChannel.ForAddress("https://localhost", new GrpcChannelOptions
121+
{
122+
HttpClient = new HttpClient(handler)
123+
});
124+
var client = new Greeter.GreeterClient(channel);
125+
126+
// Act
127+
var ex = await ExceptionAssert.ThrowsAsync<RpcException>(async () => await client.SayHelloAsync(new HelloRequest()));
128+
129+
// Assert
130+
Assert.AreEqual("HttpClient", ex.Status.DebugException.Message);
131+
}
132+
133+
[Test]
134+
public async Task Build_HttpHandler_UsedForRequestsAsync()
135+
{
136+
// Arrange
137+
var handler = new ExceptionHttpMessageHandler("HttpHandler");
138+
var channel = GrpcChannel.ForAddress("https://localhost", new GrpcChannelOptions
139+
{
140+
HttpHandler = handler
141+
});
142+
var client = new Greeter.GreeterClient(channel);
143+
144+
// Act
145+
var ex = await ExceptionAssert.ThrowsAsync<RpcException>(async () => await client.SayHelloAsync(new HelloRequest()));
146+
147+
// Assert
148+
Assert.AreEqual("HttpHandler", ex.Status.DebugException.Message);
149+
}
150+
115151
[Test]
116152
public void Dispose_NotCalled_NotDisposed()
117153
{
@@ -282,5 +318,20 @@ protected override void Dispose(bool disposing)
282318
Disposed = true;
283319
}
284320
}
321+
322+
public class ExceptionHttpMessageHandler : HttpMessageHandler
323+
{
324+
public string ExceptionMessage { get; }
325+
326+
public ExceptionHttpMessageHandler(string exceptionMessage)
327+
{
328+
ExceptionMessage = exceptionMessage;
329+
}
330+
331+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
332+
{
333+
return Task.FromException<HttpResponseMessage>(new InvalidOperationException(ExceptionMessage));
334+
}
335+
}
285336
}
286337
}

0 commit comments

Comments
 (0)