Skip to content

Commit

Permalink
local test automation
Browse files Browse the repository at this point in the history
  • Loading branch information
jurassix committed Feb 8, 2024
1 parent 5a34026 commit 9af46a9
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
68 changes: 68 additions & 0 deletions simple_http_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
import gzip
from io import BytesIO

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

def _set_response(self, content_type='text/html'):
self.send_response(200)
self.send_header('Content-type', content_type)
self.end_headers()

def _get_request_body(self):
content_length = int(self.headers['Content-Length']) # Get the size of the data
request_data = self.rfile.read(content_length) # Get the data itself

# Check if the data is gzipped and decompress if necessary
if self.headers.get('Content-Encoding') == 'gzip':
request_data = gzip.decompress(request_data)

return request_data.decode('utf-8')

def do_GET(self):
logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
self._set_response()
self.wfile.write(f"GET request for {self.path}".encode('utf-8'))

def do_POST(self):
post_data = self._get_request_body()
logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
str(self.path), str(self.headers), post_data)

self._set_response()
self.wfile.write(f"POST request for {self.path}".encode('utf-8'))

def do_PUT(self):
put_data = self._get_request_body()
logging.info("PUT request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
str(self.path), str(self.headers), put_data)

self._set_response()
self.wfile.write(f"PUT request for {self.path}".encode('utf-8'))

def do_HEAD(self):
logging.info("HEAD request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
self._set_response()
# Traditionally, HEAD requests do not have a body
# Removed the line writing data to self.wfile since HEAD response should not include a body

def run(server_class=HTTPServer, handler_class=SimpleHTTPRequestHandler, port=8080):
logging.basicConfig(level=logging.INFO)
server_address = ('', port)
httpd = server_class(server_address, handler_class)
logging.info('Starting httpd...\n')
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
logging.info('Stopping httpd...\n')

if __name__ == '__main__':
from sys import argv

if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
37 changes: 37 additions & 0 deletions tmux_local_relay.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/zsh

# Capture the current directory
current_dir=$(pwd)

# Start a new tmux session but don't attach to it yet
tmux new-session -d -s relay -c "$current_dir"

# Split the window into three panes, making sure to set the directory
tmux split-window -h -c "$current_dir"
tmux split-window -v -c "$current_dir"

# Optionally, you can balance the splits
tmux select-layout even-horizontal

# Pane indexes start at 0 by default for the first window, incremented thereafter

# Start the target server to log relayed requests in the third pane
tmux send-keys -t relay:0.2 "cd \"$current_dir\"" C-m
tmux send-keys -t relay:0.2 "clear" C-m
tmux send-keys -t relay:0.2 '/Users/clint/src/fsdev/tools/python/bin/python simple_http_server.py 8085' C-m

# Make and run the local relay server in the second pane
tmux send-keys -t relay:0.1 "cd \"$current_dir\"" C-m
tmux send-keys -t relay:0.1 "clear" C-m
tmux send-keys -t relay:0.1 'make' C-m
tmux send-keys -t relay:0.1 'TRAFFIC_RELAY_TARGET=http://localhost:8085 TRAFFIC_EXCLUDE_BODY_CONTENT=Args ./dist/relay' C-m

# Use cURL to send a POST request to the relay server in the first pane
tmux send-keys -t relay:0.0 "cd \"$current_dir\"" C-m
tmux send-keys -t relay:0.0 "clear" C-m
# print the cURL command to the pane but don't run it
tmux send-keys -t relay:0.0 'curl POST -d "{"When":1000,"Seq":1,"Evts":[{"When":1000,"Kind":2,"Args":[]}]}" http://localhost:8990/rec/bundle'
tmux select-pane -t relay:0.0

# Finally, attach to the session
tmux attach-session -t relay

0 comments on commit 9af46a9

Please sign in to comment.