Skip to content

Commit

Permalink
Added endpoint to allow testing alternate response codes
Browse files Browse the repository at this point in the history
  • Loading branch information
kamermans committed Mar 26, 2018
1 parent 7a769a6 commit c1ae913
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions test/requirements/web/webserver.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
#!/usr/bin/env python3

import os, sys
import os, sys, re
import http.server
import socketserver


class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):

self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.end_headers()

response_body = ""
response_code = 200

if self.path == "/headers":
self.wfile.write(self.headers.as_string().encode())
response_body += self.headers.as_string()
elif self.path == "/port":
response = "answer from port %s\n" % PORT
self.wfile.write(response.encode())
response_body += "answer from port %s\n" % PORT
elif re.match("/status/(\d+)", self.path):
result = re.match("/status/(\d+)", self.path)
response_code = int(result.group(1))
response_body += "answer with response code %s\n" % response_code
elif self.path == "/":
response = "I'm %s\n" % os.environ['HOSTNAME']
self.wfile.write(response.encode())
response_body += "I'm %s\n" % os.environ['HOSTNAME']
else:
self.wfile.write("No route for this path!\n".encode())
response_body += "No route for this path!\n"
response_code = 404

self.send_response(response_code)
self.send_header("Content-Type", "text/plain")
self.end_headers()

if (len(response_body)):
self.wfile.write(response_body.encode())

if __name__ == '__main__':
PORT = int(sys.argv[1])
Expand Down

0 comments on commit c1ae913

Please sign in to comment.