Skip to content

Commit

Permalink
[FEATURE] Add 'sap-csp-policies' option to 'serve' command. (#188)
Browse files Browse the repository at this point in the history
CLI counterpart for SAP/ui5-server#179
  • Loading branch information
codeworrior authored and RandomByte committed Apr 26, 2019
1 parent 8ba7962 commit 57d5567
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ Options:
--accept-remote-connections Accept remote connections. By default the server only accepts connections from localhost [boolean] [default: false]
--key Path to the private key [string] [default: "$HOME/.ui5/server/server.key"]
--cert Path to the certificate [string] [default: "$HOME/.ui5/server/server.crt"]
--sap-csp-policies Always send content security policies 'sap-target-level-1' and 'sap-target-level-2' in report-only mode [boolean] [default: false]
Examples:
ui5 serve Start a web server for the current project
Expand Down
8 changes: 7 additions & 1 deletion lib/cli/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ serve.builder = function(cli) {
default: "$HOME/.ui5/server/server.crt",
type: "string"
})
.option("sap-csp-policies", {
describe: "Always send content security policies 'sap-target-level-1' and 'sap-target-level-2' in report-only mode",
default: false,
type: "boolean"
})
.example("ui5 serve", "Start a web server for the current project")
.example("ui5 serve --h2", "Enable the HTTP/2 protocol for the web server (requires SSL certificate)")
.example("ui5 serve --config /path/to/ui5.yaml", "Use the project configuration from a custom path")
Expand All @@ -61,7 +66,8 @@ serve.handler = function(argv) {
h2: argv.h2,
acceptRemoteConnections: !!argv.acceptRemoteConnections,
cert: argv.h2 ? argv.cert : undefined,
key: argv.h2 ? argv.key : undefined
key: argv.h2 ? argv.key : undefined,
sendSAPTargetCSP: !!argv.sapCspPolicies
};

if (!serverConfig.h2) {
Expand Down
35 changes: 32 additions & 3 deletions test/lib/cli/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ test.serial("ui5 serve: default", async (t) => {
h2: false,
port: 8080,
cert: undefined,
key: undefined
key: undefined,
sendSAPTargetCSP: false
}, "Starting server with specific server config");
});

Expand Down Expand Up @@ -91,7 +92,8 @@ test.serial("ui5 serve --h2", async (t) => {
h2: true,
port: 8443,
key: "randombyte-likes-ponies-key",
cert: "randombyte-likes-ponies-cert"
cert: "randombyte-likes-ponies-cert",
sendSAPTargetCSP: false
}, "Starting server with specific server config");
});

Expand Down Expand Up @@ -161,7 +163,8 @@ test.serial("ui5 serve --key --cert", async (t) => {
h2: true,
port: 8443,
key: "ponies-loaded-from-custompath-key",
cert: "ponies-loaded-from-custompath-crt"
cert: "ponies-loaded-from-custompath-crt",
sendSAPTargetCSP: false
}, "Starting server with specific server config");
});

Expand All @@ -182,3 +185,29 @@ test.serial("ui5 serve --translator --config", async (t) => {
configPath: "path/to/my/config.json"
}, "CLI was called with static translator");
});

test.serial("ui5 serve --sap-csp-policies", async (t) => {
normalizerStub.resolves(projectTree);
serverStub.resolves({});

// loads project tree using http 2
const pPrepareServerConfig = await serve.handler(Object.assign({}, defaultInitialHandlerArgs, {sapCspPolicies: true}));
// preprocess project config
const pServeServer = await pPrepareServerConfig;
// serve server using config
await pServeServer;

const injectedProjectTree = serverStub.getCall(0).args[0];
const injectedServerConfig = serverStub.getCall(0).args[1];

t.deepEqual(injectedProjectTree, projectTree, "Starting server with given project tree");
t.deepEqual(injectedServerConfig, {
changePortIfInUse: true,
acceptRemoteConnections: false,
h2: false,
port: 8080,
cert: undefined,
key: undefined,
sendSAPTargetCSP: true
}, "Starting server with specific server config");
});

0 comments on commit 57d5567

Please sign in to comment.