{"issue":{"title":"server.respondWith function - TypeError","user":{"id":1171187,"login":"quangv","url":"https://api.github.com/users/quangv","gravatar_id":"519c182afe6aed1219889c66353b6b4e","avatar_url":"https://secure.gravatar.com/avatar/519c182afe6aed1219889c66353b6b4e?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"milestone":null,"created_at":"2011-12-06T18:10:55Z","labels":[],"state":"open","updated_at":"2011-12-21T09:22:30Z","id":2465391,"html_url":"https://github.com/cjohansen/Sinon.JS/issues/64","pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"closed_at":null,"body":"I'm getting a TypeError while passing a function to server.respondWith\r\n\r\n```\r\nwindow.server = sinon.fakeServer.create();\r\n\r\nserver.respondWith(function(){\r\n  console.log(arguments);\r\n})\r\n\r\n```\r\n\r\n>TypeError: Fake server response body should be string, but was function\r\n\r\n\r\n\r\nThis seems to be the code that causes this\r\n_lib/sinon/util/fake_server.js_\r\n\r\n```\r\n respondWith: function respondWith(method, url, body) {\r\n            if (arguments.length == 1) {\r\n                this.response = responseArray(method);\r\n            }\r\n            ...\r\n\r\nfunction responseArray(handler) {\r\n        var response = handler;\r\n\r\n        if (Object.prototype.toString.call(handler) != \"[object Array]\") {\r\n            response = [200, {}, handler];\r\n        }\r\n\r\n        if (typeof response[2] != \"string\") {\r\n            throw new TypeError(\"Fake server response body should be string, but was \" +\r\n                                typeof response[2]);\r\n        }\r\n\r\n        return response;\r\n    }\r\n\r\n```\r\n\r\nSeems like it doesn't support passing functions into `.respondWith` is this the attended behavior?\r\n\r\nThe `handler` above is really an `Array`","number":64,"url":"https://api.github.com/repos/cjohansen/Sinon.JS/issues/64","comments":2,"assignee":null},"comment":{"user":{"id":77031,"login":"ahamid","url":"https://api.github.com/users/ahamid","gravatar_id":"2c8736930cffc90567b00728c89699c4","avatar_url":"https://secure.gravatar.com/avatar/2c8736930cffc90567b00728c89699c4?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"created_at":"2011-12-21T09:22:30Z","updated_at":"2011-12-21T09:22:30Z","id":3231471,"body":"I just ran into this.  There are two related issues.  When passing a single argument, it appears that `respondWith` attempts to short-circuit the response queue, and sets the response directly:\r\n\r\n    if (arguments.length == 1) {\r\n        this.response = responseArray(method);\r\n    } \r\n\r\nThe subsequent problem is that `responseArray` does not permit a function type as a body:\r\n\r\n    if (typeof response[2] != \"string\") {\r\n        throw new TypeError(\"Fake server response body should be string, but was \" +\r\n                            typeof response[2]);\r\n    }\r\n\r\nThis can be circumvented by using a multiple argument signature, which will result in this line executing instead:\r\n\r\n    push.call(this.responses, {\r\n        method: method,\r\n        url: url,\r\n        response: typeof body == \"function\" ? body : responseArray(body)\r\n    });\r\n\r\nSo either the short-circuit case needs to be eliminated so that the function is called appropriately in the response queue, or the short-circuit case needs the same resolution logic applied as in `processRequest`/`match`.  I'm not familiar enough with the code to offer a confident refactoring patch, but it should be simple.","url":"https://api.github.com/repos/cjohansen/Sinon.JS/issues/comments/3231471"},"action":"created"}