-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathvalidate-sample.ps1
More file actions
85 lines (72 loc) · 2.84 KB
/
Copy pathvalidate-sample.ps1
File metadata and controls
85 lines (72 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
param(
[switch]$SkipInstall,
[switch]$SkipTests,
[switch]$SkipBrowser,
[switch]$KeepProcesses,
[switch]$Headed,
[int]$TimeoutSec = 60
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
. (Join-Path $PSScriptRoot '../../Tools/powershell/SampleValidation.ps1')
$appRoot = $PSScriptRoot
$packageRoot = Join-Path $appRoot 'src'
$nodeEnvironment = Get-ValidationNodeEnvironment
$runtimeHandle = $null
try {
Write-Step 'Preflight checks'
Assert-CommandExists 'node'
Assert-CommandExists 'npm'
if (-not $SkipInstall) {
Write-Step 'Installing dependencies'
Invoke-ExternalCommand -FilePath 'npm' -Arguments @('install') -WorkingDirectory $packageRoot -Environment $nodeEnvironment
}
Write-Host 'No automated test script is defined for this sample.' -ForegroundColor Yellow
Write-Step 'Starting webhook listener'
$logPath = New-ValidationLogPath -WorkingDirectory $appRoot -Name 'webhook'
$runtimeHandle = Start-LoggedProcess -FilePath 'npm' -Arguments @('run', 'start') -WorkingDirectory $packageRoot -LogPath $logPath -Environment (Merge-EnvironmentTables @($nodeEnvironment, @{ PORT = '3000' }))
$validationToken = 'sample-validation-token'
$validationUrl = "http://127.0.0.1:3000/webhook?validationToken=$validationToken"
$deadline = (Get-Date).AddSeconds($TimeoutSec)
$validated = $false
while ((Get-Date) -lt $deadline) {
if ($runtimeHandle.Process.HasExited) {
$tail = Get-LogTail -Path $runtimeHandle.LogPath
throw "Webhook listener exited before responding. Recent log output:`n$tail"
}
try {
$invokeWebRequestArguments = @{
Method = 'Post'
Uri = $validationUrl
TimeoutSec = 5
ContentType = 'application/json'
Body = '{}'
}
if ((Get-Command Invoke-WebRequest).Parameters.ContainsKey('UseBasicParsing')) {
$invokeWebRequestArguments['UseBasicParsing'] = $true
}
$response = Invoke-WebRequest @invokeWebRequestArguments
if ([string]$response.Content -eq $validationToken) {
$validated = $true
break
}
}
catch {
}
Start-Sleep -Milliseconds 500
}
if (-not $validated) {
throw 'Webhook validation token echo check did not succeed.'
}
Write-Host 'Webhook sample validation completed.' -ForegroundColor Green
Write-ValidationSummary -Status 'PASS' -Message 'Webhook listener startup and validation-token echo checks passed.'
}
catch {
Write-ValidationSummary -Status 'FAIL' -Message $_.Exception.Message
throw
}
finally {
if ($null -ne $runtimeHandle -and -not $KeepProcesses) {
Stop-LoggedProcess -Handle $runtimeHandle
}
}