UserAgentStringTest.cs 2.9 KB

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