# Path: tapes/reporting/http.py
# class HTTPReporter(Reporter):
#     """Exposes metrics via HTTP.
# 
#     For web applications, you should almost certainly just use your existing framework's capabilities. This is for
#     applications that don't have HTTP easily available.
#     """
#     def __init__(self, port, registry=None):
#         """
#         :param port: Port to listen on
#         :param registry: The registry to report from, defaults to the global one
#         """
#         super(HTTPReporter, self).__init__(registry)
#         self.port = port
#         self.thread = None
#         self.httpd = None
# 
#     def start(self):
#         class _RequestHandler(BaseHTTPRequestHandler):
#             def do_GET(inner_self):
#                 inner_self.send_response(200)
#                 response_string = json.dumps(self.registry.get_stats())
#                 inner_self.send_header('Content-Type', 'application/json')
#                 inner_self.send_header('Content-Length', len(response_string))
#                 inner_self.end_headers()
#                 inner_self.wfile.write(response_string.encode('utf-8'))
# 
#         server_address = '', self.port
# 
#         self.httpd = HTTPServer(server_address, _RequestHandler)
#         self.thread = Thread(target=self.httpd.serve_forever)
#         self.thread.start()
# 
#     def stop(self):
#         self.httpd.shutdown()
#         self.thread.join()
# 
# Path: tests/local/base.py
# class StatsTest(unittest.TestCase):
#     def setUp(self):
#         self.registry = Registry()


import requests
from tornado.testing import bind_unused_port
from tapes.reporting.http import HTTPReporter
from tests.local.base import StatsTest
from __future__ import print_function





class HTTPReportingTestCase(StatsTest):
    def test_http_reporter_serves_stats_as_json(self):
        counter = self.registry.counter('some.path')

        counter.increment(42)

        sock, http_port = bind_unused_port()
        sock.close()

        reporter = HTTPReporter(http_port, self.registry)
        reporter.start()

        response = requests.get('http://localhost:%d/' % http_port)

# Path: tapes/reporting/http.py
# class HTTPReporter(Reporter):
#     """Exposes metrics via HTTP.
# 
#     For web applications, you should almost certainly just use your existing framework's capabilities. This is for
#     applications that don't have HTTP easily available.
#     """
#     def __init__(self, port, registry=None):
#         """
#         :param port: Port to listen on
#         :param registry: The registry to report from, defaults to the global one
#         """
#         super(HTTPReporter, self).__init__(registry)
#         self.port = port
#         self.thread = None
#         self.httpd = None
# 
#     def start(self):
#         class _RequestHandler(BaseHTTPRequestHandler):
#             def do_GET(inner_self):
#                 inner_self.send_response(200)
#                 response_string = json.dumps(self.registry.get_stats())
#                 inner_self.send_header('Content-Type', 'application/json')
#                 inner_self.send_header('Content-Length', len(response_string))
#                 inner_self.end_headers()
#                 inner_self.wfile.write(response_string.encode('utf-8'))
# 
#         server_address = '', self.port
# 
#         self.httpd = HTTPServer(server_address, _RequestHandler)
#         self.thread = Thread(target=self.httpd.serve_forever)
#         self.thread.start()
# 
#     def stop(self):
#         self.httpd.shutdown()
#         self.thread.join()
# 
# Path: tests/local/base.py
# class StatsTest(unittest.TestCase):
#     def setUp(self):
#         self.registry = Registry()


# Path: tests/reporting/test_http_reporting.py
import requests
from tornado.testing import bind_unused_port
from tapes.reporting.http import HTTPReporter
from tests.local.base import StatsTest

from __future__ import print_function





class HTTPReportingTestCase(StatsTest):
    def test_http_reporter_serves_stats_as_json(self):
        counter = self.registry.counter('some.path')

        counter.increment(42)

        sock, http_port = bind_unused_port()
        sock.close()

        reporter = HTTPReporter(http_port, self.registry)
        reporter.start()

        response = requests.get('http://localhost:%d/' % http_port)

        assert response.status_code == 200