Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Fake server with url parameters. #201

firebait opened this Issue Nov 8, 2012 · 6 comments


None yet
5 participants

firebait commented Nov 8, 2012

Hello all,

I made a temporary monkey patch because I need this to work asap and I couldn't find a way to write this test. So, if someone has as suggestion please point me in the right direction so I can correct it.

Here is my problem:

I have a server call that makes a fetch with different dates like so:


this throws an error "Uncaught TypeError: Fake server request processing threw exception: Cannot call method 'slice' of null " when I do this:

this.server.respondWith('GET', "/route/to/server?start_date=2012-1-1", function(xhr){
      xhr.respond( 200, { "Content-Type": "application/json" },'{}');

This happens because it threats the "?" on the url as part of a regular expression when it's a string. This is on the match(ru).slice(1).

So here is my monkey patch for line 3397"

        if (matchOne(response, this.getHTTPMethod(request), requestUrl)) {
            if (typeof response.response == "function") {
                var ru = response.url;
                // Added this line to sinon 
                if(typeof(ru) == 'string'){ru=ru.replace("?", "\\?");}
                // end of monkey patch.
                var args = [request].concat(!ru ? [] : requestUrl.match(ru).slice(1));
                return response.response.apply(response, args);

            return true;



alFReD-NSH commented Nov 9, 2012

Well you can do this instead of that:

var route = "/route/to/server?start_date=2012-1-1";
this.server.respondWith('GET', route.replace('?', '\\?'), function(xhr){


this.server.respondWith('GET', "/route/to/server\\?start_date=2012-1-1", function(xhr){

You can use a more general regex, to escape every special character in regex: route.replace(/([\\/'*+?|()\[\]{}.^$])/g,'\\$1')


cjohansen commented Nov 12, 2012

It sucks that we treat a string as a regular expression. I'll see if I can do something about that without breaking backwards compatibility.


alFReD-NSH commented Nov 12, 2012

@cjohansen You can either use string.indexOf, or escape regex characters and match it.

webgio commented Mar 14, 2013

Not really sure what I am doing but

var route = "/route/to/server?start_date=2012-1-1";
this.server.respondWith('GET', route.replace('?', '\\?'), function(xhr){

works for me with just one backslash

var route = "/route/to/server?start_date=2012-1-1";
this.server.respondWith('GET', route.replace('?', '\?'), function(xhr){

Actually not very friendly way, but works.


alFReD-NSH commented Mar 15, 2013

@webgio that's what I meant by escaping regex special characters. Though I believe this shouldn't be happening in user land.

Same problem here. I am using the solution given above to work it around.

route.replace('?', '\\?')

@cjohansen cjohansen closed this Jul 16, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment