added feature to match data/query against RegExp

Fixes #115
This commit is contained in:
unknown 2015-06-08 16:38:26 +02:00 committed by Gregg Van Hove
parent b7c374a7a6
commit 6e77139011
4 changed files with 86 additions and 10 deletions

View File

@ -226,6 +226,62 @@ beforeEach(function(){
});
```
Complex Requests
----------------
Third-party frameworks may do many requests you can not simply respond to.
Let's assume that you are talking to a SOAP service. SOAP services mostly have identical URL's, but responses differ by the XML request that was send using a POST request.
Let's register a response that will be used if the request body was matched against a RegExp.
```javascript
beforeEach(function(){
// first install the mock
jasmine.Ajax.install();
// Than register a request to which automatically will be responded
jasmine.Ajax.stubRequest(
'https://soap.domain.tld/ws/UserManager',
/.*\<registrationRequest\>.*/
).andReturn({
status: 200,
statusText: 'HTTP/1.1 200 OK',
contentType: 'text/xml;charset=UTF-8',
responseText: '<soap:Envelope><soap:Body><registrationResponse><username>foo</username><password>bar</password></registrationResponse></soap:Body></soap:Envelope>'
});
// Register another response for the same URL, but with different SOAP request
jasmine.Ajax.stubRequest(
'https://soap.domain.tld/ws/UserManager',
/.*\<loginRequest\>.*/
).andReturn({
status: 200,
statusText: 'HTTP/1.1 200 OK',
contentType: 'text/xml;charset=UTF-8',
responseText: '<soap:Envelope><soap:Body><loginResponse><success>true</success></loginResponse></soap:Body></soap:Envelope>'
});
});
```
Or if you also want to avoid the host part of the URL, you can register it using a RegExp for the URL, too.
```javascript
beforeEach(function(){
// first install the mock
jasmine.Ajax.install();
// Than register a request to which automatically will be responded
jasmine.Ajax.stubRequest(
/.*\/ws\/UserManager/,
/.*\<registrationRequest\>.*/
).andReturn({
status: 200,
statusText: 'HTTP/1.1 200 OK',
contentType: 'text/xml;charset=UTF-8',
responseText: '<soap:Envelope><soap:Body><registrationResponse><username>foo</username><password>bar</password></registrationResponse></soap:Body></soap:Envelope>'
});
});
```
Jasmine
-------
http://github.com/jasmine/jasmine

View File

@ -526,7 +526,7 @@ getJasmineRequireObj().AjaxRequestStub = function() {
this.query = split.length > 1 ? normalizeQuery(split[1]) : undefined;
}
this.data = normalizeQuery(stubData);
this.data = (stubData instanceof RegExp) ? stubData : normalizeQuery(stubData);
this.method = method;
this.andReturn = function(options) {
@ -560,17 +560,23 @@ getJasmineRequireObj().AjaxRequestStub = function() {
};
this.matches = function(fullUrl, data, method) {
var matches = false;
var urlMatches = false;
fullUrl = fullUrl.toString();
if (this.url instanceof RegExp) {
matches = this.url.test(fullUrl);
urlMatches = this.url.test(fullUrl);
} else {
var urlSplit = fullUrl.split('?'),
url = urlSplit[0],
query = urlSplit[1];
matches = this.url === url && this.query === normalizeQuery(query);
urlMatches = this.url === url && this.query === normalizeQuery(query);
}
return matches && (!this.data || this.data === normalizeQuery(data)) && (!this.method || this.method === method);
var dataMatches = false;
if (this.data instanceof RegExp) {
dataMatches = this.data.test(data);
} else {
dataMatches = !this.data || this.data === normalizeQuery(data);
}
return urlMatches && dataMatches && (!this.method || this.method === method);
};
}

View File

@ -61,4 +61,12 @@ describe('RequestStub', function() {
expect(stub).toMatchRequest('/foo', 'baz=quux&foo=bar');
expect(stub).not.toMatchRequest('/foo', 'foo=bar');
});
it('can match the data or query params with a RegExp', function() {
var stub = new this.RequestStub('/foo', /ba[rz]=quux/);
expect(stub).toMatchRequest('/foo', 'bar=quux');
expect(stub).toMatchRequest('/foo', 'baz=quux');
expect(stub).not.toMatchRequest('/foo', 'foo=bar');
});
});

View File

@ -17,7 +17,7 @@ getJasmineRequireObj().AjaxRequestStub = function() {
this.query = split.length > 1 ? normalizeQuery(split[1]) : undefined;
}
this.data = normalizeQuery(stubData);
this.data = (stubData instanceof RegExp) ? stubData : normalizeQuery(stubData);
this.method = method;
this.andReturn = function(options) {
@ -51,17 +51,23 @@ getJasmineRequireObj().AjaxRequestStub = function() {
};
this.matches = function(fullUrl, data, method) {
var matches = false;
var urlMatches = false;
fullUrl = fullUrl.toString();
if (this.url instanceof RegExp) {
matches = this.url.test(fullUrl);
urlMatches = this.url.test(fullUrl);
} else {
var urlSplit = fullUrl.split('?'),
url = urlSplit[0],
query = urlSplit[1];
matches = this.url === url && this.query === normalizeQuery(query);
urlMatches = this.url === url && this.query === normalizeQuery(query);
}
return matches && (!this.data || this.data === normalizeQuery(data)) && (!this.method || this.method === method);
var dataMatches = false;
if (this.data instanceof RegExp) {
dataMatches = this.data.test(data);
} else {
dataMatches = !this.data || this.data === normalizeQuery(data);
}
return urlMatches && dataMatches && (!this.method || this.method === method);
};
}