Skip to content
This repository has been archived by the owner on Mar 26, 2021. It is now read-only.

Issue 95 -add support for filename as parameter #124

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,26 @@ requests:
method: GET
```

## Use strest file name as paramter in the tests
You can use the strest file name as a parmater in the tests .

*note* that the strest suffix is removed

**Usage**
The file name for this example is postman-echo.strest.yml

```yml
version: 2
requests:
test-file-name:
request:
url: https://<$ Filename() $>.com/get
method: GET
validate:
- jsonpath: status
expect: 200
```

## Response Validation

The immediate response is stored in [HAR Format](http://www.softwareishard.com/blog/har-12-spec/#response)
Expand Down
1 change: 1 addition & 0 deletions src/configSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export const Schema = Joi.object({
// Created dynamically
raw: Joi.string().required(),
relativePath: Joi.string().required(),
fileName: Joi.string().required(),
});

export const BulkSchema = Joi.array().items(Joi.string());
Expand Down
10 changes: 6 additions & 4 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export const performTests = async (testObjects: object[], cmd: any) => {
let keys = Object.keys(requests);
let nextIndex = keys.indexOf(requestName) +1;
let nextRequest = keys[nextIndex];
let computed = computeRequestObject(requestReponsesObj, testObject.raw, requestName, nextRequest);
let computed = computeRequestObject(requestReponsesObj, testObject.raw, requestName, nextRequest, testObject.fileName);
if (computed.error) {
error = { isError: true, message: computed.message, har: null, code: 0 }
}
Expand Down Expand Up @@ -244,12 +244,14 @@ export const performTests = async (testObjects: object[], cmd: any) => {
* Use nunjucks to replace and update the object
* @param obj working obj
*/
export const computeRequestObject = (r: any, raw: string, requestName: string, nextRequest: string) => {

let merged = { ...r, ...definedVariables };
export const computeRequestObject = (r: any, raw: string, requestName: string, nextRequest: string, fileName: string) => {
let merged = { ...r, ...definedVariables};
nunjucksEnv.addGlobal('JsonPath', function (path: string) {
return jp.value(merged, path)
})
nunjucksEnv.addGlobal('Filename', function () {
return fileName ? fileName.replace('.strest','') : '';
})
// Parse obj using nunjucks
try {
const regexpStart = new RegExp("^\\s{1,6}" + requestName + ":","gm")
Expand Down
2 changes: 2 additions & 0 deletions src/yaml-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ export const parseTestingFiles = (pathArray: string[], dir: string) => {
if(typeof filePath === 'string') {
const data = fs.readFileSync(filePath.toString(), 'utf8');
const parsed: any = yaml.safeLoad(data);
const { name:fileName } = path.parse(filePath);
const removeFilename = path.dirname(filePath) + path.sep;
if(dir === null) {
dir = '';
}
parsed.raw = data.replace(/(\rn|\n|\r)/g, '\n')
parsed.fileName = fileName;
parsed.relativePath = removeFilename.replace(path.join(process.cwd(), dir), '.' + path.sep);
responseData.push(parsed);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/success/file/postman-echo.com.strest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2 # only version at the moment
requests: # all test requests will be listed here
test-file-name: # name the request however you want
request:
url: https://<$ Filename() $>/get # required
method: GET # required
# log: true
validate:
- jsonpath: status
expect: 200
10 changes: 10 additions & 0 deletions tests/success/file/postman-echo.strest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2 # only version at the moment
requests: # all test requests will be listed here
test-file-name-1: # name the request however you want
request:
url: https://<$ Filename() $>.com/get # required
method: GET # required
# log: true
validate:
- jsonpath: status
expect: 200