@@ -2,22 +2,30 @@ import MockAssert from './mock-assert'
2
2
import Response from '../response'
3
3
import { isPlainObject } from '../utils/index'
4
4
import { clone } from '../utils/clone'
5
- import { sortedUrl , toSortedQueryString , isSubset } from './mock-utils'
5
+ import { sortedUrl , toSortedQueryString , filterKeys , diffString } from './mock-utils'
6
+ import { mock } from 'node:test'
6
7
7
8
/**
8
9
* @param {number } id
9
10
* @param {object } props
10
11
* @param {string } props.method
11
12
* @param {string|function } props.url
12
13
* @param {string|function } props.body - request body
14
+ * @param {string } props.mockName
13
15
* @param {object } props.response
14
16
* @param {string } props.response.body
15
17
* @param {object } props.response.headers
16
18
* @param {integer } props.response.status
17
19
*/
20
+
21
+ const MATCHED_AS_UNDEFINED_IN_MOCK = 'MATCHED_AS_UNDEFINED_IN_MOCK'
22
+ const MATCHED_BY_FUNCTION = 'MATCHED_BY_FUNCTION'
23
+ const MISMATCH_BY_FUNCTION = 'MISMATCHED_BY_FUNCTION'
24
+
18
25
function MockRequest ( id , props ) {
19
26
this . id = id
20
27
28
+ this . mockName = props . mockName ? props . mockName : this . id
21
29
this . method = props . method || 'get'
22
30
this . urlFunction = typeof props . url === 'function'
23
31
this . url = props . url
@@ -80,33 +88,100 @@ MockRequest.prototype = {
80
88
return new MockAssert ( this . calls )
81
89
} ,
82
90
83
- /**
84
- * Checks if the request matches with the mock HTTP method, URL, headers and body
85
- *
86
- * @return {boolean }
87
- */
88
- isExactMatch ( request ) {
89
- const bodyMatch = ( ) => {
90
- if ( this . body === undefined ) {
91
- return true
91
+ bodyMatchRequest ( request ) {
92
+ if ( this . body === undefined ) {
93
+ return {
94
+ match : true ,
95
+ mockValue : MATCHED_AS_UNDEFINED_IN_MOCK ,
96
+ value : MATCHED_AS_UNDEFINED_IN_MOCK ,
92
97
}
98
+ }
99
+ if ( this . bodyFunction ) {
100
+ const match = this . body ( request . body ( ) )
101
+ const value = match ? MATCHED_BY_FUNCTION : MISMATCH_BY_FUNCTION
102
+ return { match, mockValue : value , requestValue : value }
103
+ }
104
+ const requestBodyAsString = toSortedQueryString ( request . body ( ) )
105
+ const match = this . body === requestBodyAsString
106
+ return {
107
+ match,
108
+ mockValue : decodeURIComponent ( this . body ) ,
109
+ requestValue : decodeURIComponent ( requestBodyAsString ) ,
110
+ }
111
+ } ,
93
112
94
- return this . bodyFunction
95
- ? this . body ( request . body ( ) )
96
- : this . body === toSortedQueryString ( request . body ( ) )
113
+ urlMatchRequest ( request ) {
114
+ if ( this . urlFunction ) {
115
+ const match = Boolean ( this . url ( request . url ( ) , request . params ( ) ) )
116
+ const value = match ? MATCHED_BY_FUNCTION : MISMATCH_BY_FUNCTION
117
+ return { match, mockValue : value , requestValue : value }
118
+ }
119
+ const requestUrlAsSortedString = sortedUrl ( request . url ( ) )
120
+ const mockRequestUrlAsSortedString = sortedUrl ( this . url )
121
+ const match = mockRequestUrlAsSortedString === requestUrlAsSortedString
122
+ return {
123
+ match,
124
+ mockValue : decodeURIComponent ( mockRequestUrlAsSortedString ) ,
125
+ requestValue : decodeURIComponent ( requestUrlAsSortedString ) ,
97
126
}
127
+ } ,
98
128
99
- const urlMatch = this . urlFunction
100
- ? this . url ( request . url ( ) , request . params ( ) )
101
- : sortedUrl ( this . url ) === sortedUrl ( request . url ( ) )
129
+ headersMatchRequest ( request ) {
130
+ if ( ! this . headers )
131
+ return {
132
+ match : true ,
133
+ mockValue : MATCHED_AS_UNDEFINED_IN_MOCK ,
134
+ value : MATCHED_AS_UNDEFINED_IN_MOCK ,
135
+ }
136
+ if ( this . headersFunction ) {
137
+ const match = this . headers ( request . headers ( ) )
138
+ const value = match ? MATCHED_BY_FUNCTION : MISMATCH_BY_FUNCTION
139
+ return { match, mockValue : value , requestValue : value }
140
+ }
141
+ const filteredRequestHeaders = filterKeys ( this . headersObject , request . headers ( ) )
142
+ const requestHeadersAsSortedString = toSortedQueryString ( filteredRequestHeaders )
143
+ const mockRequestHeadersAsSortedString = toSortedQueryString ( this . headersObject )
144
+ const match = requestHeadersAsSortedString === mockRequestHeadersAsSortedString
145
+
146
+ return {
147
+ match,
148
+ mockValue : mockRequestHeadersAsSortedString ,
149
+ requestValue : requestHeadersAsSortedString ,
150
+ }
151
+ } ,
102
152
103
- const headerMatch =
104
- ! this . headers ||
105
- ( this . headersFunction
106
- ? this . headers ( request . headers ( ) )
107
- : isSubset ( this . headersObject , request . headers ( ) ) )
153
+ methodMatchRequest ( request ) {
154
+ const requestMethod = request . method ( )
155
+ const match = this . method === requestMethod
156
+ return {
157
+ match,
158
+ mockValue : this . method ,
159
+ requestValue : requestMethod ,
160
+ }
161
+ } ,
108
162
109
- return this . method === request . method ( ) && urlMatch && bodyMatch ( ) && headerMatch
163
+ getRequestMatching ( request ) {
164
+ const method = this . methodMatchRequest ( request )
165
+ const url = this . urlMatchRequest ( request )
166
+ const body = this . bodyMatchRequest ( request )
167
+ const headers = this . headersMatchRequest ( request )
168
+ return {
169
+ mockName : this . mockName ,
170
+ isExactMatch : method . match && url . match && body . match && headers . match ,
171
+ isPartialMatch : this . isPartialMatch ( request ) ,
172
+ method,
173
+ url,
174
+ body,
175
+ headers,
176
+ }
177
+ } ,
178
+ /**
179
+ * Checks if the request matches with the mock HTTP method, URL, headers and body
180
+ *
181
+ * @return {boolean }
182
+ */
183
+ isExactMatch ( request ) {
184
+ return this . getRequestMatching ( request ) . isExactMatch
110
185
} ,
111
186
112
187
/**
0 commit comments