browse root node int test

This commit is contained in:
xis 2023-10-16 19:43:36 +02:00
parent e2c23b80e6
commit a2c330d9dc

View file

@ -1,13 +1,14 @@
package net.schowek.nextclouddlna.controller
import net.schowek.nextclouddlna.DlnaService
import net.schowek.nextclouddlna.dlna.media.MediaServer
import org.jupnp.support.contentdirectory.DIDLParser
import org.jupnp.support.model.DIDLContent
import org.jupnp.support.model.DIDLObject
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.w3c.dom.Document
import org.w3c.dom.Node
import org.w3c.dom.NodeList
@ -26,8 +27,6 @@ class UpnpControllerIntTest extends IntegrationSpecification {
@Autowired
private MediaServer mediaServer
@Autowired
private DlnaService dlnaService
def uid
@ -114,40 +113,20 @@ class UpnpControllerIntTest extends IntegrationSpecification {
}
}
def "should handle upnp browse ROOT request"() {
def "should handle upnp browse metadata for ROOT request"() {
given:
def requestBody = '<?xml version="1.0" encoding="utf-8"?>\n' +
'<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"\n' +
' s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">\n' +
' <s:Body>\n' +
' <u:Browse xmlns:u="urn:schemas-upnp-org:service:ContentDirectory:1">\n' +
' <ObjectID>0</ObjectID>\n' +
' <BrowseFlag>BrowseMetadata</BrowseFlag>\n' +
' <Filter>*</Filter>\n' +
' <StartingIndex>0</StartingIndex>\n' +
' <RequestedCount>0</RequestedCount>\n' +
' <SortCriteria></SortCriteria>\n' +
' </u:Browse>\n' +
' </s:Body>\n' +
'</s:Envelope>'
def nodeId = "0"
def browseFlag = "BrowseMetadata"
when:
def headers = new HttpHeaders([
'content-type': 'text/xml; charset="utf-8"',
'soapaction' : '"urn:schemas-upnp-org:service:ContentDirectory:1#Browse"'
]);
HttpEntity<String> request = new HttpEntity<String>(requestBody, headers);
def response = restTemplate().postForEntity(urlWithPort("/dev/$uid/svc/upnp-org/ContentDirectory/action"), request, String)
def response = performContentDirectoryAction(nodeId, browseFlag)
then:
response.statusCode == HttpStatus.OK
response.headers['content-type'].find() == "text/xml;charset=utf-8"
when:
Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
new InputSource(new StringReader(response.body))
);
def didl = new DIDLParser().parse(nodeValue(dom, "/Envelope/Body/BrowseResponse/Result"))
def didl = extractDIDLFromResponse(response)
then:
didl.containers.size() == 1
@ -163,6 +142,52 @@ class UpnpControllerIntTest extends IntegrationSpecification {
}
}
def "should handle upnp browse direct children for ROOT request"() {
given:
def nodeId = "0"
def browseFlag = "BrowseDirectChildren"
when:
def response = performContentDirectoryAction(nodeId, browseFlag)
then:
response.statusCode == HttpStatus.OK
response.headers['content-type'].find() == "text/xml;charset=utf-8"
when:
def didl = extractDIDLFromResponse(response)
then:
didl.containers.size() == 3
didl.containers.each {
assert it.searchable
assert it.restricted
assert it.writeStatus == NOT_WRITABLE
assert it.clazz.value == new DIDLObject.Class("object.container").value
}
with(didl.containers[0]) {
assert id == "2"
assert parentID == "1"
assert title == "johndoe"
assert childCount == 3
}
with(didl.containers[1]) {
assert id == "387"
assert parentID == "384"
assert title == "janedoe"
assert childCount == 1
}
with(didl.containers[2]) {
assert id == "586"
assert parentID == "584"
assert title == "family folder"
assert childCount == 1
}
}
private String nodeValue(Document dom, String pattern) {
return node(dom, "$pattern/text()").nodeValue
}
@ -176,4 +201,34 @@ class UpnpControllerIntTest extends IntegrationSpecification {
XPath xpath = XPathFactory.newInstance().newXPath();
return (xpath.evaluate(pattern, dom, NODESET) as NodeList)
}
private ResponseEntity performContentDirectoryAction(String nodeId, String browseFlag) {
def requestBody = '<?xml version="1.0" encoding="utf-8"?>\n' +
'<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"\n' +
' s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">\n' +
' <s:Body>\n' +
' <u:Browse xmlns:u="urn:schemas-upnp-org:service:ContentDirectory:1">\n' +
' <ObjectID>' + nodeId + '</ObjectID>\n' +
' <BrowseFlag>' + browseFlag + '</BrowseFlag>\n' +
' <Filter>*</Filter>\n' +
' <StartingIndex>0</StartingIndex>\n' +
' <RequestedCount>200</RequestedCount>\n' +
' <SortCriteria></SortCriteria>\n' +
' </u:Browse>\n' +
' </s:Body>\n' +
'</s:Envelope>'
def headers = new HttpHeaders([
'content-type': 'text/xml; charset="utf-8"',
'soapaction' : '"urn:schemas-upnp-org:service:ContentDirectory:1#Browse"'
]);
HttpEntity<String> request = new HttpEntity<String>(requestBody, headers);
return restTemplate().postForEntity(urlWithPort("/dev/$uid/svc/upnp-org/ContentDirectory/action"), request, String)
}
private DIDLContent extractDIDLFromResponse(ResponseEntity<String> response) {
Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
new InputSource(new StringReader(response.body))
);
return new DIDLParser().parse(nodeValue(dom, "/Envelope/Body/BrowseResponse/Result"))
}
}