Merge branch 'linssen-regex-matchers'

This commit is contained in:
slackersoft 2014-06-10 13:10:55 -07:00
commit 1f74c2fc96
2 changed files with 28 additions and 9 deletions

View File

@ -259,14 +259,19 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}
function RequestStub(url, stubData) {
var split = url.split('?');
this.url = split[0];
var normalizeQuery = function(query) {
return query ? query.split('&').sort().join('&') : undefined;
};
this.query = normalizeQuery(split[1]);
if (url instanceof RegExp) {
this.url = url;
this.query = undefined;
} else {
var split = url.split('?');
this.url = split[0];
this.query = split.length > 1 ? normalizeQuery(split[1]) : undefined;
}
this.data = normalizeQuery(stubData);
this.andReturn = function(options) {
@ -277,10 +282,17 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
};
this.matches = function(fullUrl, data) {
var urlSplit = fullUrl.split('?'),
url = urlSplit[0],
query = urlSplit[1];
return this.url === url && this.query === normalizeQuery(query) && (!this.data || this.data === normalizeQuery(data));
var matches = false;
fullUrl = fullUrl.toString();
if (this.url instanceof RegExp) {
matches = this.url.test(fullUrl);
} else {
var urlSplit = fullUrl.split('?'),
url = urlSplit[0],
query = urlSplit[1];
matches = this.url === url && this.query === normalizeQuery(query);
}
return matches && (!this.data || this.data === normalizeQuery(data));
};
}
@ -292,4 +304,3 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jasmine.Ajax = new MockAjax(window);
}
}());

View File

@ -77,6 +77,14 @@ describe("Webmock style mocking", function() {
});
});
it("allows stubs to use RegExp", function () {
mockAjax.stubRequest(/thisGuy/).andReturn({responseText: 'regex', status: 200});
sendRequest(fakeGlobal, 'http://www.example.com/thisGuy/thatGuy');
expect(successSpy).toHaveBeenCalled();
expect(response.responseText).toBe('regex');
});
describe("stubbing with form data", function() {
beforeEach(function() {
mockAjax.stubRequest("http://example.com/someApi", 'foo=bar&baz=quux').andReturn({responseText: "form", status: 201});