Skip to content

Commit 0c34a07

Browse files
dgrove-ossdubee
authored andcommitted
Bug fix: Use configured prototcol for API requests (#300)
Add check to see if Client.Config.Host already specifies the protocol to use before prepending https:// in URL construction. Fixes a bug where if .wskprops specifies a APIHOST with a protocol, `wsk api create` generates an invalid backend URL (https://https://APIHOST/...). Also add unit test to check for this error condition. Fixes #125.
1 parent e8cc839 commit 0c34a07

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

commands/api.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,11 @@ func parseApi(cmd *cobra.Command, args []string) (*whisk.Api, *QualifiedName, er
913913
} else {
914914
urlActionPackage = "default"
915915
}
916-
api.Action.BackendUrl = "https://" + Client.Config.Host + "/api/v1/web/" + qName.GetNamespace() + "/" + urlActionPackage + "/" + qName.GetEntity() + ".http"
916+
backendUrl := Client.Config.Host + "/api/v1/web/" + qName.GetNamespace() + "/" + urlActionPackage + "/" + qName.GetEntity() + ".http"
917+
if !strings.HasPrefix(backendUrl, "http") {
918+
backendUrl = "https://" + backendUrl
919+
}
920+
api.Action.BackendUrl = backendUrl
917921
api.Action.BackendMethod = api.GatewayMethod
918922
api.Action.Name = qName.GetEntityName()
919923
api.Action.Namespace = qName.GetNamespace()

tests/src/test/scala/whisk/core/cli/test/ApiGwCliBasicTests.scala

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ abstract class ApiGwCliBasicTests extends BaseApiGwTests {
109109
rr.stdout should include(testbasepath + testrelpath)
110110
}
111111

112-
def verifyApiGet(rr: RunResult): Unit = {
112+
def verifyApiGet(rr: RunResult, apihost:String): Unit = {
113113
rr.stdout should include regex (s""""operationId":\\s+"getPathWithSub_pathsInIt"""")
114+
rr.stdout should include regex (s""""target-url":\\s+"https://$apihost""")
114115
}
115116

116117
def verifyApiFullList(rr: RunResult,
@@ -447,7 +448,7 @@ abstract class ApiGwCliBasicTests extends BaseApiGwTests {
447448
rr = apiList(basepathOrApiName = Some(testbasepath), relpath = Some(testrelpath), operation = Some(testurlop))
448449
verifyApiFullList(rr, clinamespace, actionName, testurlop, testbasepath, testrelpath, testapiname)
449450
rr = apiGet(basepathOrApiName = Some(testbasepath))
450-
verifyApiGet(rr)
451+
verifyApiGet(rr, wskprops.apihost)
451452
val deleteresult = apiDelete(basepathOrApiName = testbasepath)
452453
verifyApiDeleted(deleteresult)
453454
} finally {
@@ -456,6 +457,43 @@ abstract class ApiGwCliBasicTests extends BaseApiGwTests {
456457
}
457458
}
458459

460+
it should "verify successful creation and deletion of a new API when apihost specifies the protocol" in {
461+
val testName = "CLI_APIGWTEST2"
462+
val testbasepath = "/" + testName + "_bp"
463+
val testrelpath = "/path/with/sub_paths/in/it"
464+
val testnewrelpath = "/path_new"
465+
val testurlop = "get"
466+
val testapiname = testName + " API Name"
467+
val actionName = testName + "_action"
468+
try {
469+
println("cli namespace: " + clinamespace)
470+
471+
// Create the action for the API. It must be a "web-action" action.
472+
val file = TestUtils.getTestActionFilename(s"echo.js")
473+
wsk.action.create(name = actionName, artifact = Some(file), expectedExitCode = createCode, web = Some("true"))
474+
475+
val explicitProtocol = if (wskprops.apihost.startsWith("http")) "" else "https://"
476+
val wskpropsOverride = WskProps(apihost = explicitProtocol + wskprops.apihost)
477+
var rr = apiCreate(
478+
basepath = Some(testbasepath),
479+
relpath = Some(testrelpath),
480+
operation = Some(testurlop),
481+
action = Some(actionName),
482+
apiname = Some(testapiname))(wskpropsOverride)
483+
verifyApiCreated(rr)
484+
rr = apiList(basepathOrApiName = Some(testbasepath), relpath = Some(testrelpath), operation = Some(testurlop))
485+
verifyApiFullList(rr, clinamespace, actionName, testurlop, testbasepath, testrelpath, testapiname)
486+
rr = apiGet(basepathOrApiName = Some(testbasepath))
487+
verifyApiGet(rr, wskprops.apihost)
488+
val deleteresult = apiDelete(basepathOrApiName = testbasepath)
489+
verifyApiDeleted(deleteresult)
490+
} finally {
491+
wsk.action.delete(name = actionName, expectedExitCode = DONTCARE_EXIT)
492+
apiDelete(basepathOrApiName = testbasepath, expectedExitCode = DONTCARE_EXIT)
493+
}
494+
}
495+
496+
459497
it should "verify get API name " in {
460498
val testName = "CLI_APIGWTEST3"
461499
val testbasepath = "/" + testName + "_bp"

0 commit comments

Comments
 (0)