Friday, March 11, 2011

fake ip in pylons unit testing

controller code:
--------------------------------------------------------------------------
import logging

from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect_to

from samplebundle.lib.base import BaseController, render

log = logging.getLogger(__name__)

class WebenvironsController(BaseController):

    def get_ip(self):
        # Return a rendered template
        #return render('/webenvirons.mako')
        # or, return a response
        ip = request.environ["REMOTE_ADDR"]
        return ip

unit testing code:
--------------------------------------------------------------------------
from samplebundle.tests import *

class TestWebenvironsController(TestController):

    def test_get_ip(self):
        my_extra_environ = {"REMOTE_ADDR":"127.0.0.1"}
        response = self.app.get(url(controller='webenvirons', action='get_ip'), 
                                extra_environ = my_extra_environ)
        assert '127.0.0.1' in response

No comments:

Post a Comment