From 42e87c8f330d668974cd5bcd25233b7047680204 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sat, 4 Apr 2020 17:25:05 +0800 Subject: [PATCH] rename hostsetting, validate base url (#5821) --- .../configuration.mustache | 12 +++++++--- .../src/PSPetstore/Client/PSConfiguration.ps1 | 12 +++++++--- .../src/PSPetstore/PSPetstore.psd1 | 6 ++--- .../tests/Petstore.Tests.ps1 | 22 ++++++++++++------- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/powershell-experimental/configuration.mustache b/modules/openapi-generator/src/main/resources/powershell-experimental/configuration.mustache index ad5cb6335ac..d59d811f7b9 100644 --- a/modules/openapi-generator/src/main/resources/powershell-experimental/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/powershell-experimental/configuration.mustache @@ -119,6 +119,12 @@ function Set-{{{apiNamePrefix}}}Configuration { Process { 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 } @@ -278,7 +284,7 @@ Get the host setting in the form of array of hashtables. System.Collections.Hashtable[] #> -function Get-{{apiNamePrefix}}HostSettings { +function Get-{{apiNamePrefix}}HostSetting { return @( {{#servers}} @{ @@ -330,7 +336,7 @@ Get the URL from the host settings. String #> -function Get-{{apiNamePrefix}}UrlFromHostSettings { +function Get-{{apiNamePrefix}}UrlFromHostSetting { [CmdletBinding()] Param( @@ -340,7 +346,7 @@ function Get-{{apiNamePrefix}}UrlFromHostSettings { ) Process { - $Hosts = Get-{{apiNamePrefix}}HostSettings + $Hosts = Get-{{apiNamePrefix}}HostSetting # check array index out of bound if ($Index -lt 0 -or $Index -gt $Hosts.Length) { diff --git a/samples/client/petstore/powershell-experimental/src/PSPetstore/Client/PSConfiguration.ps1 b/samples/client/petstore/powershell-experimental/src/PSPetstore/Client/PSConfiguration.ps1 index 61fa67486ab..ccd55c338c8 100644 --- a/samples/client/petstore/powershell-experimental/src/PSPetstore/Client/PSConfiguration.ps1 +++ b/samples/client/petstore/powershell-experimental/src/PSPetstore/Client/PSConfiguration.ps1 @@ -125,6 +125,12 @@ function Set-PSConfiguration { Process { 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 } @@ -284,7 +290,7 @@ Get the host setting in the form of array of hashtables. System.Collections.Hashtable[] #> -function Get-PSHostSettings { +function Get-PSHostSetting { return @( @{ "Url" = "http://{server}.swagger.io:{port}/v2"; @@ -346,7 +352,7 @@ Get the URL from the host settings. String #> -function Get-PSUrlFromHostSettings { +function Get-PSUrlFromHostSetting { [CmdletBinding()] Param( @@ -356,7 +362,7 @@ function Get-PSUrlFromHostSettings { ) Process { - $Hosts = Get-PSHostSettings + $Hosts = Get-PSHostSetting # check array index out of bound if ($Index -lt 0 -or $Index -gt $Hosts.Length) { diff --git a/samples/client/petstore/powershell-experimental/src/PSPetstore/PSPetstore.psd1 b/samples/client/petstore/powershell-experimental/src/PSPetstore/PSPetstore.psd1 index f84d71a89a8..3ae28331de5 100644 --- a/samples/client/petstore/powershell-experimental/src/PSPetstore/PSPetstore.psd1 +++ b/samples/client/petstore/powershell-experimental/src/PSPetstore/PSPetstore.psd1 @@ -3,7 +3,7 @@ # # 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', 'Get-PSConfiguration', 'Set-PSConfiguration', 'Set-PSConfigurationApiKey', 'Set-PSConfigurationApiKeyPrefix', - 'Set-PSConfigurationDefaultHeader', 'Get-PSHostSettings', - 'Get-PSUrlFromHostSettings' + 'Set-PSConfigurationDefaultHeader', 'Get-PSHostSetting', + '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. CmdletsToExport = @() diff --git a/samples/client/petstore/powershell-experimental/tests/Petstore.Tests.ps1 b/samples/client/petstore/powershell-experimental/tests/Petstore.Tests.ps1 index 1c678b649f0..30cd15600d5 100644 --- a/samples/client/petstore/powershell-experimental/tests/Petstore.Tests.ps1 +++ b/samples/client/petstore/powershell-experimental/tests/Petstore.Tests.ps1 @@ -115,9 +115,9 @@ Describe -tag 'PSOpenAPITools' -name 'Integration Tests' { } 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]["Description"] | Should Be "petstore server" @@ -129,12 +129,12 @@ Describe -tag 'PSOpenAPITools' -name 'Integration Tests' { } - It "Get-PSUrlFromHostSettings tests" { - Get-PSUrlFromHostSettings -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-PSUrlFromHostSettings -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-PSUrlFromHostSettings -Index 0 -Variables @{ "port" = "1234" } | Should Throw "The variable 'port' in the host URL has invalid value 1234. Must be 80,8080" + It "Get-PSUrlFromHostSetting tests" { + Get-PSUrlFromHostSetting -Index 0 | Should Be "http://petstore.swagger.io:80/v2" + Get-PSUrlFromHostSetting -Index 0 -Variables @{ "port" = "8080" } | Should Be "http://petstore.swagger.io:8080/v2" + #Get-PSUrlFromHostSetting -Index 2 | Should -Throw -ExceptionType ([RuntimeException]) + #Get-PSUrlFromHostSetting -Index 2 | Should -Throw # "Invalid index 2 when selecting the host. Must be less than 2" + #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 = Set-PSConfiguration -PassThru -SkipCertificateCheck $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" } } }