User Agent now reports the OS platform#4937
User Agent now reports the OS platform#4937daxian-dbw merged 12 commits intoPowerShell:masterfrom LDSpits:hotfix/correct-useragent
Conversation
| get | ||
| { | ||
| return ("Windows NT"); | ||
| if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)){ |
There was a problem hiding this comment.
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.
Maybe better Debug.Assert and returm String.Empty?
…f unsupported platform, using system.management.automation.Platform
| get | ||
| { | ||
| if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)){ | ||
| if(System.Management.Automation.Platform.IsWindows) |
There was a problem hiding this comment.
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.
Above I suggest rename internal Platform property to exclude using "System.Management.Automation".
There was a problem hiding this comment.
any preference on the name? how about OSPlatform? @iSazonov
There was a problem hiding this comment.
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.
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.
@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 |
| return ("Windows NT"); | ||
| if (Platform.IsWindows) | ||
| { | ||
| return "Windows NT"; |
There was a problem hiding this comment.
I think we should add tests for these. These could either be a separate test or the existing 'User-Agent` tests could be expanded.
There was a problem hiding this comment.
The test should be patform sensetive - I'd prefer new separate test.
There was a problem hiding this comment.
alright, will start work on them later today.
There was a problem hiding this comment.
@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.
@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.
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.
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.
Please see FireFox. Specially MacOS and Android.
There was a problem hiding this comment.
@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.
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.
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 |
|
@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+.*'
TrueCurrent (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 |
| public static class PSUserAgent | ||
| { | ||
|
|
||
| private static string _windowsUserAgent; |
There was a problem hiding this comment.
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.
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.
Do we really need TrimEnd()?
If so it is better mobe it in OS property.
Please remove 'PSUserAgent' prefix.
|
|
||
| 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.
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 😄 |
|
@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