Skip to content

Commit

Permalink
Badgerati#546: adds tests for timers, schedules, endpoints, and others
Browse files Browse the repository at this point in the history
  • Loading branch information
Badgerati committed May 6, 2020
1 parent 8fe788e commit e13d087
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Describe 'Authentication Requests' {
$Endpoint = "http://localhost:$($Port)"

Start-Job -Name 'Pode' -ErrorAction Stop -ScriptBlock {
Import-Module -Name "$($using:PSScriptRoot)\..\..\..\src\Pode.psm1"
Import-Module -Name "$($using:PSScriptRoot)\..\..\src\Pode.psm1"

Start-PodeServer {
Add-PodeEndpoint -Address localhost -Port $using:Port -Protocol Http
Expand Down Expand Up @@ -66,21 +66,11 @@ Describe 'Authentication Requests' {
}

It 'basic - returns 401 for invalid creds' {
try {
Invoke-RestMethod -Uri "$($Endpoint)/auth/basic" -Method Post -Headers @{ Authorization = 'Basic cmljazpwaWNrbGU=' } -ErrorAction Stop
}
catch {
$_.Exception.Message.Contains('401') | Should Be $true
}
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/basic" -Method Post -Headers @{ Authorization = 'Basic cmljazpwaWNrbGU=' } -ErrorAction Stop } | Should Throw '401'
}

It 'basic - returns 400 for invalid base64' {
try {
Invoke-RestMethod -Uri "$($Endpoint)/auth/basic" -Method Post -Headers @{ Authorization = 'Basic cmlazpwaNrbGU' } -ErrorAction Stop
}
catch {
$_.Exception.Message.Contains('400') | Should Be $true
}
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/basic" -Method Post -Headers @{ Authorization = 'Basic cmlazpwaNrbGU' } -ErrorAction Stop } | Should Throw '400'
}


Expand All @@ -91,20 +81,10 @@ Describe 'Authentication Requests' {
}

It 'bearer - returns 401 for invalid token' {
try {
Invoke-RestMethod -Uri "$($Endpoint)/auth/bearer" -Method Get -Headers @{ Authorization = 'Bearer fake-token' } -ErrorAction Stop
}
catch {
$_.Exception.Message.Contains('401') | Should Be $true
}
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/bearer" -Method Get -Headers @{ Authorization = 'Bearer fake-token' } -ErrorAction Stop } | Should Throw '401'
}

It 'bearer - returns 400 for no token' {
try {
Invoke-RestMethod -Uri "$($Endpoint)/auth/bearer" -Method Get -Headers @{ Authorization = 'Bearer' } -ErrorAction Stop
}
catch {
$_.Exception.Message.Contains('400') | Should Be $true
}
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/bearer" -Method Get -Headers @{ Authorization = 'Bearer' } -ErrorAction Stop } | Should Throw 400
}
}
69 changes: 69 additions & 0 deletions tests/integration/Endpoints.Tests.ps1
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'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ Describe 'REST API Requests' {
$Endpoint = "http://localhost:$($Port)"

Start-Job -Name 'Pode' -ErrorAction Stop -ScriptBlock {
Import-Module -Name "$($using:PSScriptRoot)\..\..\..\src\Pode.psm1"
Import-Module -Name "$($using:PSScriptRoot)\..\..\src\Pode.psm1"

Start-PodeServer {
Start-PodeServer -RootPath $using:PSScriptRoot -Type Pode {
Add-PodeEndpoint -Address localhost -Port $using:Port -Protocol Http

New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging
Add-PodeRoute -Method Get -Path '/close' -ScriptBlock {
Close-PodeServer
}
Expand Down Expand Up @@ -90,21 +92,11 @@ Describe 'REST API Requests' {
}

It 'responds back with 404 for invalid route' {
try {
Invoke-RestMethod -Uri "$($Endpoint)/eek" -Method Get -ErrorAction Stop
}
catch {
$_.Exception.Message.Contains('404') | Should Be $true
}
{ Invoke-RestMethod -Uri "$($Endpoint)/eek" -Method Get -ErrorAction Stop } | Should Throw '404'
}

It 'responds back with 405 for incorrect method' {
try {
Invoke-RestMethod -Uri "$($Endpoint)/ping" -Method Post -ErrorAction Stop
}
catch {
$_.Exception.Message.Contains('405') | Should Be $true
}
{ Invoke-RestMethod -Uri "$($Endpoint)/ping" -Method Post -ErrorAction Stop } | Should Throw '405'
}

It 'responds with simple query parameter' {
Expand Down
62 changes: 62 additions & 0 deletions tests/integration/Schedules.Tests.ps1
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
}
}
43 changes: 43 additions & 0 deletions tests/integration/Timers.Tests.ps1
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Describe 'Web Page Requests' {
$Endpoint = "http://localhost:$($Port)"

Start-Job -Name 'Pode' -ErrorAction Stop -ScriptBlock {
Import-Module -Name "$($using:PSScriptRoot)\..\..\..\src\Pode.psm1"
Import-Module -Name "$($using:PSScriptRoot)\..\..\src\Pode.psm1"

Start-PodeServer -RootPath $using:PSScriptRoot {
Add-PodeEndpoint -Address localhost -Port $using:Port -Protocol Http
Expand Down Expand Up @@ -74,12 +74,7 @@ Describe 'Web Page Requests' {
}

It 'responds with 404 for non-public static content' {
try {
Invoke-WebRequest -Uri "$($Endpoint)/images/custom_ruler.png" -Method Get -ErrorAction Stop
}
catch {
$_.Exception.Message.Contains('404') | Should Be $true
}
{ Invoke-WebRequest -Uri "$($Endpoint)/images/custom_ruler.png" -Method Get -ErrorAction Stop } | Should Throw '404'
}

It 'responds with custom static content' {
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.

0 comments on commit e13d087

Please sign in to comment.