|
10 | 10 |
|
11 | 11 | package at.bitfire.dav4jvm.ktor |
12 | 12 |
|
| 13 | +import at.bitfire.dav4jvm.XmlUtils |
13 | 14 | import io.ktor.http.Url |
| 15 | +import kotlinx.coroutines.test.runTest |
14 | 16 | import org.junit.Assert.assertEquals |
15 | 17 | import org.junit.Test |
| 18 | +import java.io.StringReader |
16 | 19 |
|
17 | 20 | class ResponseParserTest { |
18 | 21 |
|
19 | | - val baseUrl = Url("https://example.com/collection/") |
20 | | - val parser = ResponseParser(baseUrl, callback = { _, _ -> |
21 | | - // no-op |
22 | | - }) |
| 22 | + private val baseUrl = Url("http://www.example.com/container/") |
| 23 | + private val parser = ResponseParser(baseUrl) |
| 24 | + |
| 25 | + |
| 26 | + @Test |
| 27 | + fun `parseResponse relation=SELF`() = runTest { |
| 28 | + val xml = XmlUtils.newPullParser() |
| 29 | + xml.setInput(StringReader("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" + |
| 30 | + "<multistatus xmlns=\"DAV:\">\n" + |
| 31 | + "<response>\n" + |
| 32 | + " <href>http://www.example.com/container/</href>\n" + |
| 33 | + " <propstat>\n" + |
| 34 | + " <prop xmlns:R=\"http://ns.example.com/boxschema/\">\n" + |
| 35 | + " <R:bigbox/>\n" + |
| 36 | + " <R:author/>\n" + |
| 37 | + " <creationdate/>\n" + |
| 38 | + " <displayname/>\n" + |
| 39 | + " <resourcetype/>\n" + |
| 40 | + " <supportedlock/>\n" + |
| 41 | + " </prop>\n" + |
| 42 | + " <status>HTTP/1.1 200 OK</status>\n" + |
| 43 | + " </propstat>\n" + |
| 44 | + "</response>" |
| 45 | + )) |
| 46 | + xml.nextTag() // multistatus |
| 47 | + xml.nextTag() // response |
| 48 | + parser.parseResponse(xml) { response, relation -> |
| 49 | + assertEquals(Url("http://www.example.com/container/"), response.href) |
| 50 | + assertEquals(Response.HrefRelation.SELF, relation) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun `parseResponse relation=MEMBER`() = runTest { |
| 56 | + val xml = XmlUtils.newPullParser() |
| 57 | + xml.setInput(StringReader("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" + |
| 58 | + "<multistatus xmlns=\"DAV:\">\n" + |
| 59 | + "<response>\n" + |
| 60 | + " <href>http://www.example.com/container/front.html</href>\n" + |
| 61 | + " <propstat>\n" + |
| 62 | + " <prop xmlns:R=\"http://ns.example.com/boxschema/\">\n" + |
| 63 | + " <R:bigbox/>\n" + |
| 64 | + " <creationdate/>\n" + |
| 65 | + " <displayname/>\n" + |
| 66 | + " <getcontentlength/>\n" + |
| 67 | + " <getcontenttype/>\n" + |
| 68 | + " <getetag/>\n" + |
| 69 | + " <getlastmodified/>\n" + |
| 70 | + " <resourcetype/>\n" + |
| 71 | + " <supportedlock/>\n" + |
| 72 | + " </prop>\n" + |
| 73 | + " <status>HTTP/1.1 200 OK</status>\n" + |
| 74 | + " </propstat>\n" + |
| 75 | + "</response>" |
| 76 | + )) |
| 77 | + xml.nextTag() // multistatus |
| 78 | + xml.nextTag() // response |
| 79 | + parser.parseResponse(xml) { response, relation -> |
| 80 | + assertEquals(Url("http://www.example.com/container/front.html"), response.href) |
| 81 | + assertEquals(Response.HrefRelation.MEMBER, relation) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun `parseResponse relation=OTHER`() = runTest { |
| 87 | + val xml = XmlUtils.newPullParser() |
| 88 | + xml.setInput(StringReader("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" + |
| 89 | + "<multistatus xmlns=\"DAV:\">\n" + |
| 90 | + "<response>\n" + |
| 91 | + " <href>http://other.example.com/was-not-requested</href>\n" + |
| 92 | + " <propstat>\n" + |
| 93 | + " <prop xmlns:R=\"http://ns.example.com/boxschema/\">\n" + |
| 94 | + " <R:bigbox/>\n" + |
| 95 | + " <creationdate/>\n" + |
| 96 | + " <displayname/>\n" + |
| 97 | + " <getcontentlength/>\n" + |
| 98 | + " <getcontenttype/>\n" + |
| 99 | + " <getetag/>\n" + |
| 100 | + " <getlastmodified/>\n" + |
| 101 | + " <resourcetype/>\n" + |
| 102 | + " <supportedlock/>\n" + |
| 103 | + " </prop>\n" + |
| 104 | + " <status>HTTP/1.1 200 OK</status>\n" + |
| 105 | + " </propstat>\n" + |
| 106 | + "</response>" |
| 107 | + )) |
| 108 | + xml.nextTag() // multistatus |
| 109 | + xml.nextTag() // response |
| 110 | + parser.parseResponse(xml) { response, relation -> |
| 111 | + assertEquals(Url("http://other.example.com/was-not-requested"), response.href) |
| 112 | + assertEquals(Response.HrefRelation.OTHER, relation) |
| 113 | + } |
| 114 | + } |
| 115 | + |
23 | 116 |
|
24 | 117 | @Test |
25 | 118 | fun `resolveHref with absolute URL`() { |
26 | 119 | assertEquals( |
27 | | - Url("https://example.com/collection/member"), |
28 | | - parser.resolveHref("https://example.com/collection/member") |
| 120 | + Url("http://www.example.com/container/member"), |
| 121 | + parser.resolveHref("http://www.example.com/container/member") |
29 | 122 | ) |
30 | 123 | } |
31 | 124 |
|
32 | 125 | @Test |
33 | 126 | fun `resolveHref with absolute path`() { |
34 | 127 | assertEquals( |
35 | | - Url("https://example.com/collection/member"), |
36 | | - parser.resolveHref("/collection/member") |
| 128 | + Url("http://www.example.com/container/member"), |
| 129 | + parser.resolveHref("/container/member") |
37 | 130 | ) |
38 | 131 | } |
39 | 132 |
|
40 | 133 | @Test |
41 | 134 | fun `resolveHref with relative path`() { |
42 | 135 | assertEquals( |
43 | | - Url("https://example.com/collection/member"), |
| 136 | + Url("http://www.example.com/container/member"), |
44 | 137 | parser.resolveHref("member") |
45 | 138 | ) |
46 | 139 | } |
47 | 140 |
|
48 | 141 | @Test |
49 | 142 | fun `resolveHref with relative path with colon`() { |
50 | 143 | assertEquals( |
51 | | - Url("https://example.com/collection/mem:ber"), |
| 144 | + Url("http://www.example.com/container/mem:ber"), |
52 | 145 | parser.resolveHref("mem:ber") |
53 | 146 | ) |
54 | 147 | } |
|
0 commit comments