def test_chat(self):
    self.mox.StubOutWithMock(xmpp_request_handler.XmppRequestHandler, '_send')

    request = webapp2.Request.blank('/xmpp', POST={'message_type': 'chat',
                                                   'to': '*****@*****.**',
                                                   'from': '*****@*****.**',
                                                   'chat': 'Chat content'})
    response = webapp2.Response()
    handler = xmpp_request_handler.XmppRequestHandler(request, response)
    data = xmpp_request_handler._FormData()
    data.add_text('from', '*****@*****.**', 'plain')
    data.add_text('to', '*****@*****.**', 'plain')
    data.add_text('body', 'Chat content', 'plain')
    data.add_text(
        'stanza',
        CompareXml(
            '<ns0:message from="*****@*****.**" to="*****@*****.**" '
            'type="chat" xmlns:ns0="jabber:client">'
            '<ns0:body>Chat content</ns0:body>'
            '</ns0:message>'),
        'xml')

    handler._send('/_ah/xmpp/message/chat/', data).AndReturn(
        dispatcher.ResponseTuple('404 Not Found', [], 'Response'))
    self.mox.ReplayAll()
    handler.post()
    self.mox.VerifyAll()
    self.assertEqual('404 Not Found', response.status)
  def test_subscribe(self):
    self.mox.StubOutWithMock(xmpp_request_handler.XmppRequestHandler, '_send')

    request = webapp2.Request.blank('/xmpp',
                                    POST={'message_type': 'subscribe',
                                          'to': '*****@*****.**',
                                          'from': '*****@*****.**',
                                          'subscription_type': 'subscribe'})
    response = webapp2.Response()
    handler = xmpp_request_handler.XmppRequestHandler(request, response)
    data = xmpp_request_handler._FormData()
    data.add_text('from', '*****@*****.**', 'plain')
    data.add_text('to', '*****@*****.**', 'plain')
    data.add_text(
        'stanza',
        CompareXml(
            '<ns0:presence from="*****@*****.**" to="*****@*****.**" '
            'type="subscribe" xmlns:ns0="jabber:client" />'),
        'xml')

    handler._send('/_ah/xmpp/subscription/subscribe/', data).AndReturn(
        dispatcher.ResponseTuple('404 Not Found', [], 'Response'))
    self.mox.ReplayAll()
    handler.post()
    self.mox.VerifyAll()
    self.assertEqual('404 Not Found', response.status)
  def test_send(self):
    self.mox.StubOutWithMock(xmpp_request_handler.XmppRequestHandler,
                             'dispatcher')

    handler = xmpp_request_handler.XmppRequestHandler()
    handler.dispatcher = self.mox.CreateMock(dispatcher.Dispatcher)
    handler.dispatcher.add_request(
        method='POST',
        relative_url='url',
        headers=[('Content-Type',
                  mox.Regex('multipart/form-data; boundary=".*?"'))],
        body=mox.IsA(str),
        source_ip='0.1.0.10',
        fake_login=True)

    data = xmpp_request_handler._FormData()
    self.mox.ReplayAll()
    handler._send('url', data)
    self.mox.VerifyAll()