def test_gen_poll_url_xml_no_xmltodict(monkeypatch): gpurl = generic_poll_text.GenPollUrl(json=False, xml=True, parse=lambda x: x, url="testing") monkeypatch.setattr(generic_poll_text, "Request", MockRequest) monkeypatch.setattr(generic_poll_text, "urlopen", Mockurlopen) generic_poll_text.Request.return_value = b"OK" with pytest.raises(Exception): gpurl.poll()
def test_gen_poll_url_json(monkeypatch): gpurl = generic_poll_text.GenPollUrl(parse=lambda x: x, data=[1, 2, 3], url="testing") monkeypatch.setattr(generic_poll_text, "Request", MockRequest) monkeypatch.setattr(generic_poll_text, "urlopen", Mockurlopen) generic_poll_text.Request.return_value = b'{"test": "OK"}' assert gpurl.poll()["test"] == "OK"
def test_gen_poll_url_text(monkeypatch): gpurl = generic_poll_text.GenPollUrl(json=False, parse=lambda x: x, url="testing") monkeypatch.setattr(generic_poll_text, "Request", MockRequest) monkeypatch.setattr(generic_poll_text, "urlopen", Mockurlopen) generic_poll_text.Request.return_value = b"OK" assert gpurl.poll() == "OK"
def test_gen_poll_url_headers_and_json(): gpurl = generic_poll_text.GenPollUrl( headers={"fake-header": "fake-value"}, data={"argument": "data value"}, user_agent="qtile test" ) assert gpurl.headers["User-agent"] == "qtile test" assert gpurl.headers["fake-header"] == "fake-value" assert gpurl.headers["Content-Type"] == "application/json" assert gpurl.data.decode() == '{"argument": "data value"}'
def test_gen_poll_url_xml_has_xmltodict(monkeypatch): # injected fake xmltodict module but we have to reload the widget module # as the ImportError test is only run once when the module is loaded. monkeypatch.setitem(sys.modules, "xmltodict", Mockxml("xmltodict")) reload(generic_poll_text) gpurl = generic_poll_text.GenPollUrl(json=False, xml=True, parse=lambda x: x, url="testing") monkeypatch.setattr(generic_poll_text, "Request", MockRequest) monkeypatch.setattr(generic_poll_text, "urlopen", Mockurlopen) generic_poll_text.Request.return_value = b"OK" print(gpurl.poll()) assert gpurl.poll()["test"] == "OK"
def test_gen_poll_url_no_json(): gpurl = generic_poll_text.GenPollUrl(json=False) assert "Content-Type" not in gpurl.headers
def test_gen_poll_url_not_configured(): gpurl = generic_poll_text.GenPollUrl() assert gpurl.poll() == "Invalid config"