Skip to content

Commit

Permalink
Remove calls to api.hh.ru in tests, add xml to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
glibin committed Jan 15, 2016
1 parent fa76ebe commit 3ef1fc9
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 18 deletions.
17 changes: 16 additions & 1 deletion examples/hello-world/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class MainHandler(RequestHandler):
def get(self):
self.fetch_requests([
('steam', 'http://store.steampowered.com/api/featured/'),
('hhapi', 'https://api.hh.ru/dictionaries', {'locale': 'EN'})
('hhapi', 'https://api.hh.ru/dictionaries', {'locale': 'EN'}),
('xml', self.request.protocol + "://" + self.request.host + '/mock/xml')
], callback=self.complete)


Expand All @@ -65,11 +66,25 @@ def get(self):
test = 1 / 0 # ZeroDivisionError


class MockXmlHandler(tornado.web.RequestHandler):
@staticmethod
def mock_data():
fd = open(path.join(path.dirname(__file__), 'simple.xml'), 'r')
data = fd.read()
fd.close()
return data

def get(self):
self.set_header('Content-Type', 'application/xml')
self.finish(self.mock_data())


class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r'/', MainHandler),
(r'/exception', ExceptionHandler),
(r'/mock/xml', MockXmlHandler),
]

tortik.logger.configure()
Expand Down
24 changes: 24 additions & 0 deletions examples/hello-world/simple.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<test>Привет!</test>
<neighbor name="Austria"/>
<neighbor name="Switzerland"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica"/>
<neighbor name="Colombia"/>
</country>
</data>
3 changes: 2 additions & 1 deletion tortik/page/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ def finish_with_debug(self):
size=sys.getsizeof,
get_params=lambda x: urlparse.parse_qs(x, keep_blank_values=True),
pretty_json=lambda x: json.dumps(x, sort_keys=True, indent=4, ensure_ascii=False),
pretty_xml=lambda x: to_unicode(tostring(x, pretty_print=True, encoding='UTF-8')),
pretty_xml=lambda x: to_unicode(tostring(x.getroot() if hasattr(x, 'getroot') else x,
pretty_print=True, encoding='UTF-8')),
to_unicode=to_unicode,
dumper=dump,
format_exception=lambda x: "".join(traceback.format_exception(*x))
Expand Down
25 changes: 25 additions & 0 deletions tortik_tests/data/simple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"count": 1,
"giveaways": [
{
"state": "ACTIVE",
"golden": true,
"code": "42de59705652efc5306463cafb5db34c",
"gift_as_key": false,
"created": 1452355133,
"is_member": false,
"price": 0,
"winner": null,
"sandbox": null,
"key_stat": null,
"constraint_check": true,
"finish": 1452891600,
"regionlock_type_id": "russia",
"regionlocks": [],
"entries": 180,
"giveaway_type_id": "GIFT",
"entered": null
}
],
"total": 1
}
80 changes: 67 additions & 13 deletions tortik_tests/debug_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
import os

import tornado.web
import tornado.ioloop
from tornado.testing import AsyncHTTPTestCase
from tornado.options import options
from tornado.escape import json_decode

from tortik.page import RequestHandler
import tortik.logger
Expand All @@ -15,12 +17,20 @@ def handle_request():
callback()

handler.fetch_requests(
handler.make_request(
name='status',
method='GET',
full_url='https://api.hh.ru/status',
request_timeout=0.2
),
[
handler.make_request(
name='mock_json',
method='GET',
full_url=handler.request.protocol + "://" + handler.request.host + '/mock/json',
request_timeout=0.2
),
handler.make_request(
name='mock_xml',
method='GET',
full_url=handler.request.protocol + "://" + handler.request.host + '/mock/xml',
request_timeout=0.2
)
],
callback=handle_request
)

Expand Down Expand Up @@ -48,11 +58,55 @@ def get(self):
self.complete('Hello, world!')


class XmlDebugHandler(MainHandler):
def get(self):
def handle_request():
self.complete('Hello, world!')

self.fetch_requests(
self.make_request(
name='xml',
method='GET',
full_url=self.request.protocol + "://" + self.request.host + '/mock/xml',
request_timeout=0.2
),
callback=handle_request
)


class MockJsonHandler(tornado.web.RequestHandler):
@staticmethod
def mock_data():
fd = open(os.path.join(os.path.dirname(__file__), 'data', 'simple.json'), 'r')
data = json_decode(fd.read())
fd.close()
return data

def get(self):
self.finish(self.mock_data())


class MockXmlHandler(tornado.web.RequestHandler):
@staticmethod
def mock_data():
fd = open(os.path.join(os.path.dirname(__file__), 'data', 'simple.xml'), 'r')
data = fd.read()
fd.close()
return data

def get(self):
self.set_header('Content-Type', 'application/xml')
self.finish(self.mock_data())


class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
(r"/exception", ExceptionHandler),
(r"/xml", XmlDebugHandler),
(r"/mock/json", MockJsonHandler),
(r"/mock/xml", MockJsonHandler),
]

settings = dict(
Expand Down Expand Up @@ -88,15 +142,15 @@ def test_main(self):
self.assertEqual(200, response.code)
self.assertIn(b'Log from preprocessor', response.body)
self.assertIn(b'Log from postprocessor', response.body)
self.assertIn(b'https://api.hh.ru/status', response.body)
self.assertIn(b'/mock/json', response.body)

def test_exception(self):
self.http_client.fetch(self.get_url('/exception'), self.stop)
response = self.wait()
self.assertEqual(500, response.code)
self.assertIn(b'Log from preprocessor', response.body)
self.assertNotIn(b'Log from postprocessor', response.body)
self.assertIn(b'https://api.hh.ru/status', response.body)
self.assertIn(b'/mock/json', response.body)
self.assertIn(b'ZeroDivisionError', response.body)

def test_debug_exception(self):
Expand Down Expand Up @@ -133,7 +187,7 @@ def test_debug_false(self):
self.assertEqual(200, response.code)
self.assertNotIn(b'Log from preprocessor', response.body)
self.assertNotIn(b'Log from postprocessor', response.body)
self.assertNotIn(b'https://api.hh.ru/status', response.body)
self.assertNotIn(b'/mock/json', response.body)
self.assertIn(b'Hello, world!', response.body)

def test_debug_true(self):
Expand All @@ -142,15 +196,15 @@ def test_debug_true(self):
self.assertEqual(200, response.code)
self.assertIn(b'Log from preprocessor', response.body)
self.assertIn(b'Log from postprocessor', response.body)
self.assertIn(b'https://api.hh.ru/status', response.body)
self.assertIn(b'/mock/json', response.body)

def test_exception(self):
self.http_client.fetch(self.get_url('/exception'), self.stop)
response = self.wait()
self.assertEqual(500, response.code)
self.assertNotIn(b'Log from preprocessor', response.body)
self.assertNotIn(b'Log from postprocessor', response.body)
self.assertNotIn(b'https://api.hh.ru/status', response.body)
self.assertNotIn(b'/mock/json', response.body)
self.assertNotIn(b'Hello, world!', response.body)

def test_debug_exception(self):
Expand Down Expand Up @@ -183,7 +237,7 @@ def test_debug(self):
self.assertEqual(200, response.code)
self.assertNotIn(b'Log from preprocessor', response.body)
self.assertNotIn(b'Log from postprocessor', response.body)
self.assertNotIn(b'https://api.hh.ru/status', response.body)
self.assertNotIn(b'/mock/json', response.body)
self.assertIn(b'Hello, world!', response.body)

def test_debug_exception(self):
Expand All @@ -192,5 +246,5 @@ def test_debug_exception(self):
self.assertEqual(500, response.code)
self.assertNotIn(b'Log from preprocessor', response.body)
self.assertNotIn(b'Log from postprocessor', response.body)
self.assertNotIn(b'https://api.hh.ru/status', response.body)
self.assertNotIn(b'/mock/json', response.body)
self.assertNotIn(b'Hello, world!', response.body)
23 changes: 20 additions & 3 deletions tortik_tests/preprocessor_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-

import os
import time

import tornado.web
import tornado.ioloop
import tornado.curl_httpclient
from tornado.escape import json_decode
from tornado.testing import AsyncHTTPTestCase

from tortik.page import RequestHandler
Expand All @@ -15,7 +17,8 @@ def handle_request(response):
callback()

http_client = tornado.curl_httpclient.CurlAsyncHTTPClient()
http_client.fetch(b'https://api.hh.ru/status', handle_request, request_timeout=0.2)
http_client.fetch(handler.request.protocol + "://" + handler.request.host + '/mock/json',
handle_request, request_timeout=0.2)


def second_preprocessor(handler, callback):
Expand All @@ -24,7 +27,8 @@ def handle_request(response):
callback()

http_client = tornado.curl_httpclient.CurlAsyncHTTPClient()
http_client.fetch(b'https://api.hh.ru/status', handle_request, request_timeout=0.2)
http_client.fetch(handler.request.protocol + "://" + handler.request.host + '/mock/json',
handle_request, request_timeout=0.2)


def third_preprocessor(handler, callback):
Expand All @@ -47,10 +51,23 @@ def get(self):
raise tornado.web.HTTPError(500)


class MockJsonHandler(tornado.web.RequestHandler):
@staticmethod
def mock_data():
fd = open(os.path.join(os.path.dirname(__file__), 'data', 'simple.json'), 'r')
data = json_decode(fd.read())
fd.close()
return data

def get(self):
self.finish(self.mock_data())


class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r'/', MainHandler),
(r"/mock/json", MockJsonHandler),
]

settings = dict(
Expand Down

0 comments on commit 3ef1fc9

Please sign in to comment.