Skip to content

Conversation

@markekraus
Copy link
Contributor

fixes #4911

  • renames the App portion of the user agent to PowerShell
  • Adjusts tests.

Copy link
Member

@SteveL-MSFT SteveL-MSFT left a comment

Choose a reason for hiding this comment

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

Thanks!

$jsonContent = $result.Output | ConvertFrom-Json
$jsonContent.headers.Host | Should Be $uri.Authority
$jsonContent.headers.'User-Agent' | Should Match "WindowsPowerShell"
$jsonContent.headers.'User-Agent' | Should Match "PowerShell"
Copy link
Member

Choose a reason for hiding this comment

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

This match would work with WindowsPowerShell as well. This should be ^PowerShell.

In addition to that, it should be MatchExactly

Copy link
Collaborator

@iSazonov iSazonov Sep 25, 2017

Choose a reason for hiding this comment

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

User-Agent can be PowerShell/6.0.0-Beta.8 or PowerShell/6.0.0- so if we use MatchExactly the pattern should be "^PowerShell/\d+\.\d+\.\d+.*"
We use the pattern many times - it make sense to use a variable.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense.

Copy link
Contributor Author

@markekraus markekraus Sep 25, 2017

Choose a reason for hiding this comment

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

It was doing an imprecise partial match before.

It's actually more like Mozilla/5.0 (Windows NT; Microsoft Windows 10.0.15063 ; en-US) WindowsPowerShell/6.0.0 and it will be more dynamic "soon"

we could pull the exact one through reflection in BeforeAll. Honestly, I'm not even sure this is a good test. Should Be $uri.Authority should be sufficient, IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.


# Validate response
$result.Output.headers.'User-Agent' | Should Match "WindowsPowerShell"
$result.Output.headers.'User-Agent' | Should Match "PowerShell"
Copy link
Member

Choose a reason for hiding this comment

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

Same as above.

# Validate response
$result.Output.headers.Host | Should Be $Uri.Authority
$result.Output.headers.'User-Agent' | Should Match "WindowsPowerShell"
$result.Output.headers.'User-Agent' | Should Match "PowerShell"
Copy link
Member

Choose a reason for hiding this comment

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

Same as above

# Validate response
$result.Output.headers.Host | Should Be $uri.Authority
$result.Output.headers.'User-Agent' | Should Match "WindowsPowerShell"
$result.Output.headers.'User-Agent' | Should Match "PowerShell"
Copy link
Member

Choose a reason for hiding this comment

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

Same as above

# Validate response
$result.Output.headers.Host | Should Match $uri.Authority
$result.Output.headers.'User-Agent' | Should Match "WindowsPowerShell"
$result.Output.headers.'User-Agent' | Should Match "PowerShell"
Copy link
Member

Choose a reason for hiding this comment

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

Same as above


# Validate response
$result.Output.headers.'User-Agent' | Should Match "WindowsPowerShell"
$result.Output.headers.'User-Agent' | Should Match "PowerShell"
Copy link
Member

Choose a reason for hiding this comment

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

Same as above

# Validate response
$result.Output.url | Should Match $uri
$result.Output.headers.'User-Agent' | Should Match "WindowsPowerShell"
$result.Output.headers.'User-Agent' | Should Match "PowerShell"
Copy link
Member

Choose a reason for hiding this comment

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

Same as above

# Validate response
$result.Output.url | Should Match $uri
$result.Output.headers.'User-Agent' | Should Match "WindowsPowerShell"
$result.Output.headers.'User-Agent' | Should Match "PowerShell"
Copy link
Member

Choose a reason for hiding this comment

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

Same as above

$jsonContent = $result.Output | ConvertFrom-Json
$jsonContent.headers.Host | Should Be $uri.Authority
$jsonContent.headers.'User-Agent' | Should Match "WindowsPowerShell"
$jsonContent.headers.'User-Agent' | Should Match "PowerShell"
Copy link
Member

Choose a reason for hiding this comment

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

Same as above

@markekraus
Copy link
Contributor Author

Test fails due to #4919

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.

Leave a comment

if ( test-path /etc/centos-release ) { $PendingCertificateTest = $true }

# Get the default UserAgent through reflection
$DefaultUserAgent = [Microsoft.PowerShell.Commands.PSUserAgent].GetProperty('UserAgent',@('Static','NonPublic')).GetValue($null,$null)
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 dummy test: if C# code will be changed the test will be automatically passed. I believe we should use explicit desired pattern.
Also please move to BeforeAll.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's not the purpose of the tests. They are just testing the response from the server to ensure that expected data sent is also returned. if we want to test the patterns in the UserAgent, that would be a separate set of tests for the PSUserAgent class.

I will add a separate describe block for PSUserAgent tests and a test for the App member (there will be other tests possibly for #4193). I will create a separate issue to add and track adding other tests for PSUserAgent.

Describe "PSUserAgent Tests" {
It "App Should Match ^PowerShell/\d+\.\d+\.\d+.*" {
$app = [Microsoft.PowerShell.Commands.PSUserAgent].GetProperty('App',@('Static','NonPublic')).GetValue($null,$null)
$app | Should Match '^PowerShell/\d+\.\d+\.\d+.*'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here I don't see the point of checking internal property - we are free to change (add/remove) internals in any time. We should test public API. Here "public API" is User-Agent which we send to a server and get it back. We should test the User-Agent format if we want to control the User-Agent. So it is good to use regex pattern. I think that check User-Agent 14 times it's redundant - we need to have one check for each web cmdlet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again, the point of most of those tests is not to test the User-Agent that is sent. Most of those assertions are being used to ensure meaningful data is returned in a response after a set of parameters has been used to make web requests. The only reason I updated them at all was because the change from WindowsPowerShell to PowerShell would have caused all these tests to false fail.

I will change the two tests that are explicitly about the User-Agent.

Copy link
Collaborator

Choose a reason for hiding this comment

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

As you mentioned $uri.Authority is sufficient. So I'd remove $DefaultUserAgent at all. I unlike it as you see 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm ok with that. This entire file needs to be more deeply analyzed and cleaned up. Maybe after I'm done making radical changes to it every day 😆.

# Validate response content
$jsonContent = $result.Output.Content | ConvertFrom-Json
$jsonContent.headers.'User-Agent' | Should Match "WindowsPowerShell"
$jsonContent.headers.'User-Agent' | Should Match '(?<!Windows)PowerShell\/\d+\.\d+\.\d+.*'
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you might use MatchExactly which does a case sensitive match (-cmatch). Especially since you're explicitly not matching WindowsPowerShell

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Copy link
Member

@adityapatwardhan adityapatwardhan left a comment

Choose a reason for hiding this comment

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

LGTM

@PowerShell PowerShell deleted a comment from msftclas Sep 27, 2017
@adityapatwardhan adityapatwardhan merged commit a8e8b1f into PowerShell:master Sep 27, 2017
@markekraus markekraus deleted the FixDefaultUserAgent branch January 19, 2018 18:57
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.

Default User Agent Reports WindowsPowerShell

7 participants