def mocked_requests_get(*args, **kwargs): url = args[0] print("GET for URL: {}".format(url)) if url == DMR_URL: return MockResponse(None, 200, read_file("data/dmr_v3.xml")) elif url == IRCC_URL: return MockResponse(None, 200, read_file("data/ircc.xml")) elif url == IRCC_URL_MISSING_INFO: return MockResponse(None, 200, read_file("data/ircc_missing_info.xml")) elif url == IRCC_URL_NO_SCHEMA: return MockResponse(None, 200, read_file("data/ircc_no_schema.xml")) elif url == ACTION_LIST_URL: return MockResponse(None, 200, read_file("data/actionlist.xml")) elif url == ACTION_LIST_URL_2: return MockResponse(None, 200, read_file("data/actionlist_no_url.xml")) elif url == SYSTEM_INFORMATION_URL: return MockResponse(None, 200, read_file("data/getSysteminformation.xml")) elif url == GET_REMOTE_COMMAND_LIST_URL: return MockResponse(None, 200, read_file("data/getRemoteCommandList.xml")) elif url == APP_LIST_URL or url == APP_LIST_URL_V4: return MockResponse(None, 200, read_file("data/appsList.xml")) elif url == REGISTRATION_URL_LEGACY: return MockResponse({}, 200) elif url == REGISTRATION_URL_V3_FAIL_401: MockResponse(None, 401).raise_for_status() elif url == GET_REMOTE_CONTROLLER_INFO_URL: return MockResponse(None, 200) elif url.startswith(REQUESTS_ERROR): raise RequestException() else: raise ValueError("Unknown url requested: {}".format(url)) return MockResponse(None, 404)
def mocked_requests_post(*args, **kwargs): url = args[0] print("POST for URL: {}".format(url)) if not url: raise URLRequired() elif url == REGISTRATION_URL_V4: return MockResponse({}, 200) elif url == REGISTRATION_URL_V4_FAIL: return MockResponse({"error": 402}, 200) elif url == REGISTRATION_URL_V4_FAIL_401: MockResponse(None, 401).raise_for_status() elif url == SOAP_URL: return MockResponse({}, 200, "data") elif url == urljoin(BASE_URL, 'system'): result = MockResponseJson({"status": "on"}) return MockResponse({"result": [result]}, 200) elif APP_START_URL_LEGACY in url: return MockResponse(None, 200) elif APP_START_URL in url: return MockResponse(None, 200) elif url == AV_TRANSPORT_URL: return MockResponse( None, 200, read_file('data/playing_status_legacy_playing.xml')) elif url == AV_TRANSPORT_URL_NO_MEDIA: return MockResponse( None, 200, read_file('data/playing_status_legacy_no_media.xml')) elif url == COMMAND_LIST_V4: json_data = jsonpickle.decode(read_file('data/commandList.json')) return MockResponse(json_data, 200, "") elif url == SYSTEM_INFORMATION_URL_V4: json_data = jsonpickle.decode(read_file('data/systemInformation.json')) return MockResponse(json_data, 200, "") elif url.startswith(REQUESTS_ERROR): raise RequestException else: raise ValueError("Unknown url requested: {}".format(url))
def create_device(): """Create a new device instance""" sonyapilib.device.TIMEOUT = 0.1 device = SonyDevice("test", "test") device.api_version = 3 device.cookies = jsonpickle.decode(read_file("data/cookies.json")) return device
def test_parse_dmr_v4(self, mock_get): content = read_file("data/dmr_v4.xml") device = self.create_device() device._parse_dmr(content) self.verify_device_dmr(device) self.assertGreater(device.api_version, 3) self.assertEqual(device.actions["register"].url, REGISTRATION_URL_V4) self.assertEqual(device.actions["register"].mode, 4) self.assertEqual(device.actions["getRemoteCommandList"].url, 'http://192.168.170.23/sony/system')
def test_parse_dmr_v3(self, mock_get): content = read_file("data/dmr_v3.xml") device = self.create_device() device._parse_dmr(content) self.verify_device_dmr(device) self.assertLess(device.api_version, 4)