rename hostsetting, validate base url (#5821)

This commit is contained in:
William Cheng 2020-04-04 17:25:05 +08:00 committed by GitHub
parent 242c2e8544
commit 42e87c8f33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 17 deletions

View File

@ -119,6 +119,12 @@ function Set-{{{apiNamePrefix}}}Configuration {
Process { Process {
If ($BaseUrl) { If ($BaseUrl) {
# validate URL
$URL = $BaseUrl -as [System.URI]
if (!($URL.AbsoluteURI -ne $null -and $URL.Scheme -match '[http|https]')) {
throw "Invalid URL '$($BaseUrl)' cannot be used in the base URL."
}
$Script:Configuration["BaseUrl"] = $BaseUrl $Script:Configuration["BaseUrl"] = $BaseUrl
} }
@ -278,7 +284,7 @@ Get the host setting in the form of array of hashtables.
System.Collections.Hashtable[] System.Collections.Hashtable[]
#> #>
function Get-{{apiNamePrefix}}HostSettings { function Get-{{apiNamePrefix}}HostSetting {
return @( return @(
{{#servers}} {{#servers}}
@{ @{
@ -330,7 +336,7 @@ Get the URL from the host settings.
String String
#> #>
function Get-{{apiNamePrefix}}UrlFromHostSettings { function Get-{{apiNamePrefix}}UrlFromHostSetting {
[CmdletBinding()] [CmdletBinding()]
Param( Param(
@ -340,7 +346,7 @@ function Get-{{apiNamePrefix}}UrlFromHostSettings {
) )
Process { Process {
$Hosts = Get-{{apiNamePrefix}}HostSettings $Hosts = Get-{{apiNamePrefix}}HostSetting
# check array index out of bound # check array index out of bound
if ($Index -lt 0 -or $Index -gt $Hosts.Length) { if ($Index -lt 0 -or $Index -gt $Hosts.Length) {

View File

@ -125,6 +125,12 @@ function Set-PSConfiguration {
Process { Process {
If ($BaseUrl) { If ($BaseUrl) {
# validate URL
$URL = $BaseUrl -as [System.URI]
if (!($URL.AbsoluteURI -ne $null -and $URL.Scheme -match '[http|https]')) {
throw "Invalid URL '$($BaseUrl)' cannot be used in the base URL."
}
$Script:Configuration["BaseUrl"] = $BaseUrl $Script:Configuration["BaseUrl"] = $BaseUrl
} }
@ -284,7 +290,7 @@ Get the host setting in the form of array of hashtables.
System.Collections.Hashtable[] System.Collections.Hashtable[]
#> #>
function Get-PSHostSettings { function Get-PSHostSetting {
return @( return @(
@{ @{
"Url" = "http://{server}.swagger.io:{port}/v2"; "Url" = "http://{server}.swagger.io:{port}/v2";
@ -346,7 +352,7 @@ Get the URL from the host settings.
String String
#> #>
function Get-PSUrlFromHostSettings { function Get-PSUrlFromHostSetting {
[CmdletBinding()] [CmdletBinding()]
Param( Param(
@ -356,7 +362,7 @@ function Get-PSUrlFromHostSettings {
) )
Process { Process {
$Hosts = Get-PSHostSettings $Hosts = Get-PSHostSetting
# check array index out of bound # check array index out of bound
if ($Index -lt 0 -or $Index -gt $Hosts.Length) { if ($Index -lt 0 -or $Index -gt $Hosts.Length) {

View File

@ -3,7 +3,7 @@
# #
# Generated by: OpenAPI Generator Team # Generated by: OpenAPI Generator Team
# #
# Generated on: 4/3/20 # Generated on: 4/4/20
# #
@{ @{
@ -81,8 +81,8 @@ FunctionsToExport = 'Add-PSPet', 'Remove-Pet', 'Find-PSPetsByStatus', 'Find-PSPe
'Initialize-PSPet', 'Initialize-PSTag', 'Initialize-PSUser', 'Initialize-PSPet', 'Initialize-PSTag', 'Initialize-PSUser',
'Get-PSConfiguration', 'Set-PSConfiguration', 'Get-PSConfiguration', 'Set-PSConfiguration',
'Set-PSConfigurationApiKey', 'Set-PSConfigurationApiKeyPrefix', 'Set-PSConfigurationApiKey', 'Set-PSConfigurationApiKeyPrefix',
'Set-PSConfigurationDefaultHeader', 'Get-PSHostSettings', 'Set-PSConfigurationDefaultHeader', 'Get-PSHostSetting',
'Get-PSUrlFromHostSettings' 'Get-PSUrlFromHostSetting'
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @() CmdletsToExport = @()

View File

@ -115,9 +115,9 @@ Describe -tag 'PSOpenAPITools' -name 'Integration Tests' {
} }
Context 'Configuration' { Context 'Configuration' {
It 'Get-PSHostSettings tests' { It 'Get-PSHostSetting tests' {
$HS = Get-PSHostSettings $HS = Get-PSHostSetting
$HS[0]["Url"] | Should Be "http://{server}.swagger.io:{port}/v2" $HS[0]["Url"] | Should Be "http://{server}.swagger.io:{port}/v2"
$HS[0]["Description"] | Should Be "petstore server" $HS[0]["Description"] | Should Be "petstore server"
@ -129,12 +129,12 @@ Describe -tag 'PSOpenAPITools' -name 'Integration Tests' {
} }
It "Get-PSUrlFromHostSettings tests" { It "Get-PSUrlFromHostSetting tests" {
Get-PSUrlFromHostSettings -Index 0 | Should Be "http://petstore.swagger.io:80/v2" Get-PSUrlFromHostSetting -Index 0 | Should Be "http://petstore.swagger.io:80/v2"
Get-PSUrlFromHostSettings -Index 0 -Variables @{ "port" = "8080" } | Should Be "http://petstore.swagger.io:8080/v2" Get-PSUrlFromHostSetting -Index 0 -Variables @{ "port" = "8080" } | Should Be "http://petstore.swagger.io:8080/v2"
#Get-PSUrlFromHostSettings -Index 2 | Should -Throw -ExceptionType ([RuntimeException]) #Get-PSUrlFromHostSetting -Index 2 | Should -Throw -ExceptionType ([RuntimeException])
#Get-PSUrlFromHostSettings -Index 2 | Should -Throw # "Invalid index 2 when selecting the host. Must be less than 2" #Get-PSUrlFromHostSetting -Index 2 | Should -Throw # "Invalid index 2 when selecting the host. Must be less than 2"
#Get-PSUrlFromHostSettings -Index 0 -Variables @{ "port" = "1234" } | Should Throw "The variable 'port' in the host URL has invalid value 1234. Must be 80,8080" #Get-PSUrlFromHostSetting -Index 0 -Variables @{ "port" = "1234" } | Should Throw "The variable 'port' in the host URL has invalid value 1234. Must be 80,8080"
} }
@ -153,6 +153,12 @@ Describe -tag 'PSOpenAPITools' -name 'Integration Tests' {
$Conf["SkipCertificateCheck"] | Should Be $false $Conf["SkipCertificateCheck"] | Should Be $false
$Conf = Set-PSConfiguration -PassThru -SkipCertificateCheck $Conf = Set-PSConfiguration -PassThru -SkipCertificateCheck
$Conf["SkipCertificateCheck"] | Should Be $true $Conf["SkipCertificateCheck"] | Should Be $true
$Conf = Set-PSConfiguration -PassThru # reset SkipCertificateCheck
}
It "Base URL tests" {
$Conf = Set-PSConfiguration -BaseURL "http://localhost"
$Conf = Set-PSConfiguration -BaseURL "https://localhost:8080/api"
} }
} }
} }