-
Notifications
You must be signed in to change notification settings - Fork 7.6k
User Agent now reports the OS platform #4937
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LDSpits Thanks for your contribution!
@@ -122,15 +123,24 @@ internal static string Platform | |||
{ | |||
get | |||
{ | |||
return ("Windows NT"); | |||
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest use public static class Platform
from CorePsPlatform.cs and rename Platform
in PSUserAgent.cs to PSUserAgentPlatform.
Also please use common formatting pattern:
if ( ... )
{
...
}
else if ( ... )
{
...
}
return "Linux"; | ||
}else{ | ||
// unknown/unsupported platform | ||
throw new PlatformNotSupportedException("Platform is not supported"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe better Debug.Assert
and returm String.Empty?
…f unsupported platform, using system.management.automation.Platform
@@ -123,15 +124,23 @@ internal static string Platform | |||
{ | |||
get | |||
{ | |||
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)){ | |||
if(System.Management.Automation.Platform.IsWindows) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a space "if". Below too.
return "Windows NT"; | ||
}else if(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)){ | ||
} | ||
else if(System.Management.Automation.Platform.IsMacOS) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Above I suggest rename internal Platform property to exclude using "System.Management.Automation".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any preference on the name? how about OSPlatform? @iSazonov
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a string name - maybe PlatformName?
Minor Nit: I suggest you change the title of the PR to something like 'User Agent now reports the OS platform'. When I first read the title, I was expecting to find a behavioral change that is dependent on the OS platform. |
else | ||
{ | ||
// unknown/unsupported platform | ||
Debug.Assert(true, "Unable to determine Operating System Platform"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be Debug.Assert(false, "...")
. And it's recommended to use Diagnostics.Assert
which is from the System.Management.Automation
namespace. See the code here as an example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@daxian-dbw I have adressed your feedback.
@LDSpits Thanks for the update. Can you please add corresponding tests? You can use https://github.com/PowerShell/PowerShell/blob/master/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1#L456 as an example to get |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add tests.
return ("Windows NT"); | ||
if (Platform.IsWindows) | ||
{ | ||
return "Windows NT"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should add tests for these. These could either be a separate test or the existing 'User-Agent` tests could be expanded.
PowerShell/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
Line 456 in b07f24e
It "Invoke-WebRequest returns User-Agent" { |
PowerShell/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
Line 1308 in b07f24e
It "Invoke-RestMethod returns User-Agent" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test should be patform sensetive - I'd prefer new separate test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alright, will start work on them later today.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LDSpits Also, it looks like the Windows platform part is supposed to include the <major>.<minor>
version of windows (6.1, 10.0, etc.). I wouldn't call this a requirement, but if you could add that too it would be great!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iSazonov I actually personally like parsing [RuntimeInformation]::OSDescription
. The parsing is a one-time effort and pretty easy. I know you might concern about the consistency of the string value returned from this property ... :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Ex. - What about Unix? I wouldn't trust OSDescription
. I'd prefer to discuss all our User-Agent string (properties) before enhance this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need the version info for Linux. This is what I got when using chrome to send request to http://httpbin.org/user-agent
:
Windows: (Windows NT 10.0; Win64; x64)
Ubuntu: (X11; Linux x86_64)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see FireFox. Specially MacOS and Android.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@daxian-dbw is correct. The OS version numbers in the platform are only sent for Windows platforms. Linux platforms send either X11
or Linux
(Linux
is used more in CLI based HTTP agents). macOS sends Machintosh
. None of those with versions.
@LDSpits One more thing, when you push new commits, please make sure the message of the last commit contains |
{ | ||
// find the version in the windows operating system description | ||
string versionText = PSUserAgent.OS.Substring(PSUserAgent.OS.TrimEnd().LastIndexOf(" ") +1); | ||
Version windowsPlatformversion = new Version(versionText); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this would be dynamically generated each time the Web Cmdlets are called, but is static beyond the initial call, perhaps adding a private field _windowsPlatformversionString
(or something) generated once on the first call (if (_windowsPlatformversionString == null)
) and then reference it directly every time thereafter. return _windowsPlatformversionString
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is good candidate for private static variable and private static initialization function.
@iSazonov @daxian-dbw @markekraus Is it really nessesary to move the tests to a new file? shouldn't we add the test to WebCmdlets.tests.ps1 since most of the infrastructure required for the tests is already inside of there? |
@LDSpits My preference would be for the tests to remain in PowerShell/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 Lines 456 to 467 in 2e901e5
|
@iSazonov @daxian-dbw @markekraus can I ask a few questions about tests? I was in the process of adding tests for powershell code used to test regex: PS C:\Users\spits\Desktop\Development\Powershell\PowerShell> $bingdings =[System.Reflection.BindingFlags]::NonPublic -b
xor [System.Reflection.BindingFlags]::Static
PS C:\Users\spits\Desktop\Development\Powershell\PowerShell> [Microsoft.PowerShell.Commands.PSUserAgent].GetProperty('Us
erAgent',$bingdings).GetValue($null,$null) -match '.*\(Windows NT \d+\.\d*;.*\) PowerShell\/\d+\.\d+\.\d+.*'
True Current (windows) test: It "Invoke-WebRequest returns Correct User-Agent on Windows" {
$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+.*'
} pester test message: Describing Invoke-WebRequest tests
[-] Invoke-WebRequest returns Correct User-Agent on MacOSX 6.87s
Expected: {Mozilla/5.0 (Windows NT; Microsoft Windows 10.0.16296 ; nl-NL) PowerShell/6.0.0} to exactly match the expression {.*\(Macintosh;.*\) PowerShell\/\d+\.\d+\.\d+.*}
at line: 466 in C:\Users\spits\Desktop\Development\Powershell\PowerShell\test\powershell\Modules\Microsoft.PowerShell
.Utility\WebCmdlets.Tests.ps1
466: $jsonContent.headers.'User-Agent' | Should MatchExactly '.*\(Macintosh;.*\) PowerShell\/\d+\.\d+\.\d+.*'
[-] Invoke-WebRequest returns Correct User-Agent on Linux 1.14s
Expected: {Mozilla/5.0 (Windows NT; Microsoft Windows 10.0.16296 ; nl-NL) PowerShell/6.0.0} to exactly match the expr
ession {.*\(Linux;.*\) PowerShell\/\d+\.\d+\.\d+.*}
at line: 479 in C:\Users\spits\Desktop\Development\Powershell\PowerShell\test\powershell\Modules\Microsoft.PowerShell
.Utility\WebCmdlets.Tests.ps1
479: $jsonContent.headers.'User-Agent' | Should MatchExactly '.*\(Linux;.*\) PowerShell\/\d+\.\d+\.\d+.*'
[-] Invoke-WebRequest returns Correct User-Agent on Windows 1.08s
Expected: {Mozilla/5.0 (Windows NT; Microsoft Windows 10.0.16296 ; nl-NL) PowerShell/6.0.0} to exactly match the expression {.*\(Windows NT \d+\.\d*;.*\) PowerShell\/\d+\.\d+\.\d+.*}
at line: 492 in C:\Users\spits\Desktop\Development\Powershell\PowerShell\test\powershell\Modules\Microsoft.PowerShell
.Utility\WebCmdlets.Tests.ps1
492: $jsonContent.headers.'User-Agent' | Should MatchExactly '.*\(Windows NT \d+\.\d*;.*\) PowerShell\/\d+\.\
d+\.\d+.*' |
@LDSpits It looks like you are not using a version of PowerShell Compiled with your changes. The pester output shows |
@@ -13,14 +14,17 @@ namespace Microsoft.PowerShell.Commands | |||
/// </summary> | |||
public static class PSUserAgent | |||
{ | |||
|
|||
private static string _windowsUserAgent; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use 's_' prefix for static variables.
if (Platform.IsWindows) | ||
{ | ||
// only generate the windows user agent once | ||
if(PSUserAgent._windowsUserAgent == null){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove 'PSUserAgent' prefix. Below too.
// only generate the windows user agent once | ||
if(PSUserAgent._windowsUserAgent == null){ | ||
// find the version in the windows operating system description | ||
string versionText = PSUserAgent.OS.Substring(PSUserAgent.OS.TrimEnd().LastIndexOf(" ") +1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need TrimEnd()
?
If so it is better mobe it in OS property.
Please remove 'PSUserAgent' prefix.
@@ -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" -Pending:(!$IsMacOS) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use Pending
to temporary turn out broken tests.
Please use Skip
. Below too.
@iSazonov @daxian-dbw @markekraus the Travis build is erroring? had a problem with the previous build on the http proxy tests... |
#5035 has been merged and I restarted the CI builds. Hopefully the CI will pass this time. |
Let's hope they'll pass this time 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@LDSpits Thanks for your contribution! |
@LDSpits Thanks for doing this! |
@daxian-dbw @iSazonov @markekraus No problem, expect to see me around 😄 |
This pr fixes #4913, I have not written any tests, If these are required then please notify me of this and Ill add them