{"action":"created","issue":{"url":"https://api.github.com/repos/hapijs/hapi/issues/2365","labels_url":"https://api.github.com/repos/hapijs/hapi/issues/2365/labels{/name}","comments_url":"https://api.github.com/repos/hapijs/hapi/issues/2365/comments","events_url":"https://api.github.com/repos/hapijs/hapi/issues/2365/events","html_url":"https://github.com/hapijs/hapi/issues/2365","id":55193025,"number":2365,"title":"server.inject - how to mock the request?","user":{"login":"PavelPolyakov","id":839290,"avatar_url":"https://avatars.githubusercontent.com/u/839290?v=3","gravatar_id":"","url":"https://api.github.com/users/PavelPolyakov","html_url":"https://github.com/PavelPolyakov","followers_url":"https://api.github.com/users/PavelPolyakov/followers","following_url":"https://api.github.com/users/PavelPolyakov/following{/other_user}","gists_url":"https://api.github.com/users/PavelPolyakov/gists{/gist_id}","starred_url":"https://api.github.com/users/PavelPolyakov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PavelPolyakov/subscriptions","organizations_url":"https://api.github.com/users/PavelPolyakov/orgs","repos_url":"https://api.github.com/users/PavelPolyakov/repos","events_url":"https://api.github.com/users/PavelPolyakov/events{/privacy}","received_events_url":"https://api.github.com/users/PavelPolyakov/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/hapijs/hapi/labels/question","name":"question","color":"d4c5f9"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-01-22T18:53:17Z","updated_at":"2015-01-22T22:03:40Z","closed_at":null,"body":"Hi there, \r\n\r\nI have a custom auth scheme, which is as simple as this:\r\n~~~javascript\r\n server.auth.scheme('registration', function (server, options) {\r\n        return {\r\n            authenticate: function (request, reply) {\r\n                var user_uuid = request.session.get('user_uuid'),\r\n                    app_uuid = request.session.get('app_uuid');\r\n\r\n                if (typeof user_uuid === 'undefined' ||\r\n                    typeof app_uuid === 'undefined') {\r\n                    reply(Boom.preconditionFailed('user_uuid, app_uuid should be present in the session'));\r\n                } else {\r\n                    reply.continue({'credentials':{}});\r\n                }\r\n            }\r\n        }\r\n    });\r\n~~~\r\n\r\nThe sessions are provided by yar.\r\n\r\nAnd I want to test it using server.inject (btw, if there are some better ways to test it - I would appreciate).\r\nHow could I mock the request object, and emulate the session storage during the test?\r\n\r\nSomething like this:\r\n~~~javascript\r\n/* inside the test */\r\n var options = {\r\n     'method': 'POST',\r\n     'url': '/registration/clientdata',\r\n     /* pseudocode */\r\n     request: {\r\n         session: {\r\n             get: function(key) {\r\n                 return true;\r\n             }\r\n         }\r\n     }\r\n };\r\n serverInstance.inject(options, function(res) {\r\n     // if the validation is not passed, it gives us 412\r\n     expect(res.statusCode).to.equal(200);\r\n     done();\r\n });\r\n~~~\r\n\r\nAny thoughts how to overcome this? And how to have the session support ?\r\n\r\nRegards,"},"comment":{"url":"https://api.github.com/repos/hapijs/hapi/issues/comments/71108365","html_url":"https://github.com/hapijs/hapi/issues/2365#issuecomment-71108365","issue_url":"https://api.github.com/repos/hapijs/hapi/issues/2365","id":71108365,"user":{"login":"PavelPolyakov","id":839290,"avatar_url":"https://avatars.githubusercontent.com/u/839290?v=3","gravatar_id":"","url":"https://api.github.com/users/PavelPolyakov","html_url":"https://github.com/PavelPolyakov","followers_url":"https://api.github.com/users/PavelPolyakov/followers","following_url":"https://api.github.com/users/PavelPolyakov/following{/other_user}","gists_url":"https://api.github.com/users/PavelPolyakov/gists{/gist_id}","starred_url":"https://api.github.com/users/PavelPolyakov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PavelPolyakov/subscriptions","organizations_url":"https://api.github.com/users/PavelPolyakov/orgs","repos_url":"https://api.github.com/users/PavelPolyakov/repos","events_url":"https://api.github.com/users/PavelPolyakov/events{/privacy}","received_events_url":"https://api.github.com/users/PavelPolyakov/received_events","type":"User","site_admin":false},"created_at":"2015-01-22T22:03:40Z","updated_at":"2015-01-22T22:03:40Z","body":"The yar is loaded, and the server instance is the same we're using on production, however it's not clear which \"request\" is loaded during the server.inject operation.\n\n22.01.2015, 23:54, \"Eran Hammer\" <notifications@github.com>:\n> Why not just load yar in the test?\n> \n> —\n> Reply to this email directly or view it on GitHub.\n\n-- \nОтправлено из мобильной Яндекс.Почты: http://m.ya.ru/ymail"}}