Adding HTTP helpers and tests.

This commit is contained in:
Rafael Caricio 2011-08-22 15:08:00 -03:00
parent d0e639182f
commit 2b9472f309
6 changed files with 18 additions and 8 deletions

View file

@ -10,12 +10,11 @@
import os
import re
import urllib
import urllib2
from pyvows import Vows
from django.http import HttpRequest
from django_pyvows import http_helpers
from django_pyvows.assertions import Url, Model, Template
from django_pyvows.server import DjangoServer
@ -58,10 +57,10 @@ class DjangoContext(Vows.Context):
return Model(self, model_class)
def get(self, path):
return urllib2.urlopen(self.get_url(path))
return http_helpers.get(self.get_url(path))
def post(self, path, params):
return urllib2.urlopen(self.get_url(path), data=urllib.urlencode(params))
return http_helpers.post(self.get_url(path), params)
def get_url(self, path):
ctx = self.parent

View file

@ -77,10 +77,10 @@ def post(url, data):
form = MultiPartForm()
for field, value in data.iteritems():
if isinstance(value, basestring):
form.add_field(field, value)
else:
if hasattr(value, "read"):
form.add_file(field, value.name, value)
else:
form.add_field(field, str(value))
request = urllib2.Request(url)
body = str(form)

View file

@ -25,6 +25,9 @@ def say_hello(request):
name = 'guess'
return HttpResponse("Hello, %s!" % name)
def post_it(request):
return HttpResponse(request.POST['value'])
def get_setting(request, attr):
return HttpResponse(str(getattr(settings, attr)))

View file

@ -109,7 +109,6 @@ TEMPLATE_LOADERS = (
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)

View file

@ -17,6 +17,7 @@ from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
url(r'^$', 'sandbox.main.views.home', name='home'),
url(r'^say/$', 'sandbox.main.views.say_hello', name='say_hello'),
url(r'^post_it/$', 'sandbox.main.views.post_it', name='post_it'),
url(r'^settings/(?P<attr>[\w_]+)/?$', 'sandbox.main.views.get_setting', name='get_setting'),
# url(r'^sandbox/', include('sandbox.foo.urls')),

View file

@ -48,3 +48,11 @@ class HttpContextVows(DjangoHTTPContext):
def should_say_hello_to_me(self, topic):
expect(topic.read()).to_equal('Hello, Rafael!')
class PostIt(DjangoContext):
def topic(self):
return self.post('/post_it/', {'value': 'posted!'})
def should_be_posted(self, topic):
expect(topic.read()).to_equal('posted!')