<s> from __future__ import absolute_import , division , print_function , with_statement <EOL> import socket <EOL> import os <EOL> import signal <EOL> import sys <EOL> import random <EOL> import time <EOL> import threading <EOL> import logging <EOL> import requests <EOL> import multiprocessing <EOL> import time <EOL> from bson . objectid import ObjectId <EOL> from turbo . test . util import unittest <EOL> from turbo import app <EOL> from turbo . conf import app_config <EOL> from turbo import register <EOL> app_config . app_name = '<STR_LIT>' <EOL> app_config . web_application_setting = { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> class HomeHandler ( app . BaseHandler ) : <EOL> def get ( self ) : <EOL> assert self . is_ajax ( ) == False <EOL> self . write ( '<STR_LIT>' ) <EOL> def post ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def put ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> class ApiHandler ( app . BaseHandler ) : <EOL> _get_required_params = [ <EOL> ( '<STR_LIT>' , int , <NUM_LIT:0> ) , <EOL> ( '<STR_LIT>' , int , <NUM_LIT:20> ) , <EOL> ] <EOL> _get_params = { <EOL> '<STR_LIT>' : [ <EOL> ( '<STR_LIT:action>' , None ) , <EOL> ] , <EOL> '<STR_LIT>' : [ <EOL> ( '<STR_LIT>' , basestring , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:bool>' , bool , False ) , <EOL> ( '<STR_LIT:int>' , int , <NUM_LIT:0> ) , <EOL> ( '<STR_LIT:float>' , float , <NUM_LIT:0> ) , <EOL> ( '<STR_LIT>' , ObjectId , None ) , <EOL> ( '<STR_LIT:list>' , list , [ ] ) , <EOL> ] <EOL> } <EOL> _post_required_params = [ <EOL> ( '<STR_LIT>' , int , <NUM_LIT:0> ) , <EOL> ( '<STR_LIT>' , int , <NUM_LIT:20> ) , <EOL> ] <EOL> _post_params = { <EOL> '<STR_LIT>' : [ <EOL> ( '<STR_LIT>' , basestring ) , <EOL> ] , <EOL> } <EOL> def GET ( self ) : <EOL> self . _params = self . parameter <EOL> assert self . _params [ '<STR_LIT>' ] == <NUM_LIT:0> <EOL> assert self . _params [ '<STR_LIT>' ] == <NUM_LIT:20> <EOL> assert self . _params [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> assert self . _params [ '<STR_LIT:action>' ] is None <EOL> assert self . _params [ '<STR_LIT:list>' ] == [ ] <EOL> assert self . is_ajax ( ) == True <EOL> self . _data = { <EOL> '<STR_LIT:value>' : self . _params [ '<STR_LIT>' ] <EOL> } <EOL> def POST ( self ) : <EOL> self . _params = self . parameter <EOL> print ( self . _params ) <EOL> assert self . _params [ '<STR_LIT>' ] == <NUM_LIT:0> <EOL> assert self . _params [ '<STR_LIT>' ] == <NUM_LIT:10> <EOL> self . _data = { <EOL> '<STR_LIT:value>' : self . _params [ '<STR_LIT>' ] <EOL> } <EOL> def PUT ( self ) : <EOL> self . _data = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT:value>' <EOL> } <EOL> } <EOL> def DELETE ( self ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def wo_json ( self , data ) : <EOL> self . write ( self . json_encode ( data , indent = <NUM_LIT:4> ) ) <EOL> PID = None <EOL> URL = None <EOL> def run_server ( port ) : <EOL> register . register_url ( '<STR_LIT:/>' , HomeHandler ) <EOL> register . register_url ( '<STR_LIT>' , HomeHandler ) <EOL> register . register_url ( '<STR_LIT>' , ApiHandler ) <EOL> app . start ( port ) <EOL> def is_used ( port ) : <EOL> sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> if sock . connect_ex ( ( '<STR_LIT:localhost>' , port ) ) == <NUM_LIT:0> : <EOL> return True <EOL> return False <EOL> def setUpModule ( ) : <EOL> port = <NUM_LIT> <EOL> while True : <EOL> if not is_used ( port ) : <EOL> break <EOL> port += <NUM_LIT:1> <EOL> server = multiprocessing . Process ( target = run_server , args = ( port , ) ) <EOL> server . start ( ) <EOL> global PID , URL <EOL> URL = '<STR_LIT>' % port <EOL> PID = server . pid <EOL> def tearDownModule ( ) : <EOL> os . kill ( PID , signal . SIGKILL ) <EOL> class AppTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> global URL <EOL> self . home_url = URL <EOL> self . api_url = URL + '<STR_LIT>' <EOL> def test_get ( self ) : <EOL> resp = requests . get ( self . home_url ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> def test_post ( self ) : <EOL> resp = requests . post ( self . home_url ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> def test_get_api ( self ) : <EOL> resp = requests . get ( self . api_url , headers = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp . json ( ) [ '<STR_LIT>' ] [ '<STR_LIT:value>' ] , '<STR_LIT>' ) <EOL> def test_delete_api ( self ) : <EOL> resp = requests . delete ( self . api_url , headers = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp . json ( ) [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_post_api ( self ) : <EOL> resp = requests . post ( self . api_url , headers = { '<STR_LIT>' : '<STR_LIT>' } , data = { '<STR_LIT>' : <NUM_LIT:10> , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp . json ( ) [ '<STR_LIT>' ] [ '<STR_LIT:value>' ] , '<STR_LIT>' ) <EOL> def test_404 ( self ) : <EOL> resp = requests . get ( self . home_url + '<STR_LIT>' ) <EOL> self . assertTrue ( resp . content . find ( '<STR_LIT>' ) != - <NUM_LIT:1> ) <EOL> def test_context ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
