File: test_client.py

package info (click to toggle)
async-upnp-client 0.46.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,080 kB
  • sloc: python: 11,872; xml: 2,826; sh: 32; makefile: 6
file content (791 lines) | stat: -rw-r--r-- 32,101 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
# -*- coding: utf-8 -*-
"""Unit tests for client_factory and client modules."""

from datetime import datetime, timedelta, timezone
from typing import MutableMapping

import defusedxml.ElementTree as DET
import pytest

from async_upnp_client.client import UpnpStateVariable
from async_upnp_client.client_factory import UpnpFactory
from async_upnp_client.const import HttpResponse
from async_upnp_client.exceptions import (
    UpnpActionError,
    UpnpActionErrorCode,
    UpnpActionResponseError,
    UpnpError,
    UpnpResponseError,
    UpnpValueError,
    UpnpXmlContentError,
    UpnpXmlParseError,
)

from .conftest import RESPONSE_MAP, UpnpTestRequester, read_file


class TestUpnpStateVariable:
    """Tests for UpnpStateVariable."""

    @pytest.mark.asyncio
    async def test_init(self) -> None:
        """Test initialization of a UpnpDevice."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        assert device
        assert device.device_type == "urn:schemas-upnp-org:device:MediaRenderer:1"

        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        assert service

        service_by_id = device.service_id("urn:upnp-org:serviceId:RenderingControl")
        assert service_by_id == service

        state_var = service.state_variable("Volume")
        assert state_var

        action = service.action("GetVolume")
        assert action

        argument = action.argument("InstanceID")
        assert argument

    @pytest.mark.asyncio
    async def test_init_embedded_device(self) -> None:
        """Test initialization of a embedded UpnpDevice."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://igd:1234/device.xml")
        assert device
        assert (
            device.device_type == "urn:schemas-upnp-org:device:InternetGatewayDevice:1"
        )

        embedded_device = device.embedded_devices[
            "urn:schemas-upnp-org:device:WANDevice:1"
        ]
        assert embedded_device
        assert embedded_device.device_type == "urn:schemas-upnp-org:device:WANDevice:1"
        assert embedded_device.parent_device == device

    @pytest.mark.asyncio
    async def test_init_xml(self) -> None:
        """Test XML is stored on every part of the UpnpDevice."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        assert device.xml is not None

        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        assert service.xml is not None

        state_var = service.state_variable("Volume")
        assert state_var.xml is not None

        action = service.action("GetVolume")
        assert action.xml is not None

        argument = action.argument("InstanceID")
        assert argument is not None
        assert argument.xml is not None

    @pytest.mark.asyncio
    async def test_init_bad_xml(self) -> None:
        """Test missing device element in device description."""
        responses = dict(RESPONSE_MAP)
        responses[("GET", "http://dlna_dmr:1234/device.xml")] = HttpResponse(
            200,
            {},
            read_file("dlna/dmr/device_bad_namespace.xml"),
        )
        requester = UpnpTestRequester(responses)
        factory = UpnpFactory(requester)
        with pytest.raises(UpnpXmlContentError):
            await factory.async_create_device("http://dlna_dmr:1234/device.xml")

    @pytest.mark.asyncio
    async def test_empty_descriptor(self) -> None:
        """Test device with an empty descriptor file called in description.xml."""
        responses = dict(RESPONSE_MAP)
        responses[("GET", "http://dlna_dmr:1234/device.xml")] = HttpResponse(
            200,
            {},
            read_file("dlna/dmr/device_with_empty_descriptor.xml"),
        )
        requester = UpnpTestRequester(responses)
        factory = UpnpFactory(requester)
        with pytest.raises(UpnpXmlParseError):
            await factory.async_create_device("http://dlna_dmr:1234/device.xml")

    @pytest.mark.asyncio
    async def test_empty_descriptor_non_strict(self) -> None:
        """Test device with an empty descriptor file called in description.xml."""
        responses = dict(RESPONSE_MAP)
        responses[("GET", "http://dlna_dmr:1234/device.xml")] = HttpResponse(
            200,
            {},
            read_file("dlna/dmr/device_with_empty_descriptor.xml"),
        )
        requester = UpnpTestRequester(responses)
        factory = UpnpFactory(requester, non_strict=True)
        await factory.async_create_device("http://dlna_dmr:1234/device.xml")

    @pytest.mark.asyncio
    async def test_set_value_volume(self) -> None:
        """Test calling parsing/reading values from UpnpStateVariable."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        state_var = service.state_variable("Volume")

        state_var.value = 10
        assert state_var.value == 10
        assert state_var.upnp_value == "10"

        state_var.upnp_value = "20"
        assert state_var.value == 20
        assert state_var.upnp_value == "20"

    @pytest.mark.asyncio
    async def test_set_value_mute(self) -> None:
        """Test setting a boolean value."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        state_var = service.state_variable("Mute")

        state_var.value = True
        assert state_var.value is True
        assert state_var.upnp_value == "1"

        state_var.value = False
        assert state_var.value is False
        assert state_var.upnp_value == "0"

        state_var.upnp_value = "1"
        assert state_var.value is True
        assert state_var.upnp_value == "1"

        state_var.upnp_value = "0"
        assert state_var.value is False
        assert state_var.upnp_value == "0"

    @pytest.mark.asyncio
    async def test_value_min_max(self) -> None:
        """Test min/max restrictions."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        state_var = service.state_variable("Volume")

        assert state_var.min_value == 0
        assert state_var.max_value == 100
        assert state_var.step_value == 1

        state_var.value = 10
        assert state_var.value == 10

        try:
            state_var.value = -10
            assert False
        except UpnpValueError:
            pass

        try:
            state_var.value = 110
            assert False
        except UpnpValueError:
            pass

    @pytest.mark.asyncio
    async def test_value_min_max_validation_disable(self) -> None:
        """Test if min/max validations can be disabled."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester, non_strict=True)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        state_var = service.state_variable("Volume")

        # min/max/step are set
        assert state_var.min_value == 0
        assert state_var.max_value == 100
        assert state_var.step_value == 1

        # min/max are not validated
        state_var.value = -10
        assert state_var.value == -10

        state_var.value = 110
        assert state_var.value == 110

    @pytest.mark.asyncio
    async def test_value_allowed_value(self) -> None:
        """Test handling allowed values."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        state_var = service.state_variable("A_ARG_TYPE_Channel")

        assert state_var.allowed_values == {"Master"}
        assert state_var.normalized_allowed_values == {"master"}

        # should be ok
        state_var.value = "Master"
        assert state_var.value == "Master"

        try:
            state_var.value = "Left"
            assert False
        except UpnpValueError:
            pass

    @pytest.mark.asyncio
    async def test_value_upnp_value_error(self) -> None:
        """Test handling invalid values in response."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester, non_strict=True)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        state_var = service.state_variable("Volume")

        # should be ok
        state_var.upnp_value = "50"
        assert state_var.value == 50

        # should set UpnpStateVariable.UPNP_VALUE_ERROR
        state_var.upnp_value = "abc"
        assert state_var.value is None
        assert state_var.value_unchecked is UpnpStateVariable.UPNP_VALUE_ERROR

    @pytest.mark.asyncio
    async def test_value_date_time(self) -> None:
        """Test parsing of datetime."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester, non_strict=True)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        state_var = service.state_variable("SV1")

        # should be ok
        state_var.upnp_value = "1985-04-12T10:15:30"
        assert state_var.value == datetime(1985, 4, 12, 10, 15, 30)

    @pytest.mark.asyncio
    async def test_value_date_time_tz(self) -> None:
        """Test parsing of date_time with a timezone."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester, non_strict=True)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        state_var = service.state_variable("SV2")
        assert state_var is not None

        # should be ok
        state_var.upnp_value = "1985-04-12T10:15:30+0400"
        assert state_var.value == datetime(
            1985, 4, 12, 10, 15, 30, tzinfo=timezone(timedelta(hours=4))
        )
        assert state_var.value.tzinfo is not None

    @pytest.mark.asyncio
    async def test_send_events(self) -> None:
        """Test if send_events is properly handled."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")

        state_var = service.state_variable("A_ARG_TYPE_InstanceID")  # old style
        assert state_var.send_events is False

        state_var = service.state_variable("A_ARG_TYPE_Channel")  # new style
        assert state_var.send_events is False

        state_var = service.state_variable("Volume")  # broken/none given
        assert state_var.send_events is False

        state_var = service.state_variable("LastChange")
        assert state_var.send_events is True

    @pytest.mark.asyncio
    async def test_big_ints(self) -> None:
        """Test state variable types i8 and ui8."""
        responses = dict(RESPONSE_MAP)
        responses[("GET", "http://dlna_dms:1234/ContentDirectory_1.xml")] = (
            HttpResponse(
                200,
                {},
                read_file("scpd_i8.xml"),
            )
        )
        requester = UpnpTestRequester(responses)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dms:1234/device.xml")
        assert device is not None


