From d1a45281d75edae20f73bc905f21abb7b4db0dfd Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Fri, 11 Jan 2019 15:04:20 -0500 Subject: [PATCH] Add test for issue #826 --- .../TestApiHeaders.cs | 30 ++++++++++++++++++- .../Tgstation.Server.Api.Tests.csproj | 1 + 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/tests/Tgstation.Server.Api.Tests/TestApiHeaders.cs b/tests/Tgstation.Server.Api.Tests/TestApiHeaders.cs index f6347eff63..fd91e30a27 100644 --- a/tests/Tgstation.Server.Api.Tests/TestApiHeaders.cs +++ b/tests/Tgstation.Server.Api.Tests/TestApiHeaders.cs @@ -1,4 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Headers; +using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Net.Http.Headers; @@ -19,5 +21,31 @@ namespace Tgstation.Server.Api.Tests Assert.ThrowsException(() => new ApiHeaders(productHeaderValue, null)); var headers = new ApiHeaders(productHeaderValue, String.Empty); } + + [TestMethod] + public void TestUserAgentsAreValid() + { + const string BrowserHeader = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36."; + const string ConformantHeader = "TGSClient/3.2.1.4"; + + ApiHeaders TestHeader(string userAgent) + { + var headers = new HeaderDictionary(); + headers.Add("Accept", ApiHeaders.ApplicationJson); + headers.Add("Api", "Tgstation.Server.Api/4.0.0.0"); + headers.Add("Authorization", "Bearer asdfasdf"); + headers.Add("User-Agent", userAgent); + + return new ApiHeaders(new RequestHeaders(headers)); + }; + + var header = TestHeader(BrowserHeader); + Assert.AreEqual(BrowserHeader, header.RawUserAgent); + Assert.IsNull(header.UserAgent); + + header = TestHeader(ConformantHeader); + Assert.AreEqual(ConformantHeader, header.RawUserAgent); + Assert.IsNotNull(header.UserAgent); + } } } diff --git a/tests/Tgstation.Server.Api.Tests/Tgstation.Server.Api.Tests.csproj b/tests/Tgstation.Server.Api.Tests/Tgstation.Server.Api.Tests.csproj index 72f5fb422a..8d6c705910 100644 --- a/tests/Tgstation.Server.Api.Tests/Tgstation.Server.Api.Tests.csproj +++ b/tests/Tgstation.Server.Api.Tests/Tgstation.Server.Api.Tests.csproj @@ -9,6 +9,7 @@ +