(self)
| 221 | |
| 222 | class TestPushGateway(unittest.TestCase): |
| 223 | def setUp(self): |
| 224 | redirect_flag = 'testFlag' |
| 225 | self.redirect_flag = redirect_flag # preserve a copy for downstream test assertions |
| 226 | self.registry = CollectorRegistry() |
| 227 | self.counter = Gauge('g', 'help', registry=self.registry) |
| 228 | self.requests = requests = [] |
| 229 | |
| 230 | class TestHandler(BaseHTTPRequestHandler): |
| 231 | def do_PUT(self): |
| 232 | if 'with_basic_auth' in self.requestline and self.headers['authorization'] != 'Basic Zm9vOmJhcg==': |
| 233 | self.send_response(401) |
| 234 | elif 'redirect' in self.requestline and redirect_flag not in self.requestline: |
| 235 | # checks for an initial test request with 'redirect' but without the redirect_flag, |
| 236 | # and simulates a redirect to a url with the redirect_flag (which will produce a 201) |
| 237 | self.send_response(301) |
| 238 | self.send_header('Location', getattr(self, 'redirect_address', None)) |
| 239 | else: |
| 240 | self.send_response(201) |
| 241 | length = int(self.headers['content-length']) |
| 242 | requests.append((self, self.rfile.read(length))) |
| 243 | self.end_headers() |
| 244 | |
| 245 | do_POST = do_PUT |
| 246 | do_DELETE = do_PUT |
| 247 | |
| 248 | # set up a separate server to serve a fake redirected request. |
| 249 | # the redirected URL will have `redirect_flag` added to it, |
| 250 | # which will cause the request handler to return 201. |
| 251 | httpd_redirect = HTTPServer(('localhost', 0), TestHandler) |
| 252 | self.redirect_address = TestHandler.redirect_address = \ |
| 253 | f'http://localhost:{httpd_redirect.server_address[1]}/{redirect_flag}' |
| 254 | |
| 255 | class TestRedirectServer(threading.Thread): |
| 256 | def run(self): |
| 257 | httpd_redirect.handle_request() |
| 258 | |
| 259 | self.redirect_server = TestRedirectServer() |
| 260 | self.redirect_server.daemon = True |
| 261 | self.redirect_server.start() |
| 262 | |
| 263 | # set up the normal server to serve the example requests across test cases. |
| 264 | httpd = HTTPServer(('localhost', 0), TestHandler) |
| 265 | self.address = f'http://localhost:{httpd.server_address[1]}' |
| 266 | |
| 267 | class TestServer(threading.Thread): |
| 268 | def run(self): |
| 269 | httpd.handle_request() |
| 270 | |
| 271 | self.server = TestServer() |
| 272 | self.server.daemon = True |
| 273 | self.server.start() |
| 274 | |
| 275 | |
| 276 | def test_push(self): |
nothing calls this directly
no test coverage detected