<s> from hamcrest import * <EOL> from tests . testing_framework . base_test_cases import WebPluginTestCase <EOL> from tests . testing_framework . server import PORT <EOL> import httplib <EOL> from os import path <EOL> from flexmock import flexmock <EOL> import subprocess <EOL> import re <EOL> import shlex <EOL> import tornado . web <EOL> class CustomHandler ( tornado . web . RequestHandler ) : <EOL> def initialize ( self , message ) : <EOL> self . message = message <EOL> def get ( self ) : <EOL> self . write ( self . message ) <EOL> class WebPluginTestCaseTests ( WebPluginTestCase ) : <EOL> HOST = "<STR_LIT:localhost>" <EOL> def test_that_defined_responses_are_set_correctly ( self ) : <EOL> path_response = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> for path , content in path_response . items ( ) : <EOL> self . set_response ( path , content ) <EOL> self . start_server ( ) <EOL> first_response = self . perform_request ( "<STR_LIT>" ) <EOL> second_response = self . perform_request ( "<STR_LIT>" ) <EOL> assert_that ( first_response . read ( ) , equal_to ( path_response [ "<STR_LIT>" ] ) ) <EOL> assert_that ( second_response . read ( ) , equal_to ( path_response [ "<STR_LIT>" ] ) ) <EOL> def test_set_response_from_file ( self ) : <EOL> file_path = path . abspath ( "<STR_LIT>" ) <EOL> self . set_response_from_file ( "<STR_LIT:/>" , file_path ) <EOL> self . start_server ( ) <EOL> response = self . perform_request ( "<STR_LIT:/>" ) . read ( ) <EOL> response_file = open ( file_path , "<STR_LIT:r>" ) <EOL> assert_that ( response , equal_to ( response_file . read ( ) ) ) <EOL> response_file . close ( ) <EOL> def test_that_it_is_possible_to_define_headers_to_be_returned_by_the_server ( self ) : <EOL> headers = { "<STR_LIT>" : "<STR_LIT:value>" } <EOL> self . set_response ( "<STR_LIT:/>" , "<STR_LIT:content>" , headers ) <EOL> self . start_server ( ) <EOL> response = self . perform_request ( "<STR_LIT:/>" ) <EOL> assert_has_header ( response , "<STR_LIT>" , "<STR_LIT:value>" ) <EOL> def test_that_dynamic_methods_facilitate_to_set_response_for_other_methods ( self ) : <EOL> content = "<STR_LIT:content>" <EOL> self . set_post_response ( "<STR_LIT:/>" , content ) <EOL> self . start_server ( ) <EOL> response = self . perform_request ( "<STR_LIT:/>" , method = "<STR_LIT:POST>" ) <EOL> assert_that ( response . read ( ) , equal_to ( content ) ) <EOL> def test_dynamic_methods_with_file_input ( self ) : <EOL> file_path = path . abspath ( "<STR_LIT>" ) <EOL> self . set_post_response_from_file ( "<STR_LIT:/>" , file_path ) <EOL> self . start_server ( ) <EOL> response = self . perform_request ( "<STR_LIT:/>" , "<STR_LIT:POST>" ) . read ( ) <EOL> response_file = open ( file_path , "<STR_LIT:r>" ) <EOL> assert_that ( response , equal_to ( response_file . read ( ) ) ) <EOL> response_file . close ( ) <EOL> def test_setting_different_methods_to_the_same_path ( self ) : <EOL> self . set_get_response ( "<STR_LIT:/>" , "<STR_LIT>" ) <EOL> self . set_post_response ( "<STR_LIT:/>" , "<STR_LIT>" ) <EOL> self . start_server ( ) <EOL> get_response = self . perform_request ( "<STR_LIT:/>" ) . read ( ) <EOL> post_response = self . perform_request ( "<STR_LIT:/>" , "<STR_LIT:POST>" ) . read ( ) <EOL> assert_that ( get_response , equal_to ( "<STR_LIT>" ) ) <EOL> assert_that ( post_response , equal_to ( "<STR_LIT>" ) ) <EOL> def test_that_running_owtf_the_output_of_the_plugin_is_stored ( self ) : <EOL> flexmock ( self ) <EOL> self . should_receive ( "<STR_LIT>" ) . and_return ( [ "<STR_LIT>" ] ) . once ( ) <EOL> self . owtf ( "<STR_LIT>" ) <EOL> assert_that ( self . owtf_output , contains_string ( "<STR_LIT>" ) ) <EOL> def test_that_it_is_possible_to_define_a_custom_handler ( self ) : <EOL> self . set_custom_handler ( "<STR_LIT:/>" , CustomHandler , { "<STR_LIT:message>" : "<STR_LIT:hello>" } ) <EOL> self . start_server ( ) <EOL> response = self . perform_request ( "<STR_LIT:/>" ) . read ( ) <EOL> assert_that ( response , equal_to ( "<STR_LIT:hello>" ) ) <EOL> def test_that_it_is_possible_to_specify_the_response_code ( self ) : <EOL> self . set_get_response ( "<STR_LIT:/>" , "<STR_LIT>" , status_code = <NUM_LIT> ) <EOL> self . start_server ( ) <EOL> response = self . perform_request ( "<STR_LIT:/>" ) <EOL> assert_that ( response . status , equal_to ( <NUM_LIT> ) ) <EOL> def perform_request ( self , path , method = "<STR_LIT:GET>" ) : <EOL> connection = httplib . HTTPConnection ( self . HOST , PORT ) <EOL> connection . request ( method , path ) <EOL> return connection . getresponse ( ) <EOL> def assert_has_header ( response , header_name , header_value = None ) : <EOL> header = response . getheader ( header_name , default = None ) <EOL> assert_that ( header is not None ) <EOL> if header_value is not None : <EOL> assert_that ( header , equal_to ( header_value ) ) </s>