class TestUpnpAction:
    """Tests for UpnpAction."""

    @pytest.mark.asyncio
    async def test_init(self) -> None:
        """Test Initializing a UpnpAction."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        action = service.action("GetVolume")

        assert action
        assert action.name == "GetVolume"

    @pytest.mark.asyncio
    async def test_valid_arguments(self) -> None:
        """Test validating arguments of an action."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        action = service.action("SetVolume")

        # all ok
        action.validate_arguments(InstanceID=0, Channel="Master", DesiredVolume=10)

        # invalid type for InstanceID
        try:
            action.validate_arguments(
                InstanceID="0", Channel="Master", DesiredVolume=10
            )
            assert False
        except UpnpValueError:
            pass

        # missing DesiredVolume
        try:
            action.validate_arguments(InstanceID="0", Channel="Master")
            assert False
        except UpnpValueError:
            pass

    @pytest.mark.asyncio
    async def test_format_request(self) -> None:
        """Test the request an action sends."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        action = service.action("SetVolume")

        service_type = "urn:schemas-upnp-org:service:RenderingControl:1"
        request = action.create_request(
            InstanceID=0, Channel="Master", DesiredVolume=10
        )

        root = DET.fromstring(request.body)
        namespace = {"rc_service": service_type}
        assert root.find(".//rc_service:SetVolume", namespace) is not None
        assert root.find(".//DesiredVolume", namespace) is not None

    @pytest.mark.asyncio
    async def test_format_request_escape(self) -> None:
        """Test escaping the request an action sends."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:AVTransport:1")
        action = service.action("SetAVTransportURI")

        service_type = "urn:schemas-upnp-org:service:AVTransport:1"
        metadata = "<item>test thing</item>"
        request = action.create_request(
            InstanceID=0,
            CurrentURI="http://example.org/file.mp3",
            CurrentURIMetaData=metadata,
        )

        root = DET.fromstring(request.body)
        namespace = {"avt_service": service_type}
        assert root.find(".//avt_service:SetAVTransportURI", namespace) is not None
        assert root.find(".//CurrentURIMetaData", namespace) is not None
        assert (
            root.findtext(".//CurrentURIMetaData", None, namespace)
            == "<item>test thing</item>"
        )

        current_uri_metadata_el = root.find(".//CurrentURIMetaData", namespace)
        assert current_uri_metadata_el is not None
        # This shouldn't have any children, due to its contents being escaped.
        assert current_uri_metadata_el.findall("./") == []

    @pytest.mark.asyncio
    async def test_parse_response(self) -> None:
        """Test calling an action and handling its response."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        action = service.action("GetVolume")

        service_type = "urn:schemas-upnp-org:service:RenderingControl:1"
        response_body = read_file("dlna/dmr/action_GetVolume.xml")
        response = HttpResponse(200, {}, response_body)
        result = action.parse_response(service_type, response)
        assert result == {"CurrentVolume": 3}

    @pytest.mark.asyncio
    async def test_parse_response_empty(self) -> None:
        """Test calling an action and handling an empty XML response."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        action = service.action("SetVolume")

        service_type = "urn:schemas-upnp-org:service:RenderingControl:1"
        response_body = read_file("dlna/dmr/action_SetVolume.xml")
        response = HttpResponse(200, {}, response_body)
        result = action.parse_response(service_type, response)
        assert result == {}

    @pytest.mark.asyncio
    async def test_parse_response_error(self) -> None:
        """Test calling and action and handling an invalid XML response."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        action = service.action("GetVolume")

        service_type = "urn:schemas-upnp-org:service:RenderingControl:1"
        response_body = read_file("dlna/dmr/action_GetVolumeError.xml")
        response = HttpResponse(200, {}, response_body)
        with pytest.raises(UpnpActionError) as exc:
            action.parse_response(service_type, response)
        assert exc.value.error_code == UpnpActionErrorCode.INVALID_ARGS
        assert exc.value.error_desc == "Invalid Args"

    @pytest.mark.asyncio
    async def test_parse_response_escape(self) -> None:
        """Test calling an action and properly (not) escaping the response."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:AVTransport:1")
        action = service.action("GetMediaInfo")

        service_type = "urn:schemas-upnp-org:service:AVTransport:1"
        response_body = read_file("dlna/dmr/action_GetMediaInfo.xml")
        response = HttpResponse(200, {}, response_body)
        result = action.parse_response(service_type, response)
        assert result == {
            "CurrentURI": "uri://1.mp3",
            "CurrentURIMetaData": "<DIDL-Lite "
            'xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" '
            'xmlns:dc="http://purl.org/dc/elements/1.1/" '
            'xmlns:dlna="urn:schemas-dlna-org:metadata-1-0/" '
            'xmlns:sec="http://www.sec.co.kr/" '
            'xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" '
            'xmlns:xbmc="urn:schemas-xbmc-org:metadata-1-0/">'
            '<item id="" parentID="" refID="" restricted="1">'
            "<upnp:artist>A &amp; B &gt; C</upnp:artist>"
            "</item>"
            "</DIDL-Lite>",
            "MediaDuration": "00:00:01",
            "NextURI": "",
            "NextURIMetaData": "",
            "NrTracks": 1,
            "PlayMedium": "NONE",
            "RecordMedium": "NOT_IMPLEMENTED",
            "WriteStatus": "NOT_IMPLEMENTED",
        }

    @pytest.mark.asyncio
    async def test_parse_response_no_service_type_version(self) -> None:
        """Test calling and action and handling a response without service type number."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        action = service.action("GetVolume")

        service_type = "urn:schemas-upnp-org:service:RenderingControl:1"
        response_body = read_file("dlna/dmr/action_GetVolumeInvalidServiceType.xml")
        response = HttpResponse(200, {}, response_body)
        try:
            action.parse_response(service_type, response)
            assert False
        except UpnpError:
            pass

    @pytest.mark.asyncio
    async def test_parse_response_no_service_type_version_2(self) -> None:
        """Test calling and action and handling a response without service type number."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:AVTransport:1")
        action = service.action("GetTransportInfo")

        service_type = "urn:schemas-upnp-org:service:AVTransport:1"
        response_body = read_file(
            "dlna/dmr/action_GetTransportInfoInvalidServiceType.xml"
        )
        response = HttpResponse(200, {}, response_body)
        try:
            action.parse_response(service_type, response)
            assert False
        except UpnpError:
            pass

    @pytest.mark.asyncio
    async def test_unknown_out_argument(self) -> None:
        """Test calling an action and handling an unknown out-argument."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        device_url = "http://dlna_dmr:1234/device.xml"
        service_type = "urn:schemas-upnp-org:service:RenderingControl:1"
        test_action = "GetVolume"

        factory = UpnpFactory(requester)
        device = await factory.async_create_device(device_url)
        service = device.service(service_type)
        action = service.action(test_action)

        response_body = read_file("dlna/dmr/action_GetVolumeExtraOutParameter.xml")
        response = HttpResponse(200, {}, response_body)
        try:
            action.parse_response(service_type, response)
            assert False
        except UpnpError:
            pass

        factory = UpnpFactory(requester, non_strict=True)
        device = await factory.async_create_device(device_url)
        service = device.service(service_type)
        action = service.action(test_action)

        try:
            action.parse_response(service_type, response)
        except UpnpError:
            assert False

    @pytest.mark.asyncio
    async def test_response_invalid_xml_namespaces(self) -> None:
        """Test parsing response with invalid XML namespaces."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        device_url = "http://igd:1234/device.xml"
        service_type = "urn:schemas-upnp-org:service:WANIPConnection:1"
        test_action = "DeletePortMapping"

        # Test strict mode.
        factory = UpnpFactory(requester)
        device = await factory.async_create_device(device_url)
        service = device.find_service(service_type)
        assert service is not None
        action = service.action(test_action)

        response_body = read_file("igd/action_WANPIPConnection_DeletePortMapping.xml")
        response = HttpResponse(200, {}, response_body)
        try:
            action.parse_response(service_type, response)
            assert False
        except UpnpError:
            pass

        # Test non-strict mode.
        factory = UpnpFactory(requester, non_strict=True)
        device = await factory.async_create_device(device_url)
        service = device.find_service(service_type)
        assert service is not None
        action = service.action(test_action)

        try:
            action.parse_response(service_type, response)
        except UpnpError:
            assert False


