Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Agent now reports the OS platform #4937

Merged
merged 12 commits into from Oct 6, 2017
Merged
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Management.Automation;
using System.Runtime.InteropServices;
using System.Globalization;

namespace Microsoft.PowerShell.Commands
Expand All @@ -13,14 +14,17 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
public static class PSUserAgent
{

private static string s_windowsUserAgent;

internal static string UserAgent
{
get
{
// format the user-agent string from the various component parts
string userAgent = string.Format(CultureInfo.InvariantCulture,
"{0} ({1}; {2}; {3}) {4}",
Compatibility, Platform, OS, Culture, App);
Compatibility, PlatformName, OS, Culture, App);
return (userAgent);
}
}
Expand All @@ -35,7 +39,7 @@ public static string InternetExplorer
// format the user-agent string from the various component parts
string userAgent = string.Format(CultureInfo.InvariantCulture,
"{0} (compatible; MSIE 9.0; {1}; {2}; {3})",
Compatibility, Platform, OS, Culture);
Compatibility, PlatformName, OS, Culture);
return (userAgent);
}
}
Expand All @@ -50,7 +54,7 @@ public static string FireFox
// format the user-agent string from the various component parts
string userAgent = string.Format(CultureInfo.InvariantCulture,
"{0} ({1}; {2}; {3}) Gecko/20100401 Firefox/4.0",
Compatibility, Platform, OS, Culture);
Compatibility, PlatformName, OS, Culture);
return (userAgent);
}
}
Expand All @@ -65,7 +69,7 @@ public static string Chrome
// format the user-agent string from the various component parts
string userAgent = string.Format(CultureInfo.InvariantCulture,
"{0} ({1}; {2}; {3}) AppleWebKit/534.6 (KHTML, like Gecko) Chrome/7.0.500.0 Safari/534.6",
Compatibility, Platform, OS, Culture);
Compatibility, PlatformName, OS, Culture);
return (userAgent);
}
}
Expand All @@ -80,7 +84,7 @@ public static string Opera
// format the user-agent string from the various component parts
string userAgent = string.Format(CultureInfo.InvariantCulture,
"Opera/9.70 ({0}; {1}; {2}) Presto/2.2.1",
Platform, OS, Culture);
PlatformName, OS, Culture);
return (userAgent);
}
}
Expand All @@ -95,7 +99,7 @@ public static string Safari
// format the user-agent string from the various component parts
string userAgent = string.Format(CultureInfo.InvariantCulture,
"{0} ({1}; {2}; {3}) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16",
Compatibility, Platform, OS, Culture);
Compatibility, PlatformName, OS, Culture);
return (userAgent);
}
}
Expand All @@ -118,19 +122,44 @@ internal static string App
}
}

internal static string Platform
internal static string PlatformName
{
get
{
return ("Windows NT");
if (Platform.IsWindows)
{
// only generate the windows user agent once
if(s_windowsUserAgent == null){
// find the version in the windows operating system description
string versionText = OS.Substring(OS.LastIndexOf(" ") +1);
Version windowsPlatformversion = new Version(versionText);
s_windowsUserAgent = $"Windows NT {windowsPlatformversion.Major}.{windowsPlatformversion.Minor}";
}

return s_windowsUserAgent;
}
else if (Platform.IsMacOS)
{
return "Macintosh";
}
else if (Platform.IsLinux)
{
return "Linux";
}
else
{
// unknown/unsupported platform
Diagnostics.Assert(false, "Unable to determine Operating System Platform");
return String.Empty;
}
}
}

internal static string OS
{
get
{
return System.Runtime.InteropServices.RuntimeInformation.OSDescription;
return RuntimeInformation.OSDescription.Trim();
}
}

Expand Down
Expand Up @@ -453,8 +453,22 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
$response.Output.Content | Should Not Be $null
}

It "Invoke-WebRequest returns User-Agent" {
#User-Agent changes on different platforms, so tests should only be run if on the correct platform
It "Invoke-WebRequest returns Correct User-Agent on MacOSX" -Skip:(!$IsMacOS) {

$uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-WebRequest -Uri '$uri' -TimeoutSec 5"

$result = ExecuteWebCommand -command $command
ValidateResponse -response $result

# Validate response content
$jsonContent = $result.Output.Content | ConvertFrom-Json
$jsonContent.headers.'User-Agent' | Should MatchExactly '.*\(Macintosh;.*\) PowerShell\/\d+\.\d+\.\d+.*'
}

It "Invoke-WebRequest returns Correct User-Agent on Linux" -Skip:(!$IsLinux) {

$uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-WebRequest -Uri '$uri' -TimeoutSec 5"

Expand All @@ -463,7 +477,20 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {

# Validate response content
$jsonContent = $result.Output.Content | ConvertFrom-Json
$jsonContent.headers.'User-Agent' | Should MatchExactly '(?<!Windows)PowerShell\/\d+\.\d+\.\d+.*'
$jsonContent.headers.'User-Agent' | Should MatchExactly '.*\(Linux;.*\) PowerShell\/\d+\.\d+\.\d+.*'
}

It "Invoke-WebRequest returns Correct User-Agent on Windows" -Skip:(!$IsWindows) {

$uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-WebRequest -Uri '$uri' -TimeoutSec 5"

$result = ExecuteWebCommand -command $command
ValidateResponse -response $result

# Validate response content
$jsonContent = $result.Output.Content | ConvertFrom-Json
$jsonContent.headers.'User-Agent' | Should MatchExactly '.*\(Windows NT \d+\.\d*;.*\) PowerShell\/\d+\.\d+\.\d+.*'
}

It "Invoke-WebRequest returns headers dictionary" {
Expand Down Expand Up @@ -1305,15 +1332,38 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
$response.PowerShell.Dispose()
}

It "Invoke-RestMethod returns User-Agent" {
#User-Agent changes on different platforms, so tests should only be run if on the correct platform
It "Invoke-RestMethod returns Correct User-Agent on MacOSX" -Skip:(!$IsMacOS) {

$uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-RestMethod -Uri '$uri' -TimeoutSec 5"

$result = ExecuteWebCommand -command $command

# Validate response
$result.Output.headers.'User-Agent' | Should MatchExactly '.*\(Macintosh;.*\) PowerShell\/\d+\.\d+\.\d+.*'
}

It "Invoke-RestMethod returns Correct User-Agent on Linux" -Skip:(!$IsLinux) {

$uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-RestMethod -Uri '$uri' -TimeoutSec 5"

$result = ExecuteWebCommand -command $command

# Validate response
$result.Output.headers.'User-Agent' | Should MatchExactly '.*\(Linux;.*\) PowerShell\/\d+\.\d+\.\d+.*'
}

It "Invoke-RestMethod returns Correct User-Agent on Windows" -Skip:(!$IsWindows) {

$uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-RestMethod -Uri '$uri' -TimeoutSec 5"

$result = ExecuteWebCommand -command $command

# Validate response
$result.Output.headers.'User-Agent' | Should MatchExactly '(?<!Windows)PowerShell\/\d+\.\d+\.\d+.*'
$result.Output.headers.'User-Agent' | Should MatchExactly '.*\(Windows NT \d+\.\d*;.*\) PowerShell\/\d+\.\d+\.\d+.*'
}

It "Invoke-RestMethod returns headers dictionary" {
Expand Down