-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpserversimulator.hy
32 lines (24 loc) · 1.06 KB
/
httpserversimulator.hy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(import [http.server [HTTPServer SimpleHTTPRequestHandler HTTPStatus]])
(import ssl collections threading)
; openssl req -new -x509 -keyout testpemfile.pem -out yourpemfile.pem -days 365 -nodes
(defclass HttpServerSimulator [object]
(defn --init-- [self]
(setv
self.httpd (HTTPServer (, "localhost" 5555) HttpRequestHandlerSimulator)
self.httpd.socket (ssl.wrap-socket self.httpd.socket :server-side True :certfile "tests/testdata/testpemfile.pem")
self.httpd.testdata (collections.deque)
self.httpserver (threading.Thread :target self.httpd.serve_forever)))
(defn feed-data [self str]
(self.httpd.testdata.append str))
(defn start [self]
(self.httpserver.start))
(defn stop [self]
(self.httpd.shutdown)
(self.httpserver.join)))
(defclass HttpRequestHandlerSimulator [SimpleHTTPRequestHandler]
(defn do_GET [self]
(self.send_response 200)
(self.send_header "Content-type" "application/json")
(self.end_headers)
(setv response (self.server.testdata.popleft))
(self.wfile.write (bytes response "utf8"))))