forked from wiremock/python-wiremock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
121 lines (103 loc) · 3.43 KB
/
server.py
File metadata and controls
121 lines (103 loc) · 3.43 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
"""WireMock Server Management."""
import atexit
import socket
import time
from subprocess import PIPE, STDOUT, Popen
import requests
from importlib.resources import files
from wiremock.server.exceptions import (
WireMockServerAlreadyStartedError,
WireMockServerNotStartedError,
)
class WireMockServer(object):
DEFAULT_JAVA = "java" # Assume java in PATH
DEFAULT_JAR = files("wiremock") / "server" / "wiremock-standalone-2.35.1.jar"
def __init__(
self,
java_path=DEFAULT_JAVA,
jar_path=DEFAULT_JAR,
port=None,
max_attempts=10,
root_dir=None,
):
self.java_path = java_path
self.jar_path = jar_path
self.port = port or self._get_free_port()
self.__subprocess = None
self.__running = False
self.max_attempts = max_attempts
self.root_dir = root_dir
def __enter__(self):
self.start()
return self
def __exit__(self, type, value, traceback):
self.stop()
@property
def is_running(self):
return self.__running
def start(self):
if self.is_running:
raise WireMockServerAlreadyStartedError(
"WireMockServer already started on port {}".format(self.port)
)
cmd = [
self.java_path,
"-jar",
self.jar_path,
"--port",
str(self.port),
"--local-response-templating",
]
if self.root_dir is not None:
cmd.append("--root-dir")
cmd.append(str(self.root_dir))
try:
self.__subprocess = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
except OSError as e:
raise WireMockServerNotStartedError(str(e)) # Problem with Java
time.sleep(0.1)
if self.__subprocess.poll() is not None:
# Process complete - server not started
raise WireMockServerNotStartedError(
"\n".join(
[
"returncode: {}".format(self.__subprocess.returncode),
"stdout:",
str(self.__subprocess.stdout.read()),
]
)
)
# Call the /__admin endpoint as a check for running state
attempts = 0
success = False
while attempts < self.max_attempts:
try:
attempts += 1
resp = requests.get("http://localhost:{}/__admin".format(self.port))
if resp.status_code == 200:
success = True
break
except requests.exceptions.ConnectionError:
pass
time.sleep(0.25)
if not success:
raise WireMockServerNotStartedError(
"unable to get a successful GET http://localhost:{}/__admin response".format(
self.port
)
)
atexit.register(self.stop, raise_on_error=False)
self.__running = True
def stop(self, raise_on_error=True):
try:
self.__subprocess.kill()
except AttributeError:
if raise_on_error:
raise WireMockServerNotStartedError()
def _get_free_port(self):
s = socket.socket(socket.AF_INET, type=socket.SOCK_STREAM)
s.bind(("localhost", 0))
address, port = s.getsockname()
s.close()
return port