aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFilipp Lepalaan <filipp@mac.com>2018-10-10 07:45:27 +0300
committerFilipp Lepalaan <filipp@mac.com>2018-10-10 07:45:27 +0300
commitdce3059a81b2a37580a430b291d4f88d34602700 (patch)
treed104cbec6e07dc9983209b5c36d8f92aa15e9d36
parent52aa39b41b0b139aec2090403bcc7d43ed28579e (diff)
downloadgsx-mockserver-dce3059a81b2a37580a430b291d4f88d34602700.tar.gz
gsx-mockserver-dce3059a81b2a37580a430b291d4f88d34602700.tar.bz2
gsx-mockserver-dce3059a81b2a37580a430b291d4f88d34602700.zip
Reflect all tags
-rwxr-xr-xserve.py70
1 files changed, 41 insertions, 29 deletions
diff --git a/serve.py b/serve.py
index 012ae44..270ddbd 100755
--- a/serve.py
+++ b/serve.py
@@ -1,19 +1,18 @@
#!/usr/bin/env python
+import re
import os
import time
import argparse
from glob import glob
-from io import BytesIO
from string import Template
+import xml.etree.ElementTree as ET
try:
from http.server import BaseHTTPRequestHandler, HTTPServer
except ImportError:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
-from lxml import etree
-
class Handler(BaseHTTPRequestHandler):
@@ -31,38 +30,51 @@ class Handler(BaseHTTPRequestHandler):
if os.getenv('GSX_THROTTLE'):
t = int(os.getenv('GSX_THROTTLE'))
- self.log_message('Throttling for %d' % t)
+ self.log_message('Throttling for %d seconds' % t)
time.sleep(t)
-
- self.send_response(200)
- l = int(self.headers['Content-Length'])
- request = etree.parse(BytesIO(self.rfile.read(l)))
-
- print(etree.tostring(request, pretty_print=True).decode())
-
- context = {}
- r = request.xpath('//repairData|//requestData|//MarkRepairCompleteRequest')
-
- if r:
- for i in r[0].iterchildren():
- context[i.tag] = i.text
-
- self.send_header('Content-Type', 'text/xml; charset="UTF-8"')
- self.end_headers()
-
- with open(self.path, 'r') as f:
- xml = f.read()
- xml = Template(xml).safe_substitute(context)
- self.send_header('Content-Length', len(xml))
- self.wfile.write(xml.encode())
- print(xml)
+
+ try:
+ l = int(self.headers['Content-Length'])
+ xml = self.rfile.read(l).decode('utf8')
+ context = {}
+
+ for v in re.findall(r'<(\w+)>(\w+)', xml, re.I + re.M):
+ k = '$' + v[0]
+ context[k] = v[1]
+
+ print("****** REQUEST ****** (%s) \n%s" % (context, xml))
+
+ self.send_response(200)
+ self.send_header('Content-Type', 'text/xml; charset="UTF-8"')
+ self.end_headers()
+
+ def reflect(m):
+ k = '$' + m.group(1)
+ if context.get(k):
+ return '<%s>%s' % (k, context[k])
+
+ return m.group(0)
+
+ with open(self.path, 'r') as f:
+ xml = f.read()
+ xml = re.sub(r'<(\w+)>\w+', reflect, xml, re.I + re.M)
+ xml = Template(xml).safe_substitute(context)
+ print("****** RESPONSE ****** \n%s" % xml)
+
+ self.send_header('Content-Length', len(xml))
+ self.wfile.write(xml.encode())
+
+ except Exception as e:
+ print(e)
+ self.send_error(500, 'Invalid request')
+ return
def validate_responses():
for r in glob('responses/*.xml'):
try:
- etree.parse(r)
- except etree.XMLSyntaxError:
+ ET.parse(r)
+ except ET.ParseError:
raise Exception('Invalid XML response: %s' % r)