Skip to content

Commit

Permalink
Merge pull request #2219 from brussee/basetest
Browse files Browse the repository at this point in the history
minor refactor basetest
  • Loading branch information
whirm committed May 20, 2016
2 parents 4e95419 + b12ada0 commit 72f6538
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions Tribler/Test/Core/Modules/RestApi/base_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,25 @@ def startProducing(self, consumer):
return succeed(None)


class AbstractApiTest(TestAsServer):
class AbstractBaseApiTest(TestAsServer):
"""
Tests for the Tribler HTTP API should create a subclass of this class.
This class contains some auxiliary methods to perform requests and to check the right response code/
"""

def setUpPreSession(self):
super(AbstractBaseApiTest, self).setUpPreSession()
self.config.set_http_api_enabled(True)
self.config.set_megacache(True)

def do_request(self, endpoint, request_type, post_data):
agent = Agent(reactor)
return agent.request(request_type, 'http://localhost:%s/%s' % (self.session.get_http_api_port(), endpoint),
Headers({'User-Agent': ['Tribler ' + version_id]}), POSTDataProducer(post_data))


class AbstractApiTest(AbstractBaseApiTest):
"""
This class contains some helper methods to perform requests and to check the right response code/
response json returned.
"""

Expand All @@ -38,11 +53,6 @@ def __init__(self, *args, **kwargs):
self.expected_response_json = None
self.should_check_equality = True

def setUpPreSession(self):
super(AbstractApiTest, self).setUpPreSession()
self.config.set_http_api_enabled(True)
self.config.set_megacache(True)

def parse_body(self, body):
if body is not None and self.should_check_equality:
self.assertDictEqual(json.loads(body), self.expected_response_json)
Expand All @@ -58,7 +68,6 @@ def do_request(self, endpoint, expected_code=200, expected_json=None, request_ty
self.expected_response_code = expected_code
self.expected_response_json = expected_json

agent = Agent(reactor)
return agent.request(request_type, 'http://localhost:%s/%s' % (self.session.get_http_api_port(), endpoint),
Headers({'User-Agent': ['Tribler ' + version_id]}), POSTDataProducer(post_data))\
.addCallback(self.parse_response).addCallback(self.parse_body)
return super(AbstractApiTest, self).do_request(endpoint, request_type, post_data)\
.addCallback(self.parse_response)\
.addCallback(self.parse_body)

0 comments on commit 72f6538

Please sign in to comment.