class TestUpnpService:
    """Tests for UpnpService."""

    @pytest.mark.asyncio
    async def test_init(self) -> None:
        """Test initializing a UpnpService."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")

        base_url = "http://dlna_dmr:1234"
        assert service
        assert service.service_type == "urn:schemas-upnp-org:service:RenderingControl:1"
        assert service.control_url == base_url + "/upnp/control/RenderingControl1"
        assert service.event_sub_url == base_url + "/upnp/event/RenderingControl1"
        assert service.scpd_url == base_url + "/RenderingControl_1.xml"

    @pytest.mark.asyncio
    async def test_state_variables_actions(self) -> None:
        """Test eding a UpnpStateVariable."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")

        state_var = service.state_variable("Volume")
        assert state_var

        action = service.action("GetVolume")
        assert action

    @pytest.mark.asyncio
    async def test_call_action(self) -> None:
        """Test calling a UpnpAction."""
        responses: MutableMapping = {
            (
                "POST",
                "http://dlna_dmr:1234/upnp/control/RenderingControl1",
            ): HttpResponse(
                200,
                {},
                read_file("dlna/dmr/action_GetVolume.xml"),
            )
        }
        responses.update(RESPONSE_MAP)
        requester = UpnpTestRequester(responses)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        action = service.action("GetVolume")

        result = await service.async_call_action(action, InstanceID=0, Channel="Master")
        assert result["CurrentVolume"] == 3

    @pytest.mark.asyncio
    async def test_soap_fault_http_error(self) -> None:
        """Test an action response with HTTP error and SOAP fault raises exception."""
        responses: MutableMapping = {
            (
                "POST",
                "http://dlna_dmr:1234/upnp/control/RenderingControl1",
            ): HttpResponse(
                500,
                {},
                read_file("dlna/dmr/action_GetVolumeError.xml"),
            )
        }
        responses.update(RESPONSE_MAP)
        requester = UpnpTestRequester(responses)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        action = service.action("GetVolume")

        with pytest.raises(UpnpActionResponseError) as exc:
            await service.async_call_action(action, InstanceID=0, Channel="Master")
        assert exc.value.error_code == UpnpActionErrorCode.INVALID_ARGS
        assert exc.value.error_desc == "Invalid Args"
        assert exc.value.status == 500

    @pytest.mark.asyncio
    async def test_http_error(self) -> None:
        """Test an action response with HTTP error and blank body raises exception."""
        responses: MutableMapping = {
            (
                "POST",
                "http://dlna_dmr:1234/upnp/control/RenderingControl1",
            ): HttpResponse(
                500,
                {},
                "",
            )
        }
        responses.update(RESPONSE_MAP)
        requester = UpnpTestRequester(responses)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        action = service.action("GetVolume")

        with pytest.raises(UpnpResponseError) as exc:
            await service.async_call_action(action, InstanceID=0, Channel="Master")
        assert exc.value.status == 500

    @pytest.mark.asyncio
    async def test_soap_fault_http_ok(self) -> None:
        """Test an action response with HTTP OK but SOAP fault raises exception."""
        responses: MutableMapping = {
            (
                "POST",
                "http://dlna_dmr:1234/upnp/control/RenderingControl1",
            ): HttpResponse(
                200,
                {},
                read_file("dlna/dmr/action_GetVolumeError.xml"),
            )
        }
        responses.update(RESPONSE_MAP)
        requester = UpnpTestRequester(responses)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
        action = service.action("GetVolume")

        with pytest.raises(UpnpActionError) as exc:
            await service.async_call_action(action, InstanceID=0, Channel="Master")
        assert exc.value.error_code == UpnpActionErrorCode.INVALID_ARGS
        assert exc.value.error_desc == "Invalid Args"

    @pytest.mark.parametrize(
        "rc_doc",
        [
            "dlna/dmr/RenderingControl_1_bad_namespace.xml",  # Bad namespace
            "dlna/dmr/RenderingControl_1_bad_root_tag.xml",  # Wrong root tag
            "dlna/dmr/RenderingControl_1_missing_state_table.xml",  # Missing state table
        ],
    )
    @pytest.mark.asyncio
    async def test_bad_scpd_strict(self, rc_doc: str) -> None:
        """Test handling of bad service descriptions in strict mode."""
        responses = dict(RESPONSE_MAP)
        responses[("GET", "http://dlna_dmr:1234/RenderingControl_1.xml")] = (
            HttpResponse(
                200,
                {},
                read_file(rc_doc),
            )
        )
        requester = UpnpTestRequester(responses)
        factory = UpnpFactory(requester)
        with pytest.raises(UpnpXmlContentError):
            await factory.async_create_device("http://dlna_dmr:1234/device.xml")

    @pytest.mark.parametrize(
        "rc_doc",
        [
            "dlna/dmr/RenderingControl_1_bad_namespace.xml",  # Bad namespace
            "dlna/dmr/RenderingControl_1_bad_root_tag.xml",  # Wrong root tag
            "dlna/dmr/RenderingControl_1_missing_state_table.xml",  # Missing state table
        ],
    )
    @pytest.mark.asyncio
    async def test_bad_scpd_non_strict_fails(self, rc_doc: str) -> None:
        """Test bad SCPD in non-strict mode."""
        responses = dict(RESPONSE_MAP)
        responses[("GET", "http://dlna_dmr:1234/RenderingControl_1.xml")] = (
            HttpResponse(
                200,
                {},
                read_file(rc_doc),
            )
        )
        requester = UpnpTestRequester(responses)
        factory = UpnpFactory(requester, non_strict=True)
        device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
        # Known good service
        assert device.services["urn:schemas-upnp-org:service:AVTransport:1"]
        # Bad service will also exist, to some extent
        assert device.services["urn:schemas-upnp-org:service:RenderingControl:1"]