Bug 1284268 - Add a local ping server; r=maja_zf
authorAndré Reinald <areinald@mozilla.com>
Mon, 01 Aug 2016 21:37:06 +0200
changeset 400405 bb7b33ca26fa10d442577f62909c27890de8d752
parent 400404 33fcf83eefe43e02f560fdf9c182b88c87343cc1
child 400406 e4437c4da937e08593dc896ce7cf39dc48e1e2ad
push id26138
push usergszorc@mozilla.com
push dateSat, 13 Aug 2016 03:04:36 +0000
reviewersmaja_zf
bugs1284268
milestone51.0a1
Bug 1284268 - Add a local ping server; r=maja_zf MozReview-Commit-ID: BprnSNceit9
testing/marionette/harness/session/__init__.py
testing/marionette/harness/session/runner/__init__.py
testing/marionette/harness/session/runner/ping_server.py
testing/marionette/harness/session/session_test.py
--- a/testing/marionette/harness/session/__init__.py
+++ b/testing/marionette/harness/session/__init__.py
@@ -22,9 +22,10 @@ from .session_test import (
 )
 
 from .runner import (
     BaseSessionArguments,
     BaseSessionTestRunner,
     SessionTest,
     SessionTestResult,
     SessionTextTestRunner,
+    PingServer,
 )
--- a/testing/marionette/harness/session/runner/__init__.py
+++ b/testing/marionette/harness/session/runner/__init__.py
@@ -4,8 +4,12 @@
 
 from .base import (
     BaseSessionArguments,
     BaseSessionTestRunner,
     SessionTest,
     SessionTestResult,
     SessionTextTestRunner,
 )
+
+from .ping_server import (
+    PingServer,
+)
new file mode 100644
--- /dev/null
+++ b/testing/marionette/harness/session/runner/ping_server.py
@@ -0,0 +1,63 @@
+import BaseHTTPServer
+import threading
+import zlib
+import json
+
+class PingHandler(BaseHTTPServer.BaseHTTPRequestHandler):
+
+    def do_HEAD(s):
+        s.send_response(200)
+        s.send_header("Content-type", "text/html")
+        s.end_headers()
+
+    def do_GET(s):
+        self.do_HEAD(self, s)
+        s.wfile.write("<html>")
+        s.wfile.write("  <head><title>Success</title></head>")
+        s.wfile.write("  <body>")
+        s.wfile.write("    <p>The server is working correctly. Firefox should send a POST request on port %d</p>" % self.server.port)
+        s.wfile.write("  </body>")
+        s.wfile.write("</html>")
+
+    def do_POST(s):
+        length = int(s.headers["Content-Length"])
+        plainData = s.rfile.read(length)
+        if s.headers.get("Content-Encoding") == "gzip":
+            plainData = zlib.decompress(plainData, zlib.MAX_WBITS | 16)
+        jsonData = json.loads(plainData)
+        self.server.appendPing(jsonData)
+        self.do_HEAD(self, s)
+
+class PingServer(threading.Thread):
+
+    def __init__(self):
+        threading.Thread.__init__(self)
+        self._httpd = BaseHTTPServer.HTTPServer(('', 0), PingHandler)
+        self._receivedPings = dict()
+
+    def run(self):
+        self._httpd.serve_forever()
+
+    def stop(self):
+        self._httpd.shutdown()
+
+    def appendPing(self, jsonData):
+        self._receivedPings[jsonData["id"]] = jsonData
+
+    def clearPings(self):
+        self._receivedPings.clear()
+
+    @property
+    def pings(self):
+        return self._receivedPings
+
+    def ping(self, id):
+        return self._receivedPings.get(id)
+
+    @property
+    def name(self):
+        return self._httpd.server_name
+
+    @property
+    def port(self):
+        return self._httpd.server_port
--- a/testing/marionette/harness/session/session_test.py
+++ b/testing/marionette/harness/session/session_test.py
@@ -24,16 +24,17 @@ from marionette_driver.errors import (
         NoSuchFrameException, InvalidElementStateException, NoAlertPresentException,
         InvalidCookieDomainException, UnableToSetCookieException, InvalidSelectorException,
         MoveTargetOutOfBoundsException
         )
 from marionette_driver.marionette import Marionette
 from marionette_driver.wait import Wait
 from marionette_driver.expected import element_present, element_not_present
 from mozlog import get_default_logger
+from runner import PingServer
 
 from marionette.marionette_test import (
         SkipTest,
         _ExpectedFailure,
         _UnexpectedSuccess,
         skip,
         expectedFailure,
         parameterized,
@@ -230,28 +231,31 @@ class CommonTestCase(unittest.TestCase):
         return self.test_name
 
     def setUp(self):
         # Convert the marionette weakref to an object, just for the
         # duration of the test; this is deleted in tearDown() to prevent
         # a persistent circular reference which in turn would prevent
         # proper garbage collection.
         self.start_time = time.time()
+        self.pingServer = PingServer()
+        self.pingServer.start()
         self.marionette = Marionette(bin=self.binary, profile=self.profile)
         if self.marionette.session is None:
             self.marionette.start_session()
         if self.marionette.timeout is not None:
             self.marionette.timeouts(self.marionette.TIMEOUT_SEARCH, self.marionette.timeout)
             self.marionette.timeouts(self.marionette.TIMEOUT_SCRIPT, self.marionette.timeout)
             self.marionette.timeouts(self.marionette.TIMEOUT_PAGE, self.marionette.timeout)
         else:
             self.marionette.timeouts(self.marionette.TIMEOUT_PAGE, 30000)
 
     def tearDown(self):
         self.marionette.cleanup()
+        self.pingServer.stop()
 
     def cleanTest(self):
         self._deleteSession()
 
     def _deleteSession(self):
         if hasattr(self, 'start_time'):
             self.duration = time.time() - self.start_time
         if hasattr(self.marionette, 'session'):