Skip to content
This repository

Fake server with url parameters. #201

Closed
firebait opened this Issue November 08, 2012 · 6 comments

5 participants

Marco Tapia Christian Johansen Farid Nouri Neshat Giorgio Bozio Rodrigo Lopes
Marco Tapia

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:

/route/to/server?start_date=2012-1-1

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;
        }

Thanks!

Farid Nouri Neshat

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){
...
});

Or

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')

Christian Johansen
Owner

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.

Farid Nouri Neshat

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

Giorgio Bozio

@alFReD-NSH
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.
Thanks

Farid Nouri Neshat

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

Rodrigo Lopes

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

route.replace('?', '\\?')
Christian Johansen cjohansen closed this July 16, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.