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