Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015 gRPC authors. |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
| 19 | using System; |
| 20 | using System.Diagnostics; |
| 21 | using System.Linq; |
| 22 | using System.Threading; |
| 23 | using System.Threading.Tasks; |
| 24 | using Grpc.Core; |
| 25 | using Grpc.Core.Internal; |
| 26 | using Grpc.Core.Profiling; |
| 27 | using Grpc.Core.Utils; |
| 28 | using NUnit.Framework; |
| 29 | |
| 30 | namespace Grpc.Core.Tests |
| 31 | { |
| 32 | public class UserAgentStringTest |
| 33 | { |
| 34 | const string Host = "127.0.0.1"; |
| 35 | |
| 36 | MockServiceHelper helper; |
| 37 | Server server; |
| 38 | Channel channel; |
| 39 | |
| 40 | [TearDown] |
| 41 | public void Cleanup() |
| 42 | { |
| 43 | channel.ShutdownAsync().Wait(); |
| 44 | server.ShutdownAsync().Wait(); |
| 45 | } |
| 46 | |
| 47 | [Test] |
| 48 | public void DefaultUserAgentString() |
| 49 | { |
| 50 | helper = new MockServiceHelper(Host); |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 51 | helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) => |
| 52 | { |
| 53 | var userAgentString = context.RequestHeaders.First(m => (m.Key == "user-agent")).Value; |
| 54 | var parts = userAgentString.Split(new [] {' '}, 2); |
| 55 | Assert.AreEqual(string.Format("grpc-csharp/{0}", VersionInfo.CurrentVersion), parts[0]); |
| 56 | Assert.IsTrue(parts[1].StartsWith("grpc-c/")); |
| 57 | return Task.FromResult("PASS"); |
| 58 | }); |
Jan Tattermusch | ed94e39 | 2017-04-18 14:25:17 +0200 | [diff] [blame] | 59 | |
| 60 | server = helper.GetServer(); |
| 61 | server.Start(); |
| 62 | channel = helper.GetChannel(); |
| 63 | |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 64 | Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "")); |
| 65 | } |
| 66 | |
| 67 | [Test] |
| 68 | public void ApplicationUserAgentString() |
| 69 | { |
| 70 | helper = new MockServiceHelper(Host, |
| 71 | channelOptions: new[] { new ChannelOption(ChannelOptions.PrimaryUserAgentString, "XYZ") }); |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 72 | helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) => |
| 73 | { |
| 74 | var userAgentString = context.RequestHeaders.First(m => (m.Key == "user-agent")).Value; |
| 75 | var parts = userAgentString.Split(new[] { ' ' }, 3); |
| 76 | Assert.AreEqual("XYZ", parts[0]); |
| 77 | return Task.FromResult("PASS"); |
| 78 | }); |
Jan Tattermusch | ed94e39 | 2017-04-18 14:25:17 +0200 | [diff] [blame] | 79 | |
| 80 | server = helper.GetServer(); |
| 81 | server.Start(); |
| 82 | channel = helper.GetChannel(); |
| 83 | |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 84 | Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "")); |
| 85 | } |
| 86 | } |
| 87 | } |