Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit 5b98887

Browse files
committed
Only prepend https protocol to API host if one is not already specified.
1 parent fd5b1e8 commit 5b98887

File tree

3 files changed

+54
-33
lines changed

3 files changed

+54
-33
lines changed

compile/apigw/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const BbPromise = require('bluebird');
44
const crypto = require('crypto');
5+
const { formatApiHost } = require('../../utils');
56

67
class OpenWhiskCompileHttpEvents {
78
constructor(serverless, options) {
@@ -313,7 +314,7 @@ class OpenWhiskCompileHttpEvents {
313314
}
314315

315316
webActionUrl(httpEvent, host, has_path_params) {
316-
const url = `https://${host}/api/v1/web/${httpEvent.namespace}/${httpEvent.pkge}/${httpEvent.action}.${httpEvent.responsetype}${has_path_params ? '$(request.path)': ''}`
317+
const url = `${formatApiHost(host)}/api/v1/web/${httpEvent.namespace}/${httpEvent.pkge}/${httpEvent.action}.${httpEvent.responsetype}${has_path_params ? '$(request.path)': ''}`
317318

318319
return url
319320
}

compile/apigw/tests/index.js

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
157157

158158
describe('#compileHttpEvent()', () => {
159159
it('should define http events from string property', () => {
160-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
160+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
161161
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
162162
const http = "GET /api/foo/bar"
163163
const result = openwhiskCompileHttpEvents.compileHttpEvent('action-name', {}, http);
@@ -172,7 +172,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
172172
});
173173

174174
it('should define http events from string property with explicit package', () => {
175-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
175+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
176176
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
177177
const http = "GET /api/foo/bar"
178178
const fnObj = { name: 'somePackage/actionName' }
@@ -188,15 +188,15 @@ describe('OpenWhiskCompileHttpEvents', () => {
188188
});
189189

190190
it('should define http events from object property', () => {
191-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
191+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
192192
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
193193
const http = {path: "/api/foo/bar", method: "GET"}
194194
const result = openwhiskCompileHttpEvents.compileHttpEvent('action-name', {}, http);
195195
return expect(result).to.deep.equal({relpath: '/api/foo/bar', operation: 'GET', action: 'my-service_action-name', namespace: 'sample_ns', pkge: 'default', responsetype: 'json'});
196196
});
197197

198198
it('should add secure auth key if present', () => {
199-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
199+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
200200
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
201201
const http = {path: "/api/foo/bar", method: "GET"}
202202
const result = openwhiskCompileHttpEvents.compileHttpEvent('action-name', {
@@ -227,7 +227,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
227227

228228
describe('#compileSwaggerPath()', () => {
229229
it('should define swagger definition from http events', () => {
230-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
230+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
231231
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
232232

233233
const httpEvent = {
@@ -254,8 +254,36 @@ describe('OpenWhiskCompileHttpEvents', () => {
254254
return expect(result).to.deep.equal(expectedResult)
255255
});
256256

257+
it('should define swagger definition from http events and respect specified protocol on api host', () => {
258+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
259+
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
260+
261+
const httpEvent = {
262+
relpath: '/api/foo/bar', operation: 'GET', secure_key: 'auth-token',
263+
action: 'action-name', namespace: '[email protected]_space', pkge: 'default', responsetype: 'json'
264+
}
265+
266+
const host = 'http://openwhisk.somewhere.com'
267+
const result = openwhiskCompileHttpEvents.compileSwaggerPath(httpEvent, host);
268+
269+
const expectedResult = {
270+
operationId: "get-/api/foo/bar",
271+
responses: {
272+
"200": { description: "A successful invocation response" }
273+
},
274+
"x-openwhisk": {
275+
action: "action-name",
276+
namespace: "[email protected]_space",
277+
package: "default",
278+
url: "http://openwhisk.somewhere.com/api/v1/web/[email protected]_space/default/action-name.json"
279+
}
280+
}
281+
282+
return expect(result).to.deep.equal(expectedResult)
283+
});
284+
257285
it('should define swagger definition with path parameters', () => {
258-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
286+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
259287
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
260288

261289
const httpEvent = {
@@ -291,7 +319,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
291319

292320
describe('#compileSwaggerCaseSwitch()', () => {
293321
it('should define swagger case statement from http events', () => {
294-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
322+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
295323
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
296324

297325
const httpEvent = {
@@ -325,7 +353,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
325353
});
326354

327355
it('should define swagger case statement from http events with path parameters', () => {
328-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
356+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
329357
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
330358

331359
const httpEvent = {
@@ -361,7 +389,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
361389

362390
describe('#generateSwagger()', () => {
363391
it('should generate APIGW swagger with paths and case statements from http event', () => {
364-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
392+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
365393
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
366394

367395
const httpEvent = {
@@ -387,7 +415,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
387415
});
388416

389417
it('should generate APIGW swagger with multiple http events on same path', () => {
390-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
418+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
391419
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
392420

393421
const gethttpEvent = {
@@ -421,7 +449,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
421449
});
422450

423451
it('should generate APIGW swagger with multiple http events on different paths', () => {
424-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
452+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
425453
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
426454

427455
const gethttpEvent = {
@@ -455,7 +483,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
455483
});
456484

457485
it('should generate APIGW swagger with default API gateway options', () => {
458-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
486+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
459487
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
460488

461489
const httpEvent = {
@@ -472,7 +500,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
472500
});
473501

474502
it('should generate APIGW swagger with custom CORS API gateway options', () => {
475-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
503+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
476504
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
477505

478506
const httpEvent = {
@@ -489,7 +517,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
489517
});
490518

491519
it('should generate APIGW swagger with custom basepath API gateway option', () => {
492-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
520+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
493521
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
494522

495523
const httpEvent = {
@@ -506,7 +534,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
506534
});
507535

508536
it('should generate APIGW swagger with custom API name option', () => {
509-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
537+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
510538
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
511539

512540
const httpEvent = {
@@ -524,7 +552,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
524552

525553

526554
it('should generate APIGW swagger with custom auth key API gateway options', () => {
527-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
555+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
528556
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
529557

530558
const httpEvent = {
@@ -542,7 +570,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
542570
});
543571

544572
it('should generate APIGW swagger with custom auth key and secret API gateway options', () => {
545-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
573+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
546574
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
547575

548576
const httpEvent = {
@@ -561,7 +589,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
561589
});
562590

563591
it('should generate APIGW swagger with AppID OAuth provider API gateway options', () => {
564-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
592+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
565593
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
566594

567595
const httpEvent = {
@@ -585,7 +613,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
585613
});
586614

587615
it('should generate APIGW swagger with Google OAuth provider API gateway options', () => {
588-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
616+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
589617
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
590618

591619
const httpEvent = {
@@ -607,7 +635,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
607635
});
608636

609637
it('should generate APIGW swagger with Facebook OAuth provider API gateway options', () => {
610-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
638+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
611639
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
612640

613641
const httpEvent = {
@@ -629,7 +657,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
629657
});
630658

631659
it('should generate APIGW swagger with Github OAuth provider API gateway options', () => {
632-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
660+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
633661
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
634662

635663
const httpEvent = {
@@ -651,7 +679,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
651679
});
652680

653681
it('should generate APIGW swagger with rate limiting API gateway options', () => {
654-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
682+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
655683
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
656684

657685
const httpEvent = {
@@ -668,7 +696,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
668696
});
669697

670698
it('should throw if API GW auth options are invalid', () => {
671-
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
699+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
672700
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
673701

674702
const httpEvent = {

info/index.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const BbPromise = require('bluebird');
44
const chalk = require('chalk');
5+
const { formatApiHost } = require('../utils');
56

67
class OpenWhiskInfo {
78
constructor(serverless, options) {
@@ -174,13 +175,4 @@ class OpenWhiskInfo {
174175
}
175176
}
176177

177-
function formatApiHost(apihost) {
178-
if (apihost && !(apihost.startsWith('http://') || apihost.startsWith('https://'))) {
179-
// assume https unless explicitly declared
180-
return `https://${apihost}`;
181-
} else {
182-
return apihost;
183-
}
184-
}
185-
186178
module.exports = OpenWhiskInfo;

0 commit comments

Comments
 (0)