In most situations, my app, upon receiving an HTTP request, sends data
to a third-party API, and returns an empty HttpResponse.  I need to
test that the correct data is sent to the third-party API based on
internal application state.  I'm perplexed as to how to intercept this
data in a unit test.

Here's a sort of dummy version of the code in question.  What I need
to be able to test is that depending on the data in the CurrentState
object, the value of the "message" parameter sent by the API call is
correct.  Any suggestions?

# myapp.views
from myapp.models import CurrentState
from myapp.exceptions import *
from third_party.api import theAPI

def my_view(request):
        state = CurrentState.get(pk=request.session.get('state'))
        new_data = request.POST.get('new_data')
        state.update(new_data)
        if state.x() and state.y():
                result = theAPI.call(msg='%s/%s' % (state.a(), new_data))
        elif state.z():
                result = theAPI.call(msg='%s/%s' % (state.b(), new_data))
        else:
                result = theAPI.call(msg=state.c())
        if not result:
                raise ApiFailureException()
        return HttpResponse('success')


# third_party.api
import urllib, urllib2
from django.conf import settings

class theAPI(object):
        def call(self, msg=''):
                data = urllib.urlencode({'message': msg})
                req = urllib2.Request(url=settings.THIRD_PARTY_API_URL, 
data=data)
                resp = urllib2.urlopen(req)
                return resp.status == '200'

Thanks, folks

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to