blob: 1b4676f9e87bbd25d7d8235c811b2a5cc5b00bda [file] [log] [blame]
Jan Tattermusch7ebbc472015-12-08 22:39:02 -08001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Jan Tattermusch7ebbc472015-12-08 22:39:02 -08004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// 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 Tattermusch7ebbc472015-12-08 22:39:02 -08008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermusch7ebbc472015-12-08 22:39:02 -080010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// 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 Tattermusch7ebbc472015-12-08 22:39:02 -080016
17#endregion
18
19using System;
20using System.Diagnostics;
21using System.Linq;
22using System.Threading;
23using System.Threading.Tasks;
24using Grpc.Core;
25using Grpc.Core.Internal;
26using Grpc.Core.Profiling;
27using Grpc.Core.Utils;
28using NUnit.Framework;
29
30namespace 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 Tattermusch7ebbc472015-12-08 22:39:02 -080051 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 Tattermusched94e392017-04-18 14:25:17 +020059
60 server = helper.GetServer();
61 server.Start();
62 channel = helper.GetChannel();
63
Jan Tattermusch7ebbc472015-12-08 22:39:02 -080064 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 Tattermusch7ebbc472015-12-08 22:39:02 -080072 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 Tattermusched94e392017-04-18 14:25:17 +020079
80 server = helper.GetServer();
81 server.Start();
82 channel = helper.GetChannel();
83
Jan Tattermusch7ebbc472015-12-08 22:39:02 -080084 Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), ""));
85 }
86 }
87}