Finish fixcontentype
This commit is contained in:
1
MANIFEST
1
MANIFEST
@@ -8,7 +8,6 @@ src/fake_services/service/webservice/__init__.py
|
||||
src/fake_services/service/webservice/fake_http_request_handler.py
|
||||
src/fake_services/service/webservice/fake_web_server.py
|
||||
src/fake_services/service/webservice/fake_web_server_manager.py
|
||||
src/fake_services/service/webservice/file_content_response.py
|
||||
src/fake_services/utility/__init__.py
|
||||
src/fake_services/utility/network/__init__.py
|
||||
src/fake_services/utility/network/ip_address_manager.py
|
||||
|
||||
6
test/service/webservice/cgi-bin/test.py → example/request/test.html
Executable file → Normal file
6
test/service/webservice/cgi-bin/test.py → example/request/test.html
Executable file → Normal file
@@ -1,9 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
print("""
|
||||
<html>
|
||||
<Title>Hello in HTML</Title>
|
||||
<body>
|
||||
<p>Hello There!</p>
|
||||
<p><b>Hi There!</b></p>
|
||||
<p><b>Hi There!</b></p>
|
||||
</body>
|
||||
</html> """)
|
||||
</html>
|
||||
8
example/request/test.xml
Normal file
8
example/request/test.xml
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="ISO8859-1" ?>
|
||||
<note>
|
||||
<to>Tove</to>
|
||||
<from>Jani</from>
|
||||
<heading>Reminder</heading>
|
||||
<body>Don't forget me this weekend!</body>
|
||||
</note>
|
||||
|
||||
7
example/request/testA.html
Normal file
7
example/request/testA.html
Normal file
@@ -0,0 +1,7 @@
|
||||
<html>
|
||||
<Title>Hello in HTMLA</Title>
|
||||
<body>
|
||||
<p>Hello There!</p>
|
||||
<p><b>Hi There!</b></p>
|
||||
</body>
|
||||
</html>
|
||||
7
example/request/testB.html
Normal file
7
example/request/testB.html
Normal file
@@ -0,0 +1,7 @@
|
||||
<html>
|
||||
<Title>Hello in HTMLB</Title>
|
||||
<body>
|
||||
<p>Hello There!</p>
|
||||
<p><b>Hi There!</b></p>
|
||||
</body>
|
||||
</html>
|
||||
7
example/request/testC.html
Normal file
7
example/request/testC.html
Normal file
@@ -0,0 +1,7 @@
|
||||
<html>
|
||||
<Title>Hello in HTMLC</Title>
|
||||
<body>
|
||||
<p>Hello There!</p>
|
||||
<p><b>Hi There!</b></p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -3,15 +3,40 @@ __author__ = 'dev'
|
||||
from fake_services.service.webservice.fake_web_server_manager import FakeWebServerManager
|
||||
|
||||
config = {
|
||||
"/1/a/3/foo/bar.php":
|
||||
"/test":
|
||||
[
|
||||
{
|
||||
"response_content_path": "cgi-bin/testA.html"
|
||||
"response_content_path": "request/test.html"
|
||||
}
|
||||
],
|
||||
"/test1":
|
||||
[
|
||||
{
|
||||
"response_content_path": "request/testA.html"
|
||||
}
|
||||
],
|
||||
"/test2":
|
||||
[
|
||||
{
|
||||
"response_content_path": "request/testB.html"
|
||||
}
|
||||
],
|
||||
"/test3":
|
||||
[
|
||||
{
|
||||
"response_content_path": "request/testC.html"
|
||||
}
|
||||
],
|
||||
"/test4":
|
||||
[
|
||||
{
|
||||
"response_content_path": "request/test.xml"
|
||||
}
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
server = FakeWebServerManager(server_port=80, requests_config=config)
|
||||
server = FakeWebServerManager(server_port=8080, requests_config=config)
|
||||
server.start_server()
|
||||
#server.stop_server()
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ __author__ = 'dev'
|
||||
import os
|
||||
import re
|
||||
import urllib.parse
|
||||
from http.server import CGIHTTPRequestHandler
|
||||
from http.server import SimpleHTTPRequestHandler
|
||||
|
||||
HEADERS_HOST_PARAMETER_KEY_NAME = "Host"
|
||||
REQUEST_LINE_ENCODING = "iso-8859-1"
|
||||
@@ -12,15 +12,15 @@ HOST_PATTERN_KEY_NAME = "host_pattern"
|
||||
RESPONSE_CONTENT_PATH_KEY_NAME = "response_content_path"
|
||||
|
||||
|
||||
class FakeHTTPRequestHandler(CGIHTTPRequestHandler):
|
||||
class FakeHTTPRequestHandler(SimpleHTTPRequestHandler):
|
||||
|
||||
def do_GET(self):
|
||||
self.__set_path_setting()
|
||||
CGIHTTPRequestHandler.do_GET(self)
|
||||
SimpleHTTPRequestHandler.do_GET(self)
|
||||
|
||||
def do_HEAD(self):
|
||||
self.__set_path_setting()
|
||||
CGIHTTPRequestHandler.do_HEAD(self)
|
||||
SimpleHTTPRequestHandler.do_HEAD(self)
|
||||
|
||||
def do_POST(self):
|
||||
self.command = "GET"
|
||||
@@ -35,17 +35,7 @@ class FakeHTTPRequestHandler(CGIHTTPRequestHandler):
|
||||
response_content_path = self.__get_response_content_path(request_config)
|
||||
|
||||
if response_content_path is not None:
|
||||
self.path = self.server.request_handle_script_path
|
||||
parent_path = os.path.join(os.path.sep, os.path.dirname(self.path))
|
||||
if parent_path not in self.cgi_directories:
|
||||
self.cgi_directories.append(parent_path)
|
||||
|
||||
request_data = {
|
||||
RESPONSE_PATH_PARAMETER_KEY_NAME: response_content_path
|
||||
}
|
||||
|
||||
url_parameter = urllib.parse.urlencode(request_data)
|
||||
self.path = self.path + "?" + url_parameter
|
||||
self.path = response_content_path
|
||||
else:
|
||||
self.path = "/404"
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ __author__ = 'dev'
|
||||
|
||||
class FakeWebServer(HTTPServer):
|
||||
|
||||
def __init__(self, server_address, request_handler_class, request_handle_script_path, requests_config):
|
||||
def __init__(self, server_address, request_handler_class, requests_config):
|
||||
super().__init__(server_address, request_handler_class)
|
||||
self.request_handle_script_path = request_handle_script_path
|
||||
self.requests_config = requests_config
|
||||
@@ -1,36 +1,14 @@
|
||||
__author__ = 'dev'
|
||||
|
||||
import os
|
||||
import stat
|
||||
import threading
|
||||
import pkgutil
|
||||
import shutil
|
||||
|
||||
from fake_services.service.webservice.fake_http_request_handler import FakeHTTPRequestHandler
|
||||
from fake_services.service.webservice.fake_web_server import FakeWebServer
|
||||
|
||||
|
||||
class FakeWebServerManager:
|
||||
|
||||
def __init__(self, server_port, requests_config=None, request_handle_script_path=None):
|
||||
|
||||
cgi_path = request_handle_script_path
|
||||
if cgi_path is None:
|
||||
cgi_path = pkgutil.get_loader("fake_services.service.webservice.file_content_response").path
|
||||
newPath = os.path.basename(cgi_path)
|
||||
if not os.path.exists(newPath):
|
||||
shutil.copyfile(cgi_path, newPath)
|
||||
|
||||
cgi_path = newPath
|
||||
|
||||
if not os.path.exists(cgi_path):
|
||||
raise FileNotFoundError
|
||||
|
||||
if not os.access(cgi_path, os.X_OK):
|
||||
st = os.stat(cgi_path)
|
||||
os.chmod(cgi_path, st.st_mode | stat.S_IEXEC)
|
||||
|
||||
self.server = FakeWebServer(('', server_port), FakeHTTPRequestHandler, cgi_path, requests_config)
|
||||
def __init__(self, server_port, requests_config=None):
|
||||
self.server = FakeWebServer(('', server_port), FakeHTTPRequestHandler, requests_config)
|
||||
|
||||
def start_server(self):
|
||||
threading.Thread(target=self.server.serve_forever).start()
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
__author__ = 'dev'
|
||||
import cgi
|
||||
|
||||
from fake_services.service.webservice.fake_http_request_handler import RESPONSE_PATH_PARAMETER_KEY_NAME
|
||||
|
||||
response_file_path = cgi.FieldStorage()[RESPONSE_PATH_PARAMETER_KEY_NAME].value
|
||||
response_file = open(response_file_path, "r")
|
||||
response_file_content = response_file.read()
|
||||
response_file.close()
|
||||
|
||||
print("\n")
|
||||
print(response_file_content)
|
||||
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
__author__ = 'dev'
|
||||
|
||||
response_file_path = "cgi-bin/test.html"
|
||||
response_file = open(response_file_path, "r")
|
||||
response_file_content = response_file.read()
|
||||
response_file.close()
|
||||
|
||||
print("\n")
|
||||
print(response_file_content)
|
||||
8
test/service/webservice/cgi-bin/test.xml
Normal file
8
test/service/webservice/cgi-bin/test.xml
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="ISO8859-1" ?>
|
||||
<note>
|
||||
<to>Tove</to>
|
||||
<from>Jani</from>
|
||||
<heading>Reminder</heading>
|
||||
<body>Don't forget me this weekend!</body>
|
||||
</note>
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
__author__ = 'dev'
|
||||
import cgi
|
||||
|
||||
from fake_services.service.webservice.fake_http_request_handler import RESPONSE_PATH_PARAMETER_KEY_NAME
|
||||
|
||||
response_file_path = cgi.FieldStorage()[RESPONSE_PATH_PARAMETER_KEY_NAME].value
|
||||
response_file = open(response_file_path, "r")
|
||||
response_file_content = response_file.read()
|
||||
response_file.close()
|
||||
|
||||
print("\n")
|
||||
print(response_file_content)
|
||||
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
__author__ = 'dev'
|
||||
import cgi
|
||||
|
||||
from fake_services.service.webservice.fake_http_request_handler import RESPONSE_PATH_PARAMETER_KEY_NAME
|
||||
|
||||
response_file_path = cgi.FieldStorage()[RESPONSE_PATH_PARAMETER_KEY_NAME].value
|
||||
response_file = open(response_file_path, "r")
|
||||
response_file_content = response_file.read()
|
||||
response_file.close()
|
||||
|
||||
print("\n")
|
||||
print(response_file_content)
|
||||
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"":
|
||||
{
|
||||
"host_pattern": "10.0.0.1",
|
||||
"path_pattern": "index.php",
|
||||
"protocol_pattern": "http",
|
||||
"response_content_path": "cgi-bin/test.html"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
__author__ = 'dev'
|
||||
|
||||
import os
|
||||
import stat
|
||||
import unittest
|
||||
import urllib.request
|
||||
import urllib.parse
|
||||
@@ -48,48 +47,18 @@ class TestFakeSever(unittest.TestCase):
|
||||
except:
|
||||
pass
|
||||
|
||||
def test_moved_request(self):
|
||||
server = FakeWebServerManager(server_port=SERVER_PORT, request_handle_script_path=CGI_TEST_SCRIPT_PATH, requests_config=DEFAULT_CONFIG)
|
||||
server.start_server()
|
||||
content = urllib.request.urlopen(TEST_URL).read().decode('utf-8')
|
||||
content2 = urllib.request.urlopen(TEST_URL_EXPAND).read().decode('utf-8')
|
||||
server.stop_server()
|
||||
self.assertNotEqual("", content)
|
||||
self.assertEqual(content, content2)
|
||||
self.assertFalse(content.startswith("#"))
|
||||
|
||||
def test_not_found_script(self):
|
||||
self.assertRaises(FileNotFoundError, FakeWebServerManager, SERVER_PORT, None, CGI_WRONG_SCRIPT_PATH)
|
||||
FakeWebServerManager(server_port=SERVER_PORT)
|
||||
|
||||
def test_set_executable(self):
|
||||
st = os.stat(CGI_FILE_SCRIPT_PATH)
|
||||
if os.access(CGI_FILE_SCRIPT_PATH, os.X_OK):
|
||||
os.chmod(CGI_FILE_SCRIPT_PATH, st.st_mode ^ stat.S_IEXEC)
|
||||
self.assertFalse(os.access(CGI_FILE_SCRIPT_PATH, os.X_OK))
|
||||
FakeWebServerManager(server_port=SERVER_PORT, request_handle_script_path=CGI_FILE_SCRIPT_PATH, requests_config=DEFAULT_CONFIG)
|
||||
self.assertTrue(os.access(CGI_FILE_SCRIPT_PATH, os.X_OK))
|
||||
|
||||
def test_file_request(self):
|
||||
server = FakeWebServerManager(server_port=SERVER_PORT, request_handle_script_path=CGI_FILE_SCRIPT_PATH, requests_config=DEFAULT_CONFIG)
|
||||
server = FakeWebServerManager(server_port=SERVER_PORT, requests_config=DEFAULT_CONFIG)
|
||||
server.start_server()
|
||||
content = urllib.request.urlopen(TEST_URL).read().decode('utf-8')
|
||||
content2 = urllib.request.urlopen(TEST_URL_EXPAND).read().decode('utf-8')
|
||||
server.stop_server()
|
||||
self.assertNotEqual("", content)
|
||||
self.assertEqual(content, content2)
|
||||
self.assertFalse(content.startswith("#"))
|
||||
|
||||
def test_file_var_bin_request(self):
|
||||
server = FakeWebServerManager(server_port=SERVER_PORT, request_handle_script_path=CGI_FILE_SCRIPT_PATH, requests_config=DEFAULT_CONFIG)
|
||||
server.start_server()
|
||||
content = urllib.request.urlopen(TEST_URL_EXPAND).read().decode('utf-8')
|
||||
server.stop_server()
|
||||
self.assertNotEqual("", content)
|
||||
self.assertNotEqual(content, content2)
|
||||
self.assertFalse(content.startswith("#"))
|
||||
|
||||
def test_file_var_bin_request_post(self):
|
||||
server = FakeWebServerManager(server_port=SERVER_PORT, request_handle_script_path=CGI_FILE_SCRIPT_PATH, requests_config=DEFAULT_CONFIG)
|
||||
server = FakeWebServerManager(server_port=SERVER_PORT, requests_config=DEFAULT_CONFIG)
|
||||
server.start_server()
|
||||
content = urllib.request.urlopen(TEST_URL_EXPAND).read().decode('utf-8')
|
||||
data = urllib.parse.urlencode({'q': 'Status'})
|
||||
@@ -100,57 +69,6 @@ class TestFakeSever(unittest.TestCase):
|
||||
self.assertFalse(content.startswith("#"))
|
||||
self.assertEqual(content, content2)
|
||||
|
||||
def test_config_request(self):
|
||||
config = {
|
||||
"/test_request":
|
||||
[
|
||||
{
|
||||
"host_pattern": "10.0.0.1",
|
||||
"response_content_path": "cgi-bin/test.html"
|
||||
}
|
||||
],
|
||||
"/1/a/3/foo/bar.php":
|
||||
[
|
||||
{
|
||||
"host_pattern": "^0.0.0.0$",
|
||||
"response_content_path": "cgi-bin/testA.html"
|
||||
},
|
||||
{
|
||||
"host_pattern": "0.0.0.0:8080",
|
||||
"response_content_path": "cgi-bin/testB.html"
|
||||
},
|
||||
{
|
||||
"response_content_path": "cgi-bin/testC.html"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
server = FakeWebServerManager(server_port=SERVER_PORT, requests_config=config,
|
||||
request_handle_script_path=CGI_VAR_BIN_SCRIPT_PATH)
|
||||
server.start_server()
|
||||
content = urllib.request.urlopen(TEST_URL_EXPAND).read().decode('utf-8')
|
||||
server.stop_server()
|
||||
self.assertNotEqual("", content)
|
||||
self.assertFalse(content.startswith("#"))
|
||||
|
||||
def test_config_request_default_script(self):
|
||||
path = os.path.abspath("cgi-bin/testA.html")
|
||||
config = {
|
||||
"/1/a/3/foo/bar.php":
|
||||
[
|
||||
{
|
||||
"response_content_path": path
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
server = FakeWebServerManager(server_port=SERVER_PORT, requests_config=config)
|
||||
server.start_server()
|
||||
content = urllib.request.urlopen(TEST_URL_EXPAND).read().decode('utf-8')
|
||||
server.stop_server()
|
||||
self.assertNotEqual("", content)
|
||||
self.assertFalse(content.startswith("#"))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user