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

Conversation

LDSpits
Copy link
Contributor

@LDSpits LDSpits commented Sep 28, 2017

This pr fixes #4913, I have not written any tests, If these are required then please notify me of this and Ill add them

Copy link
Collaborator

@iSazonov iSazonov left a 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)){
Copy link
Collaborator

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");
Copy link
Collaborator

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)
Copy link
Collaborator

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)
Copy link
Collaborator

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".

Copy link
Contributor Author

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

Copy link
Collaborator

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?

@dantraMSFT
Copy link
Contributor

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.

@LDSpits LDSpits changed the title Powershell web useragent now takes the OS into account User Agent now reports the OS platform Sep 28, 2017
@daxian-dbw daxian-dbw self-assigned this Sep 28, 2017
else
{
// unknown/unsupported platform
Debug.Assert(true, "Unable to determine Operating System Platform");
Copy link
Member

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.

Copy link
Contributor Author

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.

@daxian-dbw
Copy link
Member

@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 user-agent.

Copy link
Contributor

@markekraus markekraus left a 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";
Copy link
Contributor

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.

Copy link
Collaborator

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iSazonov I agree.
@LDSpits you can use these tests as a base. Please include tests for both Invoke-WebRequest and Invoke-RestMethod

Copy link
Contributor Author

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.

Copy link
Contributor

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!

Copy link
Member

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 ... :)

Copy link
Collaborator

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.

Copy link
Member

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)

Copy link
Collaborator

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.

Copy link
Contributor

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 LDSpits changed the title User Agent now reports the OS platform [WIP] User Agent now reports the OS platform Sep 29, 2017
@daxian-dbw
Copy link
Member

@LDSpits One more thing, when you push new commits, please make sure the message of the last commit contains [Feature] in it so that it will trigger a full test run. Web cmdlets tests by default are not included in CI runs, so we need a full test run to validate changes related to them. Thanks!

{
// find the version in the windows operating system description
string versionText = PSUserAgent.OS.Substring(PSUserAgent.OS.TrimEnd().LastIndexOf(" ") +1);
Version windowsPlatformversion = new Version(versionText);
Copy link
Contributor

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

Copy link
Collaborator

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.

@LDSpits
Copy link
Contributor Author

LDSpits commented Oct 1, 2017

@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?

@markekraus
Copy link
Contributor

@LDSpits My preference would be for the tests to remain in WebCmdlets.tests.ps1 along with the other User-Agent test. They should be new It blocks in the same file preferably right after the current ones. Here is the one for Invoke-WebRequest:

It "Invoke-WebRequest returns User-Agent" {
$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)PowerShell\/\d+\.\d+\.\d+.*'
}

@LDSpits
Copy link
Contributor Author

LDSpits commented Oct 4, 2017

@iSazonov @daxian-dbw @markekraus can I ask a few questions about tests? I was in the process of adding tests for execute-webrequest. I have copied over most of the code from the Invoke-WebRequest returns User-Agent test with only changing the regex to '.*\(Windows NT \d+\.\d*;.*\) PowerShell\/\d+\.\d+\.\d+.*'. this test failed however. so I tested the regex using powershell (to ensure i was using the same regex parsing engine as pester). However this returned true. So now im puzzled as to why it failed. Could you maybe give some pointers?

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+.*'

@markekraus
Copy link
Contributor

markekraus commented Oct 4, 2017

@LDSpits It looks like you are not using a version of PowerShell Compiled with your changes. The pester output shows Mozilla/5.0 (Windows NT; Microsoft Windows 10.0.16296 ; nl-NL) PowerShell/6.0.0 as the user-agent. note that the <major>.<minor> is missing after Windows NT

@LDSpits LDSpits changed the title [WIP] User Agent now reports the OS platform User Agent now reports the OS platform Oct 5, 2017
@@ -13,14 +14,17 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
public static class PSUserAgent
{

private static string _windowsUserAgent;
Copy link
Collaborator

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){
Copy link
Collaborator

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);
Copy link
Collaborator

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) {
Copy link
Collaborator

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.

@LDSpits
Copy link
Contributor Author

LDSpits commented Oct 6, 2017

@iSazonov @daxian-dbw @markekraus the Travis build is erroring? had a problem with the previous build on the http proxy tests...

@markekraus
Copy link
Contributor

markekraus commented Oct 6, 2017

@LDSpits Its related to #5005. We need to fix the other delay tests. I'll create an issue for it. PR #5035

@daxian-dbw daxian-dbw dismissed their stale review October 6, 2017 18:14

New commits have been pushed.

@daxian-dbw
Copy link
Member

#5035 has been merged and I restarted the CI builds. Hopefully the CI will pass this time.

@LDSpits
Copy link
Contributor Author

LDSpits commented Oct 6, 2017

Let's hope they'll pass this time 😄

Copy link
Contributor

@markekraus markekraus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@daxian-dbw daxian-dbw merged commit e1a0caf into PowerShell:master Oct 6, 2017
@daxian-dbw
Copy link
Member

@LDSpits Thanks for your contribution!
@markekraus @iSazonov Thank you for the thorough review!

@markekraus
Copy link
Contributor

@LDSpits Thanks for doing this!

@LDSpits
Copy link
Contributor Author

LDSpits commented Oct 6, 2017

@daxian-dbw @iSazonov @markekraus No problem, expect to see me around 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

User Agent Statically Reports "Windows NT" as the platform.
5 participants