Skip to content

Commit

Permalink
feat: cookie support for the capture helper (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardruiter authored Nov 19, 2022
1 parent a7f96e5 commit 3f6baf2
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 8 deletions.
5 changes: 3 additions & 2 deletions docs/handlebars.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ Usage:
}
```

2. **{{capture from='path' regex='\/users\/get\/(.*)?'}}** - For path, you'd need to specify a regex to capture a value.
3. **{{capture from='body' using='jsonpath' selector='$.lastName'}}** - To capture values from the request body, your options are either using='regex' or using='jsonpath'. Selector will change accordingly.
2. **{{capture from='cookies' key='mycookie'}}** - For cookies, you'd need to specify a key to capture a value.
3. **{{capture from='path' regex='\/users\/get\/(.*)?'}}** - For path, you'd need to specify a regex to capture a value.
4. **{{capture from='body' using='jsonpath' selector='$.lastName'}}** - To capture values from the request body, your options are either using='regex' or using='jsonpath'. Selector will change accordingly.

!!!note

Expand Down
12 changes: 12 additions & 0 deletions mocks/capture/cookies/GET.mock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
HTTP/1.1 200 OK
Content-Type: application/json

{{#if (capture from='cookies' key='scenario') }}
{
"scenario": "{{capture from='cookies' key='scenario'}}"
}
{{else}}
{
"scenario": "no cookie found for scenario"
}
{{/if}}
56 changes: 56 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"apicache": "^1.6.3",
"compression": "^1.7.4",
"convert-csv-to-json": "^1.3.3",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"express": "^4.18.1",
"express-query-parser": "^1.3.3",
Expand Down Expand Up @@ -76,6 +77,7 @@
"yargs": "^16.2.0"
},
"devDependencies": {
"@types/cookie-parser": "^1.4.3",
"@types/express": "^4.17.11",
"@types/http-errors": "^1.8.2",
"@types/js-yaml": "^4.0.5",
Expand Down
4 changes: 3 additions & 1 deletion src/handlebar/RequestHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class RequestHelper {
* Registers capture helper
* - Get the request object passed in from the context by calling template({request: req})
* - Get the from value passed in while calling {{capture from=}}, accepted values query, headers, path, body
* - For query and headers, key is required, else if not found a null/undefined value will be automatically returned.
* - For query, cookies and headers, key is required, else if not found a null/undefined value will be automatically returned.
* - For path additional input regex is mandatory, if not passed return error
* - For body additional inputs using and selector are mandatory, if not passed return error
* @returns {void}
Expand All @@ -28,6 +28,8 @@ export class RequestHelper {
return request.query[context.hash.key];
case "headers":
return request.get(context.hash.key);
case "cookies":
return request.cookies[context.hash.key] || "";
case "path":
if (typeof context.hash.regex === "undefined") {
logger.debug("ERROR: No regex specified");
Expand Down
7 changes: 3 additions & 4 deletions src/handlebar/StateHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ export class StateHelper {
*/
register = () => {
this.Handlebars.registerHelper("state", (context: any) => {
const cookie = context.data.root.request.headers.cookie;
const key = context.hash.key;
const value = new RegExp(`mocked-state-${key}=([^;]+)`).exec(cookie);
return value ? value[1] : context.fn(this);
const cookies = context.data.root.request.cookies || {};
const key = `mocked-state-${context.hash.key}`;
return cookies[key] || context.fn(this);
});
};
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import swStats from "swagger-stats";
import cors from "cors";
import compression from "compression";
import { queryParser } from "express-query-parser";
import cookieParser from "cookie-parser";
import ConfigLoader, {
getLoaderInstance,
setLoaderInstance,
} from "./ConfigLoader";
import { CamouflageConfig } from "./ConfigLoader/LoaderInterface";
import { Validation } from "./validation";

const app = express();
// Configure logging for express requests
Expand Down Expand Up @@ -64,6 +64,8 @@ app.use(
parseNumber: true,
})
);
// parse cookies
app.use(cookieParser());
/**
* Initializes required variables and starts a 1 master X workers configuration - FUTURE IMPROVEMENT - Pass a single config object
* @param {string[]} protoIgnore array of files to be ignored during loading proto files
Expand Down

0 comments on commit 3f6baf2

Please sign in to comment.