From e6056c56316bb38eed69c366e346f1eeae979b9b Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Tue, 9 Oct 2018 08:56:30 +0300 Subject: Add option to throttle responses --- .gitignore | 3 +-- README.md | 5 +++-- serve.py | 6 +++++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 270cfe9..8d98f9d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -.env -.env2 +.* diff --git a/README.md b/README.md index e726f77..76be9bf 100644 --- a/README.md +++ b/README.md @@ -9,14 +9,15 @@ The mock responses are collected from example XML snippets in Apple's [API docum Installation ============ -The server has only one external dependency - `lxml` which is used to parse the request XML. It should work with both Python 3 and 2, but is primarily developed and tested on 3. +The server requires Python 3 and has only one external dependency - `lxml` which is used to parse the request XML. $ pip install lxml $ python serve.py Validating XML responses... GSX mock server serving on http://localhost:8080 -You can also specify the port and address to serve on with the `-p` and `-a` arguments - check `-h` for the details. +You can also specify the port and address to serve on with the `-p` and `-a` arguments - check `-h` for the details. Use the `GSX_THROTTLE=X` environment +variable to add a X-second delay to each API response. Usage ===== diff --git a/serve.py b/serve.py index 433e740..2958968 100755 --- a/serve.py +++ b/serve.py @@ -1,6 +1,7 @@ #!/usr/bin/env python import os +import time import argparse from glob import glob from io import BytesIO @@ -28,6 +29,10 @@ class Handler(BaseHTTPRequestHandler): self.send_error(404, msg) return + if os.getenv('GSX_THROTTLE'): + self.log_message('Throttling for %d' % os.getenv('GSX_THROTTLE')) + time.sleep(os.getenv('GSX_THROTTLE')) + self.send_response(200) l = int(self.headers['Content-Length']) request = etree.parse(BytesIO(self.rfile.read(l))) @@ -61,7 +66,6 @@ def validate_responses(): if __name__ == '__main__': - parser = argparse.ArgumentParser() parser = argparse.ArgumentParser() parser.add_argument('-a', '--address', help='Address to host server on', default='localhost') -- cgit v1.2.3