Repository navigation
Expand file tree
/
Copy pathUserAgentTest.cs
More file actions
65 lines (56 loc) · 2.65 KB
/
Copy pathUserAgentTest.cs
File metadata and controls
65 lines (56 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System.Reflection;
using System.Text.RegularExpressions;
using Coder.Desktop.CoderSdk;
namespace Coder.Desktop.Tests.CoderSdk;
[TestFixture]
public class UserAgentTest
{
// The grammar every Coder client is expected to produce. Asserting against the pattern rather
// than recomputing the platform names keeps the test honest about the mapping in UserAgent.
private static readonly Regex Grammar = new(
@"^(?<token>coder-desktop|coder-desktop-core)/(?<version>[0-9]+\.[0-9]+\.[0-9]+) \((windows|darwin|linux)/(386|amd64|arm|arm64)\)$",
RegexOptions.Compiled);
[Test(Description = "Desktop User-Agent matches the shared grammar")]
public void DesktopMatchesGrammar()
{
var match = Grammar.Match(UserAgent.Build(CoderComponent.Desktop));
Assert.That(match.Success, Is.True);
Assert.That(match.Groups["token"].Value, Is.EqualTo("coder-desktop"));
}
[Test(Description = "Core User-Agent matches the shared grammar")]
public void CoreMatchesGrammar()
{
var match = Grammar.Match(UserAgent.Build(CoderComponent.Core));
Assert.That(match.Success, Is.True);
Assert.That(match.Groups["token"].Value, Is.EqualTo("coder-desktop-core"));
}
[Test(Description = "Four-part assembly version is trimmed to three parts")]
public void TrimsVersionToThreeParts()
{
var assembly = Assembly.GetExecutingAssembly();
var version = assembly.GetName().Version;
Assert.That(version, Is.Not.Null);
// Precondition: assembly versions are four-part, which is what makes trimming observable.
Assert.That(version!.ToString().Split('.'), Has.Length.EqualTo(4));
var userAgent = UserAgent.Build(CoderComponent.Desktop, assembly);
var match = Grammar.Match(userAgent);
Assert.That(match.Success, Is.True);
Assert.That(match.Groups["version"].Value,
Is.EqualTo($"{version.Major}.{version.Minor}.{version.Build}"));
Assert.That(userAgent, Does.Not.Contain(version.ToString()));
}
[Test(Description = "A null assembly reports an unknown version")]
public void NullAssemblyReportsUnknownVersion()
{
var userAgent = UserAgent.Build(CoderComponent.Core, null);
Assert.That(Grammar.IsMatch(userAgent), Is.True);
Assert.That(userAgent, Does.StartWith("coder-desktop-core/0.0.0 "));
}
[Test(Description = "An unknown component is rejected")]
public void UnknownComponentThrows()
{
var ex = Assert.Throws<ArgumentOutOfRangeException>(() =>
UserAgent.Build((CoderComponent)(-1), null));
Assert.That(ex!.ParamName, Is.EqualTo("component"));
}
}