@@ -6,172 +6,41 @@ import { QueryParam } from './query-param'
6
6
import { pathToPosix } from '../services/util'
7
7
import { printDeprecationWarning } from '../reporters/util'
8
8
import { Content , Entrypoint } from './construct'
9
+ import { Assertion as CoreAssertion , NumericAssertionBuilder , GeneralAssertionBuilder } from './internal/assertion'
9
10
10
- // eslint-disable-next-line no-restricted-syntax
11
- enum AssertionSource {
12
- STATUS_CODE = 'STATUS_CODE' ,
13
- JSON_BODY = 'JSON_BODY' ,
14
- HEADERS = 'HEADERS' ,
15
- TEXT_BODY = 'TEXT_BODY' ,
16
- RESPONSE_TIME = 'RESPONSE_TIME' ,
17
- }
18
-
19
- // eslint-disable-next-line no-restricted-syntax
20
- enum AssertionComparison {
21
- EQUALS = 'EQUALS' ,
22
- NOT_EQUALS = 'NOT_EQUALS' ,
23
- HAS_KEY = 'HAS_KEY' ,
24
- NOT_HAS_KEY = 'NOT_HAS_KEY' ,
25
- HAS_VALUE = 'HAS_VALUE' ,
26
- NOT_HAS_VALUE = 'NOT_HAS_VALUE' ,
27
- IS_EMPTY = 'IS_EMPTY' ,
28
- NOT_EMPTY = 'NOT_EMPTY' ,
29
- GREATER_THAN = 'GREATER_THAN' ,
30
- LESS_THAN = 'LESS_THAN' ,
31
- CONTAINS = 'CONTAINS' ,
32
- NOT_CONTAINS = 'NOT_CONTAINS' ,
33
- IS_NULL = 'IS_NULL' ,
34
- NOT_NULL = 'NOT_NULL' ,
35
- }
11
+ type AssertionSource =
12
+ | 'STATUS_CODE'
13
+ | 'JSON_BODY'
14
+ | 'HEADERS'
15
+ | 'TEXT_BODY'
16
+ | 'RESPONSE_TIME'
36
17
37
- export interface Assertion {
38
- source : string ,
39
- property : string ,
40
- comparison : string ,
41
- target : string ,
42
- regex : string | null ,
43
- }
18
+ export type Assertion = CoreAssertion < AssertionSource >
44
19
45
20
export class AssertionBuilder {
46
21
static statusCode ( ) {
47
- return new NumericAssertionBuilder ( AssertionSource . STATUS_CODE )
22
+ return new NumericAssertionBuilder < AssertionSource > ( ' STATUS_CODE' )
48
23
}
49
24
50
25
static jsonBody ( property ?: string ) {
51
- return new GeneralAssertionBuilder ( AssertionSource . JSON_BODY , property )
26
+ return new GeneralAssertionBuilder < AssertionSource > ( ' JSON_BODY' , property )
52
27
}
53
28
54
29
static headers ( property ?: string , regex ?: string ) {
55
- return new GeneralAssertionBuilder ( AssertionSource . HEADERS , property , regex )
30
+ return new GeneralAssertionBuilder < AssertionSource > ( ' HEADERS' , property , regex )
56
31
}
57
32
58
33
static textBody ( property ?: string ) {
59
- return new GeneralAssertionBuilder ( AssertionSource . TEXT_BODY , property )
34
+ return new GeneralAssertionBuilder < AssertionSource > ( ' TEXT_BODY' , property )
60
35
}
61
36
62
37
/** @deprecated Use responseTime() instead */
63
38
static responseTme ( ) {
64
- return new NumericAssertionBuilder ( AssertionSource . RESPONSE_TIME )
39
+ return new NumericAssertionBuilder < AssertionSource > ( ' RESPONSE_TIME' )
65
40
}
66
41
67
42
static responseTime ( ) {
68
- return new NumericAssertionBuilder ( AssertionSource . RESPONSE_TIME )
69
- }
70
- }
71
-
72
- class NumericAssertionBuilder {
73
- source : AssertionSource
74
- constructor ( source : AssertionSource ) {
75
- this . source = source
76
- }
77
-
78
- equals ( target : number ) : Assertion {
79
- return this . _toAssertion ( AssertionComparison . EQUALS , target )
80
- }
81
-
82
- notEquals ( target : number ) : Assertion {
83
- return this . _toAssertion ( AssertionComparison . NOT_EQUALS , target )
84
- }
85
-
86
- lessThan ( target : number ) : Assertion {
87
- return this . _toAssertion ( AssertionComparison . LESS_THAN , target )
88
- }
89
-
90
- greaterThan ( target : number ) : Assertion {
91
- return this . _toAssertion ( AssertionComparison . GREATER_THAN , target )
92
- }
93
-
94
- /** @private */
95
- private _toAssertion ( comparison : AssertionComparison , target : number ) : Assertion {
96
- return { source : this . source , comparison, property : '' , target : target . toString ( ) , regex : null }
97
- }
98
- }
99
-
100
- class GeneralAssertionBuilder {
101
- source : AssertionSource
102
- property ?: string
103
- regex ?: string
104
- constructor ( source : AssertionSource , property ?: string , regex ?: string ) {
105
- this . source = source
106
- this . property = property
107
- this . regex = regex
108
- }
109
-
110
- equals ( target : string | number | boolean ) : Assertion {
111
- return this . _toAssertion ( AssertionComparison . EQUALS , target )
112
- }
113
-
114
- notEquals ( target : string | number | boolean ) : Assertion {
115
- return this . _toAssertion ( AssertionComparison . NOT_EQUALS , target )
116
- }
117
-
118
- hasKey ( target : string ) : Assertion {
119
- return this . _toAssertion ( AssertionComparison . HAS_KEY , target )
120
- }
121
-
122
- notHasKey ( target : string ) : Assertion {
123
- return this . _toAssertion ( AssertionComparison . NOT_HAS_KEY , target )
124
- }
125
-
126
- hasValue ( target : string | number | boolean ) : Assertion {
127
- return this . _toAssertion ( AssertionComparison . HAS_VALUE , target )
128
- }
129
-
130
- notHasValue ( target : string | number | boolean ) : Assertion {
131
- return this . _toAssertion ( AssertionComparison . NOT_HAS_VALUE , target )
132
- }
133
-
134
- isEmpty ( ) {
135
- return this . _toAssertion ( AssertionComparison . IS_EMPTY )
136
- }
137
-
138
- notEmpty ( ) {
139
- return this . _toAssertion ( AssertionComparison . NOT_EMPTY )
140
- }
141
-
142
- lessThan ( target : string | number | boolean ) : Assertion {
143
- return this . _toAssertion ( AssertionComparison . LESS_THAN , target )
144
- }
145
-
146
- greaterThan ( target : string | number | boolean ) : Assertion {
147
- return this . _toAssertion ( AssertionComparison . GREATER_THAN , target )
148
- }
149
-
150
- contains ( target : string ) : Assertion {
151
- return this . _toAssertion ( AssertionComparison . CONTAINS , target )
152
- }
153
-
154
- notContains ( target : string ) : Assertion {
155
- return this . _toAssertion ( AssertionComparison . NOT_CONTAINS , target )
156
- }
157
-
158
- isNull ( ) {
159
- return this . _toAssertion ( AssertionComparison . IS_NULL )
160
- }
161
-
162
- isNotNull ( ) {
163
- return this . _toAssertion ( AssertionComparison . NOT_NULL )
164
- }
165
-
166
- /** @private */
167
- private _toAssertion ( comparison : AssertionComparison , target ?: string | number | boolean ) : Assertion {
168
- return {
169
- source : this . source ,
170
- comparison,
171
- property : this . property ?? '' ,
172
- target : target ?. toString ( ) ?? '' ,
173
- regex : this . regex ?? null ,
174
- }
43
+ return new NumericAssertionBuilder < AssertionSource > ( 'RESPONSE_TIME' )
175
44
}
176
45
}
177
46
0 commit comments