(expectedMethod, expectedUrl, expectedData, expectedHeaders,
expectedKeys)
| 2157 | } |
| 2158 | |
| 2159 | function MockHttpExpectation(expectedMethod, expectedUrl, expectedData, expectedHeaders, |
| 2160 | expectedKeys) { |
| 2161 | |
| 2162 | this.data = expectedData; |
| 2163 | this.headers = expectedHeaders; |
| 2164 | |
| 2165 | this.match = function(method, url, data, headers) { |
| 2166 | if (expectedMethod !== method) return false; |
| 2167 | if (!this.matchUrl(url)) return false; |
| 2168 | if (angular.isDefined(data) && !this.matchData(data)) return false; |
| 2169 | if (angular.isDefined(headers) && !this.matchHeaders(headers)) return false; |
| 2170 | return true; |
| 2171 | }; |
| 2172 | |
| 2173 | this.matchUrl = function(url) { |
| 2174 | if (!expectedUrl) return true; |
| 2175 | if (angular.isFunction(expectedUrl.test)) return expectedUrl.test(url); |
| 2176 | if (angular.isFunction(expectedUrl)) return expectedUrl(url); |
| 2177 | return (expectedUrl === url || compareUrlWithQuery(url)); |
| 2178 | }; |
| 2179 | |
| 2180 | this.matchHeaders = function(headers) { |
| 2181 | if (angular.isUndefined(expectedHeaders)) return true; |
| 2182 | if (angular.isFunction(expectedHeaders)) return expectedHeaders(headers); |
| 2183 | return angular.equals(expectedHeaders, headers); |
| 2184 | }; |
| 2185 | |
| 2186 | this.matchData = function(data) { |
| 2187 | if (angular.isUndefined(expectedData)) return true; |
| 2188 | if (expectedData && angular.isFunction(expectedData.test)) return expectedData.test(data); |
| 2189 | if (expectedData && angular.isFunction(expectedData)) return expectedData(data); |
| 2190 | if (expectedData && !angular.isString(expectedData)) { |
| 2191 | return angular.equals(angular.fromJson(angular.toJson(expectedData)), angular.fromJson(data)); |
| 2192 | } |
| 2193 | // eslint-disable-next-line eqeqeq |
| 2194 | return expectedData == data; |
| 2195 | }; |
| 2196 | |
| 2197 | this.toString = function() { |
| 2198 | return expectedMethod + ' ' + expectedUrl; |
| 2199 | }; |
| 2200 | |
| 2201 | this.params = function(url) { |
| 2202 | var queryStr = url.indexOf('?') === -1 ? '' : url.substring(url.indexOf('?') + 1); |
| 2203 | var strippedUrl = stripQueryAndHash(url); |
| 2204 | |
| 2205 | return angular.extend(extractParamsFromQuery(queryStr), extractParamsFromPath(strippedUrl)); |
| 2206 | }; |
| 2207 | |
| 2208 | function compareUrlWithQuery(url) { |
| 2209 | var urlWithQueryRe = /^([^?]*)\?(.*)$/; |
| 2210 | |
| 2211 | var expectedMatch = urlWithQueryRe.exec(expectedUrl); |
| 2212 | var actualMatch = urlWithQueryRe.exec(url); |
| 2213 | |
| 2214 | return !!(expectedMatch && actualMatch) && |
| 2215 | (expectedMatch[1] === actualMatch[1]) && |
| 2216 | (normalizeQuery(expectedMatch[2]) === normalizeQuery(actualMatch[2])); |
nothing calls this directly
no test coverage detected