1 #region Copyright notice and license
2 
3 // Copyright 2015 gRPC authors.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
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]
Cleanup()41         public void Cleanup()
42         {
43             channel.ShutdownAsync().Wait();
44             server.ShutdownAsync().Wait();
45         }
46 
47         [Test]
DefaultUserAgentString()48         public void DefaultUserAgentString()
49         {
50             helper = new MockServiceHelper(Host);
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             });
59 
60             server = helper.GetServer();
61             server.Start();
62             channel = helper.GetChannel();
63 
64             Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), ""));
65         }
66 
67         [Test]
ApplicationUserAgentString()68         public void ApplicationUserAgentString()
69         {
70             helper = new MockServiceHelper(Host,
71                 channelOptions: new[] { new ChannelOption(ChannelOptions.PrimaryUserAgentString, "XYZ") });
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             });
79 
80             server = helper.GetServer();
81             server.Start();
82             channel = helper.GetChannel();
83 
84             Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), ""));
85         }
86     }
87 }
88