Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:agrausem/britney
- Loading branch information
Showing
9 changed files
with
135 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[run] | ||
source = | ||
source = | ||
britney | ||
britney/middleware | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
import wsgiref.handlers | ||
|
||
|
||
VERSION = '0.4.0' | ||
VERSION = '0.5.0' | ||
|
||
|
||
def get_user_agent(): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import unittest | ||
import britney | ||
from britney.middleware import Json | ||
from britney.middleware.base import Middleware | ||
from os.path import abspath, dirname, join | ||
import responses | ||
from datetime import datetime | ||
|
||
|
||
class OrderMiddleware(Middleware): | ||
|
||
def process_order(self, response): | ||
if hasattr(response, 'order'): | ||
response.order.append(self.__class__.__name__) | ||
else: | ||
response.order = [self.__class__.__name__] | ||
|
||
|
||
class JsonOrder(OrderMiddleware, Json): | ||
|
||
def process_response(self, response): | ||
self.process_order(response) | ||
return super(JsonOrder, self).process_response(response) | ||
|
||
|
||
class Timer(OrderMiddleware): | ||
|
||
def process_request(self, environ): | ||
self.timer = datetime.now() | ||
|
||
def process_response(self, response): | ||
self.process_order(response) | ||
response.time = datetime.now() - self.timer | ||
return response | ||
|
||
|
||
class TestSporeResponse(unittest.TestCase): | ||
|
||
description_path = join(dirname(abspath(__file__)), 'descriptions') | ||
|
||
def setUp(self): | ||
self.client = britney.new(join(self.description_path, 'api.json')) | ||
|
||
@responses.activate | ||
def test_simple_method(self): | ||
responses.add(responses.GET, 'http://test.api.org/test', | ||
body='{"test": "good"}', status=200, | ||
content_type='application/json') | ||
|
||
response = self.client.test() | ||
|
||
self.assertEqual(response.text, '{"test": "good"}') | ||
self.assertEqual(response.status_code, 200) | ||
|
||
@responses.activate | ||
def test_chaining_middlewares(self): | ||
self.client.enable(Timer) | ||
self.client.enable(JsonOrder) | ||
|
||
responses.add(responses.GET, | ||
'http://test.api.org/test-requires/1.json', | ||
body='{"test": "good"}', status=200, | ||
content_type='application/json') | ||
|
||
response = self.client.test_requires(format='json', id='1') | ||
|
||
self.assertEqual(response.order, ['JsonOrder', 'Timer']) | ||
|
||
@responses.activate | ||
def test_format_middleware(self): | ||
self.client.enable('Json') | ||
|
||
responses.add(responses.GET, | ||
'http://test.api.org/test-requires/1.json', | ||
body='{"test": "good"}', status=200, | ||
content_type='application/json') | ||
|
||
response = self.client.test_requires(format='json', id='1') | ||
self.assertEqual(response.data, {'test': 'good'}) | ||
|
||
@responses.activate | ||
def test_bad_status_code(self): | ||
responses.add(responses.GET, | ||
'http://test.api.org/test-requires/2.json', | ||
body='{"detail": "not found"}', status=404, | ||
content_type='application/json') | ||
|
||
with self.assertRaises(britney.HTTPError): | ||
self.client.test_requires(format='json', id='2') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ commands = nosetests --config=nose.cfg | |
deps = | ||
coverage | ||
nose | ||
responses |