Refactor
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
const PASS = 'PASS',
|
const PASS = "PASS";
|
||||||
FAIL = 'FAIL',
|
const FAIL = "FAIL";
|
||||||
ERROR = 'ERROR';
|
const ERROR = "ERROR";
|
||||||
|
|
||||||
const styles = {
|
const styles = {
|
||||||
[PASS]: {icon: 'check', class: 'success-response'},
|
[PASS]: { icon: "check", class: "success-response" },
|
||||||
[FAIL]: {icon: 'close', class: 'cl-error-response'},
|
[FAIL]: { icon: "close", class: "cl-error-response" },
|
||||||
[ERROR]: {icon: 'close', class: 'cl-error-response'},
|
[ERROR]: { icon: "close", class: "cl-error-response" },
|
||||||
none: {icon: '', class: ''}
|
none: { icon: "", class: "" }
|
||||||
};
|
};
|
||||||
|
|
||||||
//TODO: probably have to use a more global state for `test`
|
//TODO: probably have to use a more global state for `test`
|
||||||
@@ -15,12 +15,12 @@ export default function runTestScriptWitVariables(script, variables) {
|
|||||||
let pw = {
|
let pw = {
|
||||||
_errors: [],
|
_errors: [],
|
||||||
_testReports: [],
|
_testReports: [],
|
||||||
_report: '',
|
_report: "",
|
||||||
expect: function(value) {
|
expect(value) {
|
||||||
try {
|
try {
|
||||||
return expect(value, this._testReports);
|
return expect(value, this._testReports);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
pw._testReports.push({result: ERROR, message: e});
|
pw._testReports.push({ result: ERROR, message: e });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
test: (descriptor, func) => test(descriptor, func, pw._testReports)
|
test: (descriptor, func) => test(descriptor, func, pw._testReports)
|
||||||
@@ -39,17 +39,17 @@ export default function runTestScriptWitVariables(script, variables) {
|
|||||||
}
|
}
|
||||||
return item;
|
return item;
|
||||||
});
|
});
|
||||||
return {report: pw._report, errors: pw._errors, testResults: testReports};
|
return { report: pw._report, errors: pw._errors, testResults: testReports };
|
||||||
}
|
}
|
||||||
|
|
||||||
function test(descriptor, func, _testReports) {
|
function test(descriptor, func, _testReports) {
|
||||||
_testReports.push({startBlock: descriptor});
|
_testReports.push({ startBlock: descriptor });
|
||||||
try {
|
try {
|
||||||
func();
|
func();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
_testReports.push({result: ERROR, message: e});
|
_testReports.push({ result: ERROR, message: e });
|
||||||
}
|
}
|
||||||
_testReports.push({endBlock: true});
|
_testReports.push({ endBlock: true });
|
||||||
|
|
||||||
// TODO: Organieze and generate text report of each {descriptor: true} section in testReports.
|
// TODO: Organieze and generate text report of each {descriptor: true} section in testReports.
|
||||||
// add checkmark or x depending on if each testReport is pass=true or pass=false
|
// add checkmark or x depending on if each testReport is pass=true or pass=false
|
||||||
@@ -78,68 +78,98 @@ class Expectation {
|
|||||||
} else {
|
} else {
|
||||||
return expectValue === targetValue;
|
return expectValue === targetValue;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
_fmtNot(message) {
|
_fmtNot(message) {
|
||||||
// given a string with "(not)" in it, replaces with "not" or "", depending if the expectation is expecting the positive or inverse (this._not)
|
// given a string with "(not)" in it, replaces with "not" or "", depending if the expectation is expecting the positive or inverse (this._not)
|
||||||
if (this.not === true) {
|
if (this.not === true) {
|
||||||
return message.replace("(not)", "not ");
|
return message.replace("(not)", "not ");
|
||||||
} else {
|
} else {
|
||||||
return message.replace("(not)", "")
|
return message.replace("(not)", "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_fail(message) {
|
_fail(message) {
|
||||||
this._testReports.push({result: FAIL, message})
|
this._testReports.push({ result: FAIL, message });
|
||||||
}
|
}
|
||||||
_pass(message) {
|
_pass(message) {
|
||||||
this._testReports.push({result: PASS});
|
this._testReports.push({ result: PASS });
|
||||||
}
|
}
|
||||||
// TEST METHODS DEFINED BELOW
|
// TEST METHODS DEFINED BELOW
|
||||||
// these are the usual methods that would follow expect(...)
|
// these are the usual methods that would follow expect(...)
|
||||||
toBe(value) {
|
toBe(value) {
|
||||||
return this._satisfies(value)
|
return this._satisfies(value)
|
||||||
? this._pass()
|
? this._pass()
|
||||||
: this._fail(this._fmtNot(`Expected ${this.expectValue} (not)to be ${value}`));
|
: this._fail(
|
||||||
|
this._fmtNot(`Expected ${this.expectValue} (not)to be ${value}`)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
toHaveProperty(value) {
|
toHaveProperty(value) {
|
||||||
return this._satisfies(this.expectValue.hasOwnProperty(value), true)
|
return this._satisfies(this.expectValue.hasOwnProperty(value), true)
|
||||||
? this._pass()
|
? this._pass()
|
||||||
: this._fail(this._fmtNot(`Expected object ${this.expectValue} to (not)have property ${value}`))
|
: this._fail(
|
||||||
|
this._fmtNot(
|
||||||
|
`Expected object ${this.expectValue} to (not)have property ${value}`
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
toBeLevel2xx() {
|
toBeLevel2xx() {
|
||||||
let code = parseInt(this.expectValue);
|
let code = parseInt(this.expectValue);
|
||||||
if (Number.isNaN(code)) {
|
if (Number.isNaN(code)) {
|
||||||
return this._fail(`Expecteded 200-level status but could not parse value ${this.expectValue}`);
|
return this._fail(
|
||||||
|
`Expecteded 200-level status but could not parse value ${this.expectValue}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return this._satisfies(code >= 200 && code < 300)
|
return this._satisfies(code >= 200 && code < 300)
|
||||||
? this._pass() :
|
? this._pass()
|
||||||
this._fail(this._fmtNot(`Expected ${this.expectValue} to (not)be 200-level status`));
|
: this._fail(
|
||||||
|
this._fmtNot(
|
||||||
|
`Expected ${this.expectValue} to (not)be 200-level status`
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
toBeLevel3xx() {
|
toBeLevel3xx() {
|
||||||
let code = parseInt(this.expectValue);
|
let code = parseInt(this.expectValue);
|
||||||
if (Number.isNaN(code)) {
|
if (Number.isNaN(code)) {
|
||||||
return this._fail(`Expected 300-level status but could not parse value ${this.expectValue}`);
|
return this._fail(
|
||||||
|
`Expected 300-level status but could not parse value ${this.expectValue}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return this._satisfies(code >= 300 && code < 400)
|
return this._satisfies(code >= 300 && code < 400)
|
||||||
? this._pass() :
|
? this._pass()
|
||||||
this._fail(this._fmtNot(`Expected ${this.expectValue} to (not)be 300-level status`));
|
: this._fail(
|
||||||
|
this._fmtNot(
|
||||||
|
`Expected ${this.expectValue} to (not)be 300-level status`
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
toBeLevel4xx() {
|
toBeLevel4xx() {
|
||||||
let code = parseInt(this.expectValue);
|
let code = parseInt(this.expectValue);
|
||||||
if (Number.isNaN(code)) {
|
if (Number.isNaN(code)) {
|
||||||
return this._fail(`Expected 400-level status but could not parse value ${this.expectValue}`);
|
return this._fail(
|
||||||
|
`Expected 400-level status but could not parse value ${this.expectValue}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return this._satisfies(code >= 400 && code < 500)
|
return this._satisfies(code >= 400 && code < 500)
|
||||||
? this._pass() :
|
? this._pass()
|
||||||
this._fail(this._fmtNot(`Expected ${this.expectValue} to (not)be 400-level status`));
|
: this._fail(
|
||||||
|
this._fmtNot(
|
||||||
|
`Expected ${this.expectValue} to (not)be 400-level status`
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
toBeLevel5xx() {
|
toBeLevel5xx() {
|
||||||
let code = parseInt(this.expectValue);
|
let code = parseInt(this.expectValue);
|
||||||
if (Number.isNaN(code)) {
|
if (Number.isNaN(code)) {
|
||||||
return this._fail(`Expected 200-level status but could not parse value ${this.expectValue}`);
|
return this._fail(
|
||||||
|
`Expected 200-level status but could not parse value ${this.expectValue}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return this._satisfies(code >= 500 && code < 600)
|
return this._satisfies(code >= 500 && code < 600)
|
||||||
? this._pass()
|
? this._pass()
|
||||||
: this._fail(this._fmtNot(`Expected ${this.expectValue} to (not)be 500-level status`));
|
: this._fail(
|
||||||
|
this._fmtNot(
|
||||||
|
`Expected ${this.expectValue} to (not)be 500-level status`
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -286,9 +286,7 @@
|
|||||||
:class="'icon' + (testsEnabled ? ' info-response' : '')"
|
:class="'icon' + (testsEnabled ? ' info-response' : '')"
|
||||||
id="preRequestScriptButto"
|
id="preRequestScriptButto"
|
||||||
v-tooltip.bottom="{
|
v-tooltip.bottom="{
|
||||||
content: !testsEnabled
|
content: !testsEnabled ? 'Enable Tests' : 'Disable Tests'
|
||||||
? 'Enable Tests'
|
|
||||||
: 'Disable Tests'
|
|
||||||
}"
|
}"
|
||||||
@click="testsEnabled = !testsEnabled"
|
@click="testsEnabled = !testsEnabled"
|
||||||
>
|
>
|
||||||
@@ -296,11 +294,10 @@
|
|||||||
class="material-icons"
|
class="material-icons"
|
||||||
:class="testsEnabled"
|
:class="testsEnabled"
|
||||||
v-if="!testsEnabled"
|
v-if="!testsEnabled"
|
||||||
>assignment_turned_in</i
|
|
||||||
>
|
|
||||||
<i class="material-icons" :class="testsEnabled" v-else
|
|
||||||
>close</i
|
|
||||||
>
|
>
|
||||||
|
assignment_turned_in
|
||||||
|
</i>
|
||||||
|
<i class="material-icons" :class="testsEnabled" v-else>close</i>
|
||||||
</button>
|
</button>
|
||||||
</span>
|
</span>
|
||||||
<span>
|
<span>
|
||||||
@@ -340,7 +337,8 @@
|
|||||||
v-if="testsEnabled"
|
v-if="testsEnabled"
|
||||||
class="orange"
|
class="orange"
|
||||||
label="Tests"
|
label="Tests"
|
||||||
ref="postRequestTests">
|
ref="postRequestTests"
|
||||||
|
>
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
<div class="flex-wrap">
|
<div class="flex-wrap">
|
||||||
@@ -373,20 +371,24 @@
|
|||||||
<div v-if="testReports">
|
<div v-if="testReports">
|
||||||
<div v-for="testReport in testReports">
|
<div v-for="testReport in testReports">
|
||||||
<div v-if="testReport.result">
|
<div v-if="testReport.result">
|
||||||
<span :class="testReport.styles.class"><i class="material-icons">{{testReport.styles.icon}}</i> {{testReport.result}}</span>
|
<span :class="testReport.styles.class">
|
||||||
|
<i class="material-icons">
|
||||||
|
{{ testReport.styles.icon }}
|
||||||
|
</i>
|
||||||
|
{{ testReport.result }}
|
||||||
|
</span>
|
||||||
<ul v-if="testReport.message">
|
<ul v-if="testReport.message">
|
||||||
<li>{{testReport.message}}</li>
|
<li>{{ testReport.message }}</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<div v-else-if="testReport.startBlock">
|
<div v-else-if="testReport.startBlock">
|
||||||
<h4>{{testReport.startBlock}}</h4>
|
<h4>{{ testReport.startBlock }}</h4>
|
||||||
</div>
|
</div>
|
||||||
<div v-else-if="testReport.endBlock"><br/></div>
|
<div v-else-if="testReport.endBlock"><br /></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
</pw-section>
|
</pw-section>
|
||||||
<section id="options">
|
<section id="options">
|
||||||
<input id="tab-one" type="radio" name="options" checked="checked" />
|
<input id="tab-one" type="radio" name="options" checked="checked" />
|
||||||
@@ -1224,7 +1226,7 @@ import querystring from "querystring";
|
|||||||
import textareaAutoHeight from "../directives/textareaAutoHeight";
|
import textareaAutoHeight from "../directives/textareaAutoHeight";
|
||||||
import parseCurlCommand from "../assets/js/curlparser.js";
|
import parseCurlCommand from "../assets/js/curlparser.js";
|
||||||
import getEnvironmentVariablesFromScript from "../functions/preRequest";
|
import getEnvironmentVariablesFromScript from "../functions/preRequest";
|
||||||
import runTestScriptWitVariables from '../functions/postWomanTesting'
|
import runTestScriptWitVariables from "../functions/postWomanTesting";
|
||||||
import parseTemplateString from "../functions/templating";
|
import parseTemplateString from "../functions/templating";
|
||||||
import AceEditor from "../components/ace-editor";
|
import AceEditor from "../components/ace-editor";
|
||||||
import { tokenRequest, oauthRedirect } from "../assets/js/oauth";
|
import { tokenRequest, oauthRedirect } from "../assets/js/oauth";
|
||||||
@@ -2218,9 +2220,10 @@ export default {
|
|||||||
body: this.response.body,
|
body: this.response.body,
|
||||||
headers: this.response.headers
|
headers: this.response.headers
|
||||||
};
|
};
|
||||||
const { testResults } = runTestScriptWitVariables(this.testScript, {response: syntheticResponse});
|
const { testResults } = runTestScriptWitVariables(this.testScript, {
|
||||||
|
response: syntheticResponse
|
||||||
|
});
|
||||||
this.testReports = testResults;
|
this.testReports = testResults;
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
if (error.response) {
|
if (error.response) {
|
||||||
|
|||||||
Reference in New Issue
Block a user