11import { RequestSnippetSchema } from '@/types'
22import { VerificationRule , VerificationRuleInstance } from '@/types/rules'
33import { matchFilter } from './utils'
4- import { exhaustive } from '@/utils/typescript'
5-
6- type CheckValue = string | number
7-
8- // TODO: this is redundant
9- function getValueFromRule (
10- rule : VerificationRule ,
11- response : RequestSnippetSchema [ 'data' ] [ 'response' ]
12- ) : CheckValue {
13- if ( ! response ) return ''
14- const { type } = rule . value
15-
16- switch ( type ) {
17- case 'recordedValue' :
18- return rule . target === 'status' ? response . statusCode : response . content
19- case 'string' :
20- return rule . value . value
21- case 'variable' :
22- return `VARS['${ rule . value . variableName } ']`
23- default :
24- return exhaustive ( type )
25- }
26- }
27-
28- const getExpectedValue = (
29- rule : VerificationRule ,
30- value : CheckValue
31- ) : CheckValue => {
32- switch ( rule . value . type ) {
33- case 'recordedValue' :
34- return rule . target === 'status'
35- ? value
36- : `String.raw\`${ escapeBackticksAndDollarSign ( value . replace ( / (?: \r \n | \r | \n ) / g, '' ) ) } \``
37- case 'string' :
38- return rule . target === 'status' ? value : `'${ value } '`
39- case 'variable' :
40- return rule . target === 'status' ? `Number(${ value } )` : value
41- default :
42- return exhaustive ( rule . value )
43- }
44- }
45-
46- function getTarget ( rule : VerificationRule ) {
47- const property = rule . target === 'status' ? 'r.status' : 'r.body'
48-
49- if ( rule . value . type === 'recordedValue' && rule . target === 'body' ) {
50- return `${ property } .replace(/(?:\\r\\n|\\r|\\n)/g, '')`
51- }
52-
53- return property
54- }
55-
56- function getCheckExpression ( rule : VerificationRule , value : CheckValue ) : string {
57- const target = getTarget ( rule )
58- const expectedValue = getExpectedValue ( rule , value )
59- console . log ( 'expectedValue' , expectedValue )
60-
61- switch ( rule . operator ) {
62- case 'equals' :
63- return `${ target } === ${ expectedValue } `
64- case 'contains' :
65- return `${ target } .includes(${ expectedValue } )`
66- case 'notContains' :
67- return `!${ target } .includes(${ expectedValue } )`
68- default :
69- return exhaustive ( rule . operator )
70- }
71- }
72-
73- function getValueDescription ( rule : VerificationRule , value : CheckValue ) {
74- switch ( rule . value . type ) {
75- case 'recordedValue' :
76- return rule . target === 'body' ? 'recorded value' : value
77- case 'string' :
78- return value
79- case 'variable' :
80- return `variable "${ rule . value . variableName } "`
81- default :
82- return exhaustive ( rule . value )
83- }
84- }
85-
86- function getCheckDescription (
87- rule : VerificationRule ,
88- value : CheckValue
89- ) : string {
90- const valueDescription = getValueDescription ( rule , value )
91-
92- switch ( rule . operator ) {
93- case 'equals' :
94- return `${ rule . target } equals ${ valueDescription } `
95- case 'contains' :
96- return `${ rule . target } contains ${ valueDescription } `
97- case 'notContains' :
98- return `${ rule . target } does not contain ${ valueDescription } `
99- default :
100- return exhaustive ( rule . operator )
101- }
102- }
4+ import {
5+ getCheckDescription ,
6+ getCheckExpression ,
7+ getValueFromRule ,
8+ } from './verification.utils'
1039
10410export function createVerificationRuleInstance (
10511 rule : VerificationRule
@@ -129,8 +35,8 @@ export function createVerificationRuleInstance(
12935 state . matchedRequestIds = [ ...state . matchedRequestIds , id ]
13036
13137 const value = getValueFromRule ( rule , response )
38+ const checkDescription = getCheckDescription ( rule )
13239 const checkExpression = getCheckExpression ( rule , value )
133- const checkDescription = getCheckDescription ( rule , value )
13440
13541 const verificationSnippet = `check(resp, { '${ checkDescription } ': (r) => ${ checkExpression } , })`
13642
@@ -141,8 +47,3 @@ export function createVerificationRuleInstance(
14147 } ,
14248 }
14349}
144-
145- // Without escaping, backticks and dollar sign break the String.raw template literal
146- function escapeBackticksAndDollarSign ( str : string ) {
147- return str . replace ( / \$ / g, '${"$"}' ) . replace ( / ` / g, '${"`"}' )
148- }
0 commit comments