Skip to content

Commit

Permalink
Merge 38ea123 into 112301e
Browse files Browse the repository at this point in the history
  • Loading branch information
Badgerati committed Apr 1, 2020
2 parents 112301e + 38ea123 commit 937aca3
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Pode.psd1
Expand Up @@ -180,6 +180,7 @@
'Start-PodeStaticServer',
'Show-PodeGui',
'Add-PodeEndpoint',
'Get-PodeEndpoint',
'Pode',

# openapi
Expand Down
118 changes: 118 additions & 0 deletions src/Public/Core.ps1
Expand Up @@ -818,6 +818,124 @@ function Add-PodeEndpoint
}
}

<#
.SYNOPSIS
Get an Endpoint(s).
.DESCRIPTION
Get an Endpoint(s).
.PARAMETER Address
An Address to filter the endpoints.
.PARAMETER Port
A Port to filter the endpoints.
.PARAMETER Protocol
A Protocol to filter the endpoints.
.PARAMETER Name
Any endpoints Names to filter endpoints.
.EXAMPLE
Get-PodeEndpoint -Address 127.0.0.1
.EXAMPLE
Get-PodeEndpoint -Protocol Http
.EXAMPLE
Get-PodeEndpoint -Name Admin, User
#>
function Get-PodeEndpoint
{
[CmdletBinding()]
param(
[Parameter()]
[string]
$Address,

[Parameter()]
[int]
$Port = 0,

[Parameter()]
[ValidateSet('', 'Http', 'Https', 'Smtp', 'Tcp', 'Ws', 'Wss')]
[string]
$Protocol,

[Parameter()]
[string[]]
$Name
)

$endpoints = $PodeContext.Server.Endpoints

# if we have an address, filter
if (![string]::IsNullOrWhiteSpace($Address)) {
if ($Address -eq '*') {
$Address = '0.0.0.0'
}

if ($PodeContext.Server.IsIIS) {
$Address = '127.0.0.1'
}

$endpoints = @(foreach ($endpoint in $endpoints) {
if ($endpoint.Address.ToString() -ine $Address) {
continue
}

$endpoint
})
}

# if we have a port, filter
if ($Port -gt 0) {
if ($PodeContext.Server.IsIIS) {
$Port = [int]$env:ASPNETCORE_PORT
}

$endpoints = @(foreach ($endpoint in $endpoints) {
if ($endpoint.Port -ne $Port) {
continue
}

$endpoint
})
}

# if we have a protocol, filter
if (![string]::IsNullOrWhiteSpace($Protocol)) {
if ($PodeContext.Server.IsIIS) {
$Protocol = 'Http'
}

$endpoints = @(foreach ($endpoint in $endpoints) {
if ($endpoint.Protocol -ine $Protocol) {
continue
}

$endpoint
})
}

# further filter by endpoint names
if (($null -ne $Name) -and ($Name.Length -gt 0)) {
$endpoints = @(foreach ($_name in $Name) {
foreach ($endpoint in $endpoints) {
if ($endpoint.Name -ine $_name) {
continue
}

$endpoint
}
})
}

# return
return $endpoints
}

<#
.SYNOPSIS
Adds a new Timer with logic to periodically invoke.
Expand Down
106 changes: 106 additions & 0 deletions tests/unit/Context.Tests.ps1
Expand Up @@ -281,6 +281,112 @@ Describe 'Add-PodeEndpoint' {
}
}

Describe 'Get-PodeEndpoint' {
Mock Test-PodeIPAddress { return $true }
Mock Test-IsAdminUser { return $true }

It 'Returns no Endpoints' {
$PodeContext.Server = @{ Endpoints = @(); Type = $null }
$endpoints = Get-PodeEndpoint
$endpoints.Length | Should Be 0
}

It 'Returns all Endpoints' {
$PodeContext.Server = @{ Endpoints = @(); Type = $null }

Add-PodeEndpoint -Address '127.0.0.1' -Port 80 -Protocol 'HTTP'
Add-PodeEndpoint -Address 'pode.foo.com' -Port 80 -Protocol 'HTTP'
Add-PodeEndpoint -Address 'pode.foo.com' -Port 8080 -Protocol 'HTTP'

$endpoints = Get-PodeEndpoint
$endpoints.Length | Should Be 3
}

It 'Returns 1 endpoint by address' {
$PodeContext.Server = @{ Endpoints = @(); Type = $null }

Add-PodeEndpoint -Address '127.0.0.1' -Port 80 -Protocol 'HTTP'
Add-PodeEndpoint -Address 'pode.foo.com' -Port 80 -Protocol 'HTTP'
Add-PodeEndpoint -Address 'pode.foo.com' -Port 8080 -Protocol 'HTTP'

$endpoints = Get-PodeEndpoint -Address '127.0.0.1'
$endpoints.Length | Should Be 1
}

It 'Returns 2 endpoints by address' {
$PodeContext.Server = @{ Endpoints = @(); Type = $null }

Add-PodeEndpoint -Address '127.0.0.1' -Port 80 -Protocol 'HTTP'
Add-PodeEndpoint -Address 'pode.foo.com' -Port 80 -Protocol 'HTTP'
Add-PodeEndpoint -Address 'pode.foo.com' -Port 8080 -Protocol 'HTTP'

$endpoints = Get-PodeEndpoint -Address 'pode.foo.com'
$endpoints.Length | Should Be 2
}

It 'Returns 2 endpoints by port' {
$PodeContext.Server = @{ Endpoints = @(); Type = $null }

Add-PodeEndpoint -Address '127.0.0.1' -Port 80 -Protocol 'HTTP'
Add-PodeEndpoint -Address 'pode.foo.com' -Port 80 -Protocol 'HTTP'
Add-PodeEndpoint -Address 'pode.foo.com' -Port 8080 -Protocol 'HTTP'

$endpoints = Get-PodeEndpoint -Port 80
$endpoints.Length | Should Be 2
}

It 'Returns all endpoints by protocol' {
$PodeContext.Server = @{ Endpoints = @(); Type = $null }

Add-PodeEndpoint -Address '127.0.0.1' -Port 80 -Protocol 'HTTP'
Add-PodeEndpoint -Address 'pode.foo.com' -Port 80 -Protocol 'HTTP'
Add-PodeEndpoint -Address 'pode.foo.com' -Port 8080 -Protocol 'HTTP'

$endpoints = Get-PodeEndpoint -Protocol Http
$endpoints.Length | Should Be 3
}

It 'Returns 2 endpoints by name' {
$PodeContext.Server = @{ Endpoints = @(); Type = $null }

Add-PodeEndpoint -Address '127.0.0.1' -Port 80 -Protocol 'HTTP' -Name 'Admin'
Add-PodeEndpoint -Address 'pode.foo.com' -Port 80 -Protocol 'HTTP' -Name 'User'
Add-PodeEndpoint -Address 'pode.foo.com' -Port 8080 -Protocol 'HTTP' -Name 'Dev'

$endpoints = Get-PodeEndpoint -Name Admin, User
$endpoints.Length | Should Be 2
}

It 'Returns 1 endpoint using everything' {
$PodeContext.Server = @{ Endpoints = @(); Type = $null }

Add-PodeEndpoint -Address '127.0.0.1' -Port 80 -Protocol 'HTTP' -Name 'Admin'
Add-PodeEndpoint -Address 'pode.foo.com' -Port 80 -Protocol 'HTTP' -Name 'User'
Add-PodeEndpoint -Address 'pode.foo.com' -Port 8080 -Protocol 'HTTP' -Name 'Dev'

$endpoints = Get-PodeEndpoint -Address 'pode.foo.com' -Port 80 -Protocol Http -Name User
$endpoints.Length | Should Be 1
}

It 'Returns endpoint set using wildcard' {
$PodeContext.Server = @{ Endpoints = @(); Type = $null }

Add-PodeEndpoint -Address '*' -Port 80 -Protocol 'HTTP'

$endpoints = Get-PodeEndpoint -Address '*'
$endpoints.Length | Should Be 1
}

It 'Returns endpoint set using localhost' {
$PodeContext.Server = @{ Endpoints = @(); Type = $null }

Add-PodeEndpoint -Address 'localhost' -Port 80 -Protocol 'HTTP'

$endpoints = Get-PodeEndpoint -Address 'localhost'
$endpoints.Length | Should Be 1
}
}

Describe 'Import-PodeModule' {
Context 'Invalid parameters supplied' {
It 'Throw null path parameter error' {
Expand Down
5 changes: 0 additions & 5 deletions tests/unit/Routes.Tests.ps1
Expand Up @@ -1274,11 +1274,6 @@ Describe 'Get-PodeStaticRoute' {
$routes.Length | Should Be 1
}






It 'Returns one static route for endpoint' {
$PodeContext.Server = @{ Routes = @{ STATIC = @{}; }; Root = $pwd; Endpoints = @(); Type = $null }

Expand Down

0 comments on commit 937aca3

Please sign in to comment.