PowerShell/test/tools/Modules/WebListener/WebListener.psm1

158 lines
3.9 KiB
PowerShell
Raw Normal View History

Add test WebListener module and tests for Web Cmdlet Certificate Authentication (#4622) Introduce new test module 'WebListener.psm1'. Now web HTTPS tests can use it to exclude using external sites. PowerShell/PowerShell#4609 * [Feature] Add Tests for Web Cmdlet Certificate Authentication PowerShell/PowerShell#4609 * [feature] Add new app to Publish-PSTestTools refactor tests also add ASP.NET to .spelling * [feature] spelling fix * [feature] revert badssl changes * [feature] Impliment suggestions * [feature] Spelling, var rename, port 8443 to 8083 rebase fix conflict * [feature] Rename to HttpsListener and Module-ize . * [feature] password protect ClientCert to fix macOS import issue * [feature] Rename to WebListener * Rename HttpsListener to WebListener * Switch Listener from Razor pages to MVC * Address PR feedback * Adjust tests * [feature] Address PR feedback * [feature] Replace missing smeicolons * [feature] Address PR Feedback * [feature] Cleanup and minor fix * Enum was not used * GetStatus() was not accessing the correct property chain * Added -Test param to make URL generation smoother in test code and to fix double / issues * [feature] More minor fixes * Https when it matters. * Expand property... not exclude.. * Remove superfluous and outdated ToString() override * [Feature] Move ClientCeret.pfx to WebListener Module * Move the cert * Adjust Get-WebListenerClientCertificate * Remove cert from csproj * ActionResult -> JsonResult (was mistakenly left as ActionResult during testing).. * [Feature] Move ServerCert.pfx to Module * Move cert * Upate csproj * Update module * Add/Update README.md's CI Retest.
2017-08-31 11:30:35 +02:00
Class WebListener
{
[int]$HttpPort
[int]$HttpsPort
[System.Management.Automation.Job]$Job
WebListener () { }
[String] GetStatus()
{
return $This.Job.JobStateInfo.State
}
}
[WebListener]$WebListener
function Get-WebListener
{
[CmdletBinding(ConfirmImpact = 'Low')]
[OutputType([WebListener])]
param()
process
{
return [WebListener]$Script:WebListener
}
}
function Start-WebListener
{
[CmdletBinding(ConfirmImpact = 'Low')]
[OutputType([WebListener])]
param
(
[ValidateRange(1,65535)]
[int]$HttpPort = 8083,
[ValidateRange(1,65535)]
[int]$HttpsPort = 8084
)
process
{
$runningListener = Get-WebListener
if ($null -ne $runningListener -and $runningListener.GetStatus() -eq 'Running')
{
return $runningListener
}
$initTimeoutSeconds = 15
$appDll = 'WebListener.dll'
$serverPfx = 'ServerCert.pfx'
$serverPfxPassword = 'password'
$initCompleteMessage = 'Now listening on'
$serverPfxPath = Join-Path $MyInvocation.MyCommand.Module.ModuleBase $serverPfx
$timeOut = (get-date).AddSeconds($initTimeoutSeconds)
$Job = Start-Job {
$path = Split-Path -parent (get-command WebListener).Path
Push-Location $path
dotnet $using:appDll $using:serverPfxPath $using:serverPfxPassword $using:HttpPort $using:HttpsPort
}
$Script:WebListener = [WebListener]@{
HttpPort = $HttpPort
HttpsPort = $HttpsPort
Job = $Job
}
# Wait until the app is running or until the initTimeoutSeconds have been reached
do
{
Start-Sleep -Milliseconds 100
$initStatus = $Job.ChildJobs[0].Output | Out-String
$isRunning = $initStatus -match $initCompleteMessage
}
while (-not $isRunning -and (get-date) -lt $timeOut)
if (-not $isRunning)
{
$Job | Stop-Job -PassThru | Receive-Job
$Job | Remove-Job
throw 'WebListener did not start before the timeout was reached.'
}
return $Script:WebListener
}
}
function Stop-WebListener
{
[CmdletBinding(ConfirmImpact = 'Low')]
[OutputType([Void])]
param()
process
{
$Script:WebListener.job | Stop-Job -PassThru | Remove-Job
$Script:WebListener = $null
}
}
function Get-WebListenerClientCertificate {
[CmdletBinding(ConfirmImpact = 'Low')]
[OutputType([System.Security.Cryptography.X509Certificates.X509Certificate2])]
param()
process {
$pfxPath = Join-Path $MyInvocation.MyCommand.Module.ModuleBase 'ClientCert.pfx'
[System.Security.Cryptography.X509Certificates.X509Certificate2]::new($pfxPath,'password')
}
}
function Get-WebListenerUrl {
[CmdletBinding()]
[OutputType([Uri])]
param (
[switch]$Https,
[ValidateSet(
'Cert',
'Compression',
'Delay',
'Encoding',
'Get',
'Home',
'Multipart',
'Redirect',
'/'
)]
[String]$Test,
[String]$TestValue
Add test WebListener module and tests for Web Cmdlet Certificate Authentication (#4622) Introduce new test module 'WebListener.psm1'. Now web HTTPS tests can use it to exclude using external sites. PowerShell/PowerShell#4609 * [Feature] Add Tests for Web Cmdlet Certificate Authentication PowerShell/PowerShell#4609 * [feature] Add new app to Publish-PSTestTools refactor tests also add ASP.NET to .spelling * [feature] spelling fix * [feature] revert badssl changes * [feature] Impliment suggestions * [feature] Spelling, var rename, port 8443 to 8083 rebase fix conflict * [feature] Rename to HttpsListener and Module-ize . * [feature] password protect ClientCert to fix macOS import issue * [feature] Rename to WebListener * Rename HttpsListener to WebListener * Switch Listener from Razor pages to MVC * Address PR feedback * Adjust tests * [feature] Address PR feedback * [feature] Replace missing smeicolons * [feature] Address PR Feedback * [feature] Cleanup and minor fix * Enum was not used * GetStatus() was not accessing the correct property chain * Added -Test param to make URL generation smoother in test code and to fix double / issues * [feature] More minor fixes * Https when it matters. * Expand property... not exclude.. * Remove superfluous and outdated ToString() override * [Feature] Move ClientCeret.pfx to WebListener Module * Move the cert * Adjust Get-WebListenerClientCertificate * Remove cert from csproj * ActionResult -> JsonResult (was mistakenly left as ActionResult during testing).. * [Feature] Move ServerCert.pfx to Module * Move cert * Upate csproj * Update module * Add/Update README.md's CI Retest.
2017-08-31 11:30:35 +02:00
)
process {
$runningListener = Get-WebListener
if ($null -eq $runningListener -or $runningListener.GetStatus() -ne 'Running')
{
return $null
}
$Uri = [System.UriBuilder]::new()
$Uri.Host = 'localhost'
$Uri.Port = $runningListener.HttpPort
$Uri.Scheme = 'Http'
if ($Https.IsPresent)
{
$Uri.Port = $runningListener.HttpsPort
$Uri.Scheme = 'Https'
}
if ($TestValue)
{
$Uri.Path = '{0}/{1}' -f $Test, $TestValue
}
else
{
$Uri.Path = $Test
}
Add test WebListener module and tests for Web Cmdlet Certificate Authentication (#4622) Introduce new test module 'WebListener.psm1'. Now web HTTPS tests can use it to exclude using external sites. PowerShell/PowerShell#4609 * [Feature] Add Tests for Web Cmdlet Certificate Authentication PowerShell/PowerShell#4609 * [feature] Add new app to Publish-PSTestTools refactor tests also add ASP.NET to .spelling * [feature] spelling fix * [feature] revert badssl changes * [feature] Impliment suggestions * [feature] Spelling, var rename, port 8443 to 8083 rebase fix conflict * [feature] Rename to HttpsListener and Module-ize . * [feature] password protect ClientCert to fix macOS import issue * [feature] Rename to WebListener * Rename HttpsListener to WebListener * Switch Listener from Razor pages to MVC * Address PR feedback * Adjust tests * [feature] Address PR feedback * [feature] Replace missing smeicolons * [feature] Address PR Feedback * [feature] Cleanup and minor fix * Enum was not used * GetStatus() was not accessing the correct property chain * Added -Test param to make URL generation smoother in test code and to fix double / issues * [feature] More minor fixes * Https when it matters. * Expand property... not exclude.. * Remove superfluous and outdated ToString() override * [Feature] Move ClientCeret.pfx to WebListener Module * Move the cert * Adjust Get-WebListenerClientCertificate * Remove cert from csproj * ActionResult -> JsonResult (was mistakenly left as ActionResult during testing).. * [Feature] Move ServerCert.pfx to Module * Move cert * Upate csproj * Update module * Add/Update README.md's CI Retest.
2017-08-31 11:30:35 +02:00
return [Uri]$Uri.ToString()
}
}