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

Commit

Permalink
Merge pull request #117 from eykrehbein/defect/windows
Browse files Browse the repository at this point in the history
fix windows line endings
  • Loading branch information
jgroom33 authored Dec 17, 2018
2 parents 1d1205e + 5d30dc6 commit 2693e82
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ npm i -g @strest/cli
```bash
# Via Docker
# The image contains everything in the tests directory
docker run -it eykrehbein/strest:latest tests/success/chaining/
docker run -it eykrehbein/strest:latest strest tests/success/chaining/

# Bring your own test and environment
docker run -it --env STREST_URL=https://jsonplaceholder.typicode.com -v ${PWD}:/app/data eykrehbein/strest:latest /data/tests/success/Env/
docker run -it --env STREST_URL=https://jsonplaceholder.typicode.com -v ${PWD}:/app/data eykrehbein/strest:latest strest /data/tests/success/Env/
```

We'll be using the [postman-echo](https://docs.postman-echo.com) test API in this tutorial.
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@strest/cli",
"version": "2.1.1",
"version": "2.1.2",
"description": "A new dimension of REST API testing",
"main": "dist/main.js",
"repository": "https://github.com/eykhagen/strest",
Expand Down Expand Up @@ -51,13 +51,13 @@
]
},
"devDependencies": {
"@types/faker": "^4.1.3",
"@types/jsonpath": "^0.2.0",
"@types/commander": "^2.12.2",
"@types/faker": "^4.1.3",
"@types/jest": "^23.3.2",
"@types/joi": "^13.4.4",
"@types/js-yaml": "^3.11.2",
"@types/jsonfile": "^4.0.1",
"@types/jsonpath": "^0.2.0",
"@types/node": "^10.9.4",
"@types/nunjucks": "^3.1.0",
"@types/ora": "^1.3.4",
Expand Down
25 changes: 14 additions & 11 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const nunjucksEnv = nunjucks.configure(".", {
variableEnd: '$>',
commentStart: '<#',
commentEnd: '#>'
}
},
throwOnUndefined: true
});
nunjucksEnv.addGlobal('Faker', function (faked: string) {
return faker.fake(`{{${faked}}}`);
Expand Down Expand Up @@ -138,18 +139,19 @@ export const performTests = async (testObjects: object[], cmd: any) => {
let nextIndex = keys.indexOf(requestName) +1;
let nextRequest = keys[nextIndex];
let computed = computeRequestObject(requestReponsesObj, testObject.raw, requestName, nextRequest);
if (error !== null) {
// pass
} else {
if (typeof computed.if !== 'undefined') {
if (computed.if.operand == computed.if.equals) {
error = await performRequest(computed, requestName, printAll);
if (computed.error) {
error = { isError: true, message: computed.message, har: null, code: 0 }
}
if (error == null) {
if (typeof computed.parsed.if !== 'undefined') {
if (computed.parsed.if.operand == computed.parsed.if.equals) {
error = await performRequest(computed.parsed, requestName, printAll);
} else {
result = "skipped"
error = { isError: false, message: null, har: null, code: 0 }
}
} else {
error = await performRequest(computed, requestName, printAll);
error = await performRequest(computed.parsed, requestName, printAll);
}
}

Expand Down Expand Up @@ -263,9 +265,10 @@ export const computeRequestObject = (r: any, raw: string, requestName: string, n
var newRaw = lines.slice(start, end - 1).join("\n")
let converted = nunjucksEnv.renderString(newRaw, merged)
const parsed: any = yaml.load(converted)
return parsed
return {parsed: parsed, error: null}
} catch (e) {
throw e;
let err = validationError(`Failed to process ${requestName} request line using nunjucks:\n ${e}`);
return { parsed: null, error: true, message: err}
}
}

Expand Down Expand Up @@ -440,7 +443,7 @@ const performRequest = async (requestObject: requestsObjectSchema, requestName:
var ajv = new Ajv();
let validated = ajv.validate(validate.jsonschema, jsonPathValue);
if (!validated) {
let err = validationError(`The jsonschema ${chalk.bold(validate.jsonschema.toString())} did not validate against ${chalk.bold(jsonPathValue)}`);
let err = validationError(`The jsonschema ${chalk.bold(JSON.stringify(validate.jsonschema))} did not validate against ${chalk.bold(JSON.stringify(jsonPathValue))}`);
return { isError: true, har: har, message: err, code: 1, curl: response.request.toCurl() }
} else {
message = message + "jsonpath " + validate.jsonpath + "(" + jsonPathValue + ")" + " jsonschema validated on " + validate.jsonschema + "\n"
Expand Down
2 changes: 1 addition & 1 deletion src/yaml-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const parseTestingFiles = (pathArray: string[], dir: string) => {
if(dir === null) {
dir = '';
}
parsed.raw = data
parsed.raw = data.replace(/(\rn|\n|\r)/g, '\n')
parsed.relativePath = removeFilename.replace(path.join(process.cwd(), dir), '.' + path.sep);
responseData.push(parsed);
}
Expand Down
13 changes: 13 additions & 0 deletions tests/failure/failureMaxRetries2.strest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: 2
requests:
maxRetries:
request:
url: https://postman-echo.com/time/now
method: GET
delay: 900
maxRetries: 3
validate:
- jsonpath: status
expect: 200
- jsonpath: content
expect: <$ failed.path $>
7 changes: 2 additions & 5 deletions tests/failure/failureNunjucks.strest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ variables:
to_log: true

requests:
invalidValidation:
nunjucksFailure:
request:
url: <$ failUrl $>/todos/1
url: <$ failUrl.0.foo $>/todos/1
method: GET
validate:
- jsonpath: content.id
expect: 100
11 changes: 11 additions & 0 deletions tests/failure/failureNunjucks2.strest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2

variables:
testUrl: https://jsonplaceholder.typicode.com/todos/1
to_log: true

requests:
nunjucksFailure2:
request:
url: <$ failUrl.foo $>/todos/1
method: GET
2 changes: 1 addition & 1 deletion tests/success/Methods/Fake/name.strest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ requests:
value: <$ Faker("name.firstName") $>
- name: first_last
value: <$ Faker("name.firstName") $> <$ Faker("name.lastName") $>
log: true
# log: true
7 changes: 7 additions & 0 deletions tests/success/other/windows_line_endings.strest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2

requests:
arr:
request:
url: https://jsonplaceholder.typicode.com/todos
method: GET
2 changes: 1 addition & 1 deletion tests/success/postman.strest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ requests: # all test requests will be listed here
request:
url: https://postman-echo.com/get # required
method: GET # required
log: true
# log: true
validate:
- jsonpath: status
expect: 200
4 changes: 2 additions & 2 deletions tests/success/validate/object.strest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ requests:
text:
foo: bar
baz: 1
log: true
# log: true
objectReset:
request:
url: https://postman-echo.com/post
Expand All @@ -21,4 +21,4 @@ requests:
validate:
- jsonpath: content.data
expect: {"new":{"foo":"bar","baz":1}}
log: true
# log: true
4 changes: 2 additions & 2 deletions tests/success/validate/object_array.strest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ requests:
baz: 1
- foo1: bar
bax: 2
log: true
# log: true
object_arrayReset:
request:
url: https://postman-echo.com/post
Expand All @@ -24,4 +24,4 @@ requests:
validate:
- jsonpath: content.data.new
expect: {"foo": "bar","baz": 1}
log: true
# log: true
2 changes: 1 addition & 1 deletion tests/success/variables.strest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ requests:
request:
url: <$ testUrl $>
method: GET
log: <$ to_log $>
# log: <$ to_log $>

0 comments on commit 2693e82

Please sign in to comment.