forked from Badgerati/Pode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Badgerati#546: adds tests for timers, schedules, endpoints, and others
- Loading branch information
Showing
10 changed files
with
187 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
Describe 'Endpoint Requests' { | ||
|
||
BeforeAll { | ||
$Port1 = 9000 | ||
$Endpoint1 = "http://localhost:$($Port1)" | ||
|
||
$Port2 = 9001 | ||
$Endpoint2 = "http://localhost:$($Port2)" | ||
|
||
Start-Job -Name 'Pode' -ErrorAction Stop -ScriptBlock { | ||
Import-Module -Name "$($using:PSScriptRoot)\..\..\src\Pode.psm1" | ||
|
||
Start-PodeServer -RootPath $using:PSScriptRoot -Type Pode { | ||
Add-PodeEndpoint -Address localhost -Port $using:Port1 -Protocol Http -Name 'Endpoint1' | ||
Add-PodeEndpoint -Address localhost -Port $using:Port2 -Protocol Http -Name 'Endpoint2' | ||
|
||
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging | ||
Add-PodeRoute -Method Get -Path '/close' -ScriptBlock { | ||
Close-PodeServer | ||
} | ||
|
||
Add-PodeRoute -Method Get -Path '/ping-1' -EndpointName 'Endpoint1' -ScriptBlock { | ||
Write-PodeJsonResponse -Value @{ Result = 'Pong1' } | ||
} | ||
|
||
Add-PodeRoute -Method Get -Path '/ping-2' -EndpointName 'Endpoint2' -ScriptBlock { | ||
Write-PodeJsonResponse -Value @{ Result = 'Pong2' } | ||
} | ||
|
||
Add-PodeRoute -Method Get -Path '/ping-all' -EndpointName 'Endpoint1', 'Endpoint2' -ScriptBlock { | ||
Write-PodeJsonResponse -Value @{ Result = 'PongAll' } | ||
} | ||
} | ||
} | ||
|
||
Start-Sleep -Seconds 3 | ||
} | ||
|
||
AfterAll { | ||
Invoke-RestMethod -Uri "$($Endpoint1)/close" -Method Get | Out-Null | ||
Get-Job -Name 'Pode' | Remove-Job -Force | ||
} | ||
|
||
|
||
It 'responds back with pong1' { | ||
$result = Invoke-RestMethod -Uri "$($Endpoint1)/ping-1" -Method Get | ||
$result.Result | Should Be 'Pong1' | ||
} | ||
|
||
It 'fails pong1 on second endpoint' { | ||
{ Invoke-RestMethod -Uri "$($Endpoint2)/ping-1" -Method Get -ErrorAction Stop } | Should Throw '404' | ||
} | ||
|
||
It 'responds back with pong2' { | ||
$result = Invoke-RestMethod -Uri "$($Endpoint2)/ping-2" -Method Get | ||
$result.Result | Should Be 'Pong2' | ||
} | ||
|
||
It 'fails pong2 on first endpoint' { | ||
{ Invoke-RestMethod -Uri "$($Endpoint1)/ping-2" -Method Get -ErrorAction Stop } | Should Throw '404' | ||
} | ||
|
||
It 'responds back with pong all' { | ||
$result = Invoke-RestMethod -Uri "$($Endpoint1)/ping-all" -Method Get | ||
$result.Result | Should Be 'PongAll' | ||
$result = Invoke-RestMethod -Uri "$($Endpoint2)/ping-all" -Method Get | ||
$result.Result | Should Be 'PongAll' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
Describe 'Schedules' { | ||
|
||
BeforeAll { | ||
$Port = 9000 | ||
$Endpoint = "http://localhost:$($Port)" | ||
|
||
Start-Job -Name 'Pode' -ErrorAction Stop -ScriptBlock { | ||
Import-Module -Name "$($using:PSScriptRoot)\..\..\src\Pode.psm1" | ||
|
||
Start-PodeServer -RootPath $using:PSScriptRoot { | ||
Add-PodeEndpoint -Address localhost -Port $using:Port -Protocol Http | ||
|
||
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging | ||
Add-PodeRoute -Method Get -Path '/close' -ScriptBlock { | ||
Close-PodeServer | ||
} | ||
|
||
# test1 | ||
Set-PodeState -Name 'Test1' -Value 0 | ||
Add-PodeSchedule -Name 'Test1' -Cron '* * * * *' -ScriptBlock { | ||
Set-PodeState -Name 'Test1' -Value 1337 | ||
} | ||
|
||
Add-PodeRoute -Method Get -Path '/test1' -ScriptBlock { | ||
Invoke-PodeSchedule -Name 'Test1' | ||
Start-Sleep -Seconds 2 | ||
Write-PodeJsonResponse -Value @{ Result = (Get-PodeState -Name 'Test1') } | ||
} | ||
|
||
# test2 | ||
Set-PodeState -Name 'Test2' -Value 0 | ||
Add-PodeSchedule -Name 'Test2' -Cron '@minutely' -ScriptBlock { | ||
Set-PodeState -Name 'Test2' -Value 314 | ||
} | ||
|
||
Add-PodeRoute -Method Get -Path '/test2' -ScriptBlock { | ||
Invoke-PodeSchedule -Name 'Test2' | ||
Start-Sleep -Seconds 2 | ||
Write-PodeJsonResponse -Value @{ Result = (Get-PodeState -Name 'Test2') } | ||
} | ||
} | ||
} | ||
|
||
Start-Sleep -Seconds 3 | ||
} | ||
|
||
AfterAll { | ||
Invoke-RestMethod -Uri "$($Endpoint)/close" -Method Get | Out-Null | ||
Get-Job -Name 'Pode' | Remove-Job -Force | ||
} | ||
|
||
|
||
It 'schedule updates state value - full cron' { | ||
$result = Invoke-RestMethod -Uri "$($Endpoint)/test1" -Method Get | ||
$result.Result | Should Be 1337 | ||
} | ||
|
||
It 'schedule updates state value - short cron' { | ||
$result = Invoke-RestMethod -Uri "$($Endpoint)/test2" -Method Get | ||
$result.Result | Should Be 314 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
Describe 'Timers' { | ||
|
||
BeforeAll { | ||
$Port = 9000 | ||
$Endpoint = "http://localhost:$($Port)" | ||
|
||
Start-Job -Name 'Pode' -ErrorAction Stop -ScriptBlock { | ||
Import-Module -Name "$($using:PSScriptRoot)\..\..\src\Pode.psm1" | ||
|
||
Start-PodeServer -RootPath $using:PSScriptRoot { | ||
Add-PodeEndpoint -Address localhost -Port $using:Port -Protocol Http | ||
|
||
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging | ||
Add-PodeRoute -Method Get -Path '/close' -ScriptBlock { | ||
Close-PodeServer | ||
} | ||
|
||
Set-PodeState -Name 'Test1' -Value 0 | ||
Add-PodeTimer -Name 'Test1' -Interval 1 -ScriptBlock { | ||
Set-PodeState -Name 'Test1' -Value 1337 | ||
} | ||
|
||
Add-PodeRoute -Method Get -Path '/test1' -ScriptBlock { | ||
Invoke-PodeTimer -Name 'Test1' | ||
Write-PodeJsonResponse -Value @{ Result = (Get-PodeState -Name 'Test1') } | ||
} | ||
} | ||
} | ||
|
||
Start-Sleep -Seconds 3 | ||
} | ||
|
||
AfterAll { | ||
Invoke-RestMethod -Uri "$($Endpoint)/close" -Method Get | Out-Null | ||
Get-Job -Name 'Pode' | Remove-Job -Force | ||
} | ||
|
||
|
||
It 'timer updates state value' { | ||
$result = Invoke-RestMethod -Uri "$($Endpoint)/test1" -Method Get | ||
$result.Result | Should Be 1337 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.