Bug 1488468 - Move HTTP POST request to wait_for_ping in TestPingServer; r=davehunt
authorRaphael Pierzina <rpierzina@mozilla.com>
Tue, 16 Oct 2018 10:42:06 +0000
changeset 489794 51b87be56bcae1931f2a8b76b2494c945c3b2db8
parent 489793 bf88501c1f8af00508f0a13029329ea2f005f51c
child 489795 ab9dfffff004480f216246435f2de37f801a636c
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersdavehunt
bugs1488468
milestone64.0a1
Bug 1488468 - Move HTTP POST request to wait_for_ping in TestPingServer; r=davehunt Depends on D8531 Differential Revision: https://phabricator.services.mozilla.com/D8532
toolkit/components/telemetry/tests/marionette/tests/unit/test_ping_server_received_ping.py
--- a/toolkit/components/telemetry/tests/marionette/tests/unit/test_ping_server_received_ping.py
+++ b/toolkit/components/telemetry/tests/marionette/tests/unit/test_ping_server_received_ping.py
@@ -1,21 +1,38 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/
 
 import requests
-import simplejson as json
 
 from telemetry_harness.testcase import TelemetryTestCase
 
 
 class TestPingServer(TelemetryTestCase):
+    def test_ping_server_received_ping(self):
+        ping_type = "server-test-ping"
+        ping_reason = "unit-test"
 
-    def test_ping_server_received_ping(self):
+        def action_func():
+            """Perform a POST request to the ping server."""
+            data = {"type": ping_type, "reason": ping_reason}
+            headers = {"Content-type": "application/json", "Accept": "text/plain"}
+
+            response = requests.post(self.ping_server_url, json=data, headers=headers)
 
-        data = {'type': 'server-test-ping', 'reason': 'unit-test'}
-        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
-        json_req = requests.post(self.ping_server_url, data=json.dumps(data), headers=headers)
-        ping = self.wait_for_ping(None, lambda p: p['type'] == 'server-test-ping')
-        assert ping is not None
-        assert json_req.status_code == 200
-        assert data['type'] == ping['type'] and data['reason'] == ping['reason']
+            self.assertEqual(
+                response.status_code,
+                200,
+                msg="Error sending POST request to ping server: {response.text}".format(
+                    response=response
+                ),
+            )
+            return response
+
+        def ping_filter_func(ping):
+            return ping["type"] == ping_type
+
+        ping = self.wait_for_ping(action_func, ping_filter_func)
+
+        self.assertIsNotNone(ping)
+        self.assertEqual(ping["type"], ping_type)
+        self.assertEqual(ping["reason"], ping_reason)