From 2663441db9d4e78a834bb89955bb1fb78bd7a128 Mon Sep 17 00:00:00 2001 From: Bernardo Heynemann Date: Thu, 8 Mar 2012 16:26:57 -0300 Subject: [PATCH 1/4] Reverting my changes --- Makefile | 2 +- django_pyvows/assertions/models.py | 3 ++- django_pyvows/http_helpers.py | 24 +++++++++----------- setup.py | 3 +-- vows/server_vows.py | 35 +++++++++++++++--------------- vows/settings_vows.py | 20 ++++++++--------- 6 files changed, 41 insertions(+), 46 deletions(-) diff --git a/Makefile b/Makefile index f63b348..3770414 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -pyvows: db +pyvows: @env PYTHONPATH=$$PYTHONPATH:vows/sandbox/:. pyvows --cover --cover_package=django_pyvows --cover_threshold=95 vows/ ci_test: diff --git a/django_pyvows/assertions/models.py b/django_pyvows/assertions/models.py index c97f53d..42e9e57 100644 --- a/django_pyvows/assertions/models.py +++ b/django_pyvows/assertions/models.py @@ -8,7 +8,8 @@ # http://www.opensource.org/licenses/mit-license # Copyright (c) 2011 Rafael Caricio rafael@caricio.com -from pyvows import Vows +from pyvows import Vows, expect + class Model(object): def __init__(self, context, model): diff --git a/django_pyvows/http_helpers.py b/django_pyvows/http_helpers.py index e37ecff..ad6a595 100644 --- a/django_pyvows/http_helpers.py +++ b/django_pyvows/http_helpers.py @@ -1,7 +1,5 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -import httplib2 +import urllib2 +from urllib import urlencode import mimetypes import mimetools @@ -72,12 +70,10 @@ class MultiPartForm(object): def get(url): - h = httplib2.Http() - resp, content = h.request(url) - return resp, content + response = urllib2.urlopen(url) + return response def post(url, data): - h = httplib2.Http() form = MultiPartForm() for field, value in data.iteritems(): @@ -86,15 +82,15 @@ def post(url, data): else: form.add_field(field, str(value)) + request = urllib2.Request(url) body = str(form) - headers = { - 'Content-type': form.get_content_type(), - 'Content-length': str(len(body)) - } + request.add_header('Content-type', form.get_content_type()) + request.add_header('Content-length', len(body)) + request.add_data(body) try: - response = h.request(url, 'POST', headers=headers, body=body) - except httplib2.HttpLib2Error, err: + response = urllib2.urlopen(request) + except urllib2.HTTPError, err: response = err return response diff --git a/setup.py b/setup.py index 85ccb7c..db69264 100644 --- a/setup.py +++ b/setup.py @@ -43,8 +43,7 @@ django-pyvows are pyvows extensions to django web framework. install_requires=[ "pyvows", - "django", - "httplib2" + "django" ], ) diff --git a/vows/server_vows.py b/vows/server_vows.py index a6f2655..14d23c5 100644 --- a/vows/server_vows.py +++ b/vows/server_vows.py @@ -8,10 +8,9 @@ # http://www.opensource.org/licenses/mit-license # Copyright (c) 2011 Rafael Caricio rafael@caricio.com +import urllib2 from os.path import abspath, join, dirname -import httplib2 - from django_pyvows.context import DjangoContext, DjangoHTTPContext from django_pyvows.assertions import * @@ -26,57 +25,57 @@ class HttpContextVows(DjangoHTTPContext): self.start_server() return self.get("/") - def the_return_code_should_be_200(self, (topic, content)): - expect(topic.status).to_equal(200) + def the_return_code_should_be_200(self, topic): + expect(topic.getcode()).to_equal(200) - def should_return_the_success_response(self, (topic, content)): - expect(content).to_equal("hello world") + def should_return_the_success_response(self, topic): + expect(topic.read()).to_equal("hello world") class Methods(DjangoContext): def topic(self): return self.get('/?name=rafael') - def should_be_possible_to_pass_get_parameters(self, (topic, content)): - expect(topic.status).to_equal(200) + def should_be_possible_to_pass_get_parameters(self, topic): + expect(topic.getcode()).to_equal(200) class AskMyName(DjangoContext): def topic(self): return self.get('/say/') - def should_ask_for_my_name(self, (topic, content)): - expect(content).to_equal('What\'s your name?') + def should_ask_for_my_name(self, topic): + expect(topic.read()).to_equal('What\'s your name?') class SayHelloToMe(DjangoContext): def topic(self): return self.get('/say/?name=Rafael') - def should_say_hello_to_me(self, (topic, content)): - expect(content).to_equal('Hello, Rafael!') + 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, content)): - expect(content).to_equal('posted!') + def should_be_posted(self, topic): + expect(topic.read()).to_equal('posted!') class PostFile(DjangoContext): def topic(self): return self.post('/post_file/', {'the_file': open(TEST_FILE_PATH) }) - def should_be_posted_to_the_server(self, (topic, content)): - expect(content).to_equal("the contents") + def should_be_posted_to_the_server(self, topic): + expect(topic.read()).to_equal("the contents") class PostToNotFound(DjangoContext): def topic(self): return self.post('/post_/', {'the_file': open(TEST_FILE_PATH) }) - def should_be_404(self, (topic, content)): - expect(topic.status).to_equal(404) + def should_be_thrown_an_error(self, topic): + expect(topic).to_be_an_error_like(urllib2.HTTPError) diff --git a/vows/settings_vows.py b/vows/settings_vows.py index f7eb934..0dead0a 100644 --- a/vows/settings_vows.py +++ b/vows/settings_vows.py @@ -54,25 +54,25 @@ class SettingsVows(DjangoContext): def topic(self): self.settings.SAY_HELLO_WITHOUT_NAME = False - self.start_server(port=9000) + self.start_server(port=8002) return self.get('/say/') - def should_be_ok(self, (topic, content)): - expect(topic.status).to_equal(200) + def should_be_ok(self, topic): + expect(topic.code).to_equal(200) - def should_ask_for_my_name(self, (topic, content)): - expect(content).to_equal("What's your name?") + def should_ask_for_my_name(self, topic): + expect(topic.read()).to_equal("What's your name?") class SayHelloWithoutName(DjangoHTTPContext): def topic(self): self.settings.SAY_HELLO_WITHOUT_NAME = True - self.start_server(port=9001) + self.start_server(port=8003) return self.get('/say/') - def should_be_ok(self, (topic, content)): - expect(topic.status).to_equal(200) + def should_be_ok(self, topic): + expect(topic.code).to_equal(200) - def should_(self, (topic, content)): - expect(content).to_equal("Hello, guess!") + def should_(self, topic): + expect(topic.read()).to_equal("Hello, guess!") From 617d79e91f33617d86587f8f11c8c7a9635179d0 Mon Sep 17 00:00:00 2001 From: Bernardo Heynemann Date: Thu, 8 Mar 2012 16:28:59 -0300 Subject: [PATCH 2/4] Release 0.5.1 --- django_pyvows/context.py | 2 +- django_pyvows/version.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/django_pyvows/context.py b/django_pyvows/context.py index e971351..a137f00 100644 --- a/django_pyvows/context.py +++ b/django_pyvows/context.py @@ -112,5 +112,5 @@ class DjangoHTTPContext(DjangoContext): def get_url(self, path): if re.match('^https?:\/\/', path): return path - return 'http://%s:%d%s' % (self.host, self.port, path) + return 'http://%s:%d/%s' % (self.host, self.port, path.lstrip('/')) diff --git a/django_pyvows/version.py b/django_pyvows/version.py index d25add3..d0e486e 100644 --- a/django_pyvows/version.py +++ b/django_pyvows/version.py @@ -8,4 +8,4 @@ # http://www.opensource.org/licenses/mit-license # Copyright (c) 2011 Rafael Caricio rafael@caricio.com -__version__ = (0, 5, 0) +__version__ = (0, 5, 1) From 00ecb7fb6dbac767b279be4e877f059e34b51925 Mon Sep 17 00:00:00 2001 From: Bernardo Heynemann Date: Thu, 8 Mar 2012 16:32:29 -0300 Subject: [PATCH 3/4] Http lib back in the game --- Makefile | 2 +- django_pyvows/assertions/models.py | 3 +-- django_pyvows/http_helpers.py | 24 +++++++++++--------- setup.py | 3 ++- vows/server_vows.py | 35 +++++++++++++++--------------- vows/settings_vows.py | 20 ++++++++--------- 6 files changed, 46 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index 3770414..f63b348 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -pyvows: +pyvows: db @env PYTHONPATH=$$PYTHONPATH:vows/sandbox/:. pyvows --cover --cover_package=django_pyvows --cover_threshold=95 vows/ ci_test: diff --git a/django_pyvows/assertions/models.py b/django_pyvows/assertions/models.py index 42e9e57..c97f53d 100644 --- a/django_pyvows/assertions/models.py +++ b/django_pyvows/assertions/models.py @@ -8,8 +8,7 @@ # http://www.opensource.org/licenses/mit-license # Copyright (c) 2011 Rafael Caricio rafael@caricio.com -from pyvows import Vows, expect - +from pyvows import Vows class Model(object): def __init__(self, context, model): diff --git a/django_pyvows/http_helpers.py b/django_pyvows/http_helpers.py index ad6a595..e37ecff 100644 --- a/django_pyvows/http_helpers.py +++ b/django_pyvows/http_helpers.py @@ -1,5 +1,7 @@ -import urllib2 -from urllib import urlencode +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import httplib2 import mimetypes import mimetools @@ -70,10 +72,12 @@ class MultiPartForm(object): def get(url): - response = urllib2.urlopen(url) - return response + h = httplib2.Http() + resp, content = h.request(url) + return resp, content def post(url, data): + h = httplib2.Http() form = MultiPartForm() for field, value in data.iteritems(): @@ -82,15 +86,15 @@ def post(url, data): else: form.add_field(field, str(value)) - request = urllib2.Request(url) body = str(form) - request.add_header('Content-type', form.get_content_type()) - request.add_header('Content-length', len(body)) - request.add_data(body) + headers = { + 'Content-type': form.get_content_type(), + 'Content-length': str(len(body)) + } try: - response = urllib2.urlopen(request) - except urllib2.HTTPError, err: + response = h.request(url, 'POST', headers=headers, body=body) + except httplib2.HttpLib2Error, err: response = err return response diff --git a/setup.py b/setup.py index db69264..85ccb7c 100644 --- a/setup.py +++ b/setup.py @@ -43,7 +43,8 @@ django-pyvows are pyvows extensions to django web framework. install_requires=[ "pyvows", - "django" + "django", + "httplib2" ], ) diff --git a/vows/server_vows.py b/vows/server_vows.py index 14d23c5..a6f2655 100644 --- a/vows/server_vows.py +++ b/vows/server_vows.py @@ -8,9 +8,10 @@ # http://www.opensource.org/licenses/mit-license # Copyright (c) 2011 Rafael Caricio rafael@caricio.com -import urllib2 from os.path import abspath, join, dirname +import httplib2 + from django_pyvows.context import DjangoContext, DjangoHTTPContext from django_pyvows.assertions import * @@ -25,57 +26,57 @@ class HttpContextVows(DjangoHTTPContext): self.start_server() return self.get("/") - def the_return_code_should_be_200(self, topic): - expect(topic.getcode()).to_equal(200) + def the_return_code_should_be_200(self, (topic, content)): + expect(topic.status).to_equal(200) - def should_return_the_success_response(self, topic): - expect(topic.read()).to_equal("hello world") + def should_return_the_success_response(self, (topic, content)): + expect(content).to_equal("hello world") class Methods(DjangoContext): def topic(self): return self.get('/?name=rafael') - def should_be_possible_to_pass_get_parameters(self, topic): - expect(topic.getcode()).to_equal(200) + def should_be_possible_to_pass_get_parameters(self, (topic, content)): + expect(topic.status).to_equal(200) class AskMyName(DjangoContext): def topic(self): return self.get('/say/') - def should_ask_for_my_name(self, topic): - expect(topic.read()).to_equal('What\'s your name?') + def should_ask_for_my_name(self, (topic, content)): + expect(content).to_equal('What\'s your name?') class SayHelloToMe(DjangoContext): def topic(self): return self.get('/say/?name=Rafael') - def should_say_hello_to_me(self, topic): - expect(topic.read()).to_equal('Hello, Rafael!') + def should_say_hello_to_me(self, (topic, content)): + expect(content).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!') + def should_be_posted(self, (topic, content)): + expect(content).to_equal('posted!') class PostFile(DjangoContext): def topic(self): return self.post('/post_file/', {'the_file': open(TEST_FILE_PATH) }) - def should_be_posted_to_the_server(self, topic): - expect(topic.read()).to_equal("the contents") + def should_be_posted_to_the_server(self, (topic, content)): + expect(content).to_equal("the contents") class PostToNotFound(DjangoContext): def topic(self): return self.post('/post_/', {'the_file': open(TEST_FILE_PATH) }) - def should_be_thrown_an_error(self, topic): - expect(topic).to_be_an_error_like(urllib2.HTTPError) + def should_be_404(self, (topic, content)): + expect(topic.status).to_equal(404) diff --git a/vows/settings_vows.py b/vows/settings_vows.py index 0dead0a..f7eb934 100644 --- a/vows/settings_vows.py +++ b/vows/settings_vows.py @@ -54,25 +54,25 @@ class SettingsVows(DjangoContext): def topic(self): self.settings.SAY_HELLO_WITHOUT_NAME = False - self.start_server(port=8002) + self.start_server(port=9000) return self.get('/say/') - def should_be_ok(self, topic): - expect(topic.code).to_equal(200) + def should_be_ok(self, (topic, content)): + expect(topic.status).to_equal(200) - def should_ask_for_my_name(self, topic): - expect(topic.read()).to_equal("What's your name?") + def should_ask_for_my_name(self, (topic, content)): + expect(content).to_equal("What's your name?") class SayHelloWithoutName(DjangoHTTPContext): def topic(self): self.settings.SAY_HELLO_WITHOUT_NAME = True - self.start_server(port=8003) + self.start_server(port=9001) return self.get('/say/') - def should_be_ok(self, topic): - expect(topic.code).to_equal(200) + def should_be_ok(self, (topic, content)): + expect(topic.status).to_equal(200) - def should_(self, topic): - expect(topic.read()).to_equal("Hello, guess!") + def should_(self, (topic, content)): + expect(content).to_equal("Hello, guess!") From 5b183597facf3b7801da66072ea47ff1dbc92102 Mon Sep 17 00:00:00 2001 From: Bernardo Heynemann Date: Thu, 8 Mar 2012 16:33:03 -0300 Subject: [PATCH 4/4] Version 0.5.2 --- django_pyvows/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_pyvows/version.py b/django_pyvows/version.py index d0e486e..7751615 100644 --- a/django_pyvows/version.py +++ b/django_pyvows/version.py @@ -8,4 +8,4 @@ # http://www.opensource.org/licenses/mit-license # Copyright (c) 2011 Rafael Caricio rafael@caricio.com -__version__ = (0, 5, 1) +__version__ = (0, 5, 2)