xml_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package webdav
  5. import (
  6. "bytes"
  7. "encoding/xml"
  8. "io"
  9. "net/http"
  10. "net/http/httptest"
  11. "reflect"
  12. "strings"
  13. "testing"
  14. )
  15. func TestReadLockInfo(t *testing.T) {
  16. // The "section x.y.z" test cases come from section x.y.z of the spec at
  17. // http://www.webdav.org/specs/rfc4918.html
  18. testCases := []struct {
  19. desc string
  20. input string
  21. wantLI lockInfo
  22. wantStatus int
  23. }{{
  24. "bad: junk",
  25. "xxx",
  26. lockInfo{},
  27. http.StatusBadRequest,
  28. }, {
  29. "bad: invalid owner XML",
  30. "" +
  31. "<D:lockinfo xmlns:D='DAV:'>\n" +
  32. " <D:lockscope><D:exclusive/></D:lockscope>\n" +
  33. " <D:locktype><D:write/></D:locktype>\n" +
  34. " <D:owner>\n" +
  35. " <D:href> no end tag \n" +
  36. " </D:owner>\n" +
  37. "</D:lockinfo>",
  38. lockInfo{},
  39. http.StatusBadRequest,
  40. }, {
  41. "bad: invalid UTF-8",
  42. "" +
  43. "<D:lockinfo xmlns:D='DAV:'>\n" +
  44. " <D:lockscope><D:exclusive/></D:lockscope>\n" +
  45. " <D:locktype><D:write/></D:locktype>\n" +
  46. " <D:owner>\n" +
  47. " <D:href> \xff </D:href>\n" +
  48. " </D:owner>\n" +
  49. "</D:lockinfo>",
  50. lockInfo{},
  51. http.StatusBadRequest,
  52. }, {
  53. "bad: unfinished XML #1",
  54. "" +
  55. "<D:lockinfo xmlns:D='DAV:'>\n" +
  56. " <D:lockscope><D:exclusive/></D:lockscope>\n" +
  57. " <D:locktype><D:write/></D:locktype>\n",
  58. lockInfo{},
  59. http.StatusBadRequest,
  60. }, {
  61. "bad: unfinished XML #2",
  62. "" +
  63. "<D:lockinfo xmlns:D='DAV:'>\n" +
  64. " <D:lockscope><D:exclusive/></D:lockscope>\n" +
  65. " <D:locktype><D:write/></D:locktype>\n" +
  66. " <D:owner>\n",
  67. lockInfo{},
  68. http.StatusBadRequest,
  69. }, {
  70. "good: empty",
  71. "",
  72. lockInfo{},
  73. 0,
  74. }, {
  75. "good: plain-text owner",
  76. "" +
  77. "<D:lockinfo xmlns:D='DAV:'>\n" +
  78. " <D:lockscope><D:exclusive/></D:lockscope>\n" +
  79. " <D:locktype><D:write/></D:locktype>\n" +
  80. " <D:owner>gopher</D:owner>\n" +
  81. "</D:lockinfo>",
  82. lockInfo{
  83. XMLName: xml.Name{Space: "DAV:", Local: "lockinfo"},
  84. Exclusive: new(struct{}),
  85. Write: new(struct{}),
  86. Owner: owner{
  87. InnerXML: "gopher",
  88. },
  89. },
  90. 0,
  91. }, {
  92. "section 9.10.7",
  93. "" +
  94. "<D:lockinfo xmlns:D='DAV:'>\n" +
  95. " <D:lockscope><D:exclusive/></D:lockscope>\n" +
  96. " <D:locktype><D:write/></D:locktype>\n" +
  97. " <D:owner>\n" +
  98. " <D:href>http://example.org/~ejw/contact.html</D:href>\n" +
  99. " </D:owner>\n" +
  100. "</D:lockinfo>",
  101. lockInfo{
  102. XMLName: xml.Name{Space: "DAV:", Local: "lockinfo"},
  103. Exclusive: new(struct{}),
  104. Write: new(struct{}),
  105. Owner: owner{
  106. InnerXML: "\n <D:href>http://example.org/~ejw/contact.html</D:href>\n ",
  107. },
  108. },
  109. 0,
  110. }}
  111. for _, tc := range testCases {
  112. li, status, err := readLockInfo(strings.NewReader(tc.input))
  113. if tc.wantStatus != 0 {
  114. if err == nil {
  115. t.Errorf("%s: got nil error, want non-nil", tc.desc)
  116. continue
  117. }
  118. } else if err != nil {
  119. t.Errorf("%s: %v", tc.desc, err)
  120. continue
  121. }
  122. if !reflect.DeepEqual(li, tc.wantLI) || status != tc.wantStatus {
  123. t.Errorf("%s:\ngot lockInfo=%v, status=%v\nwant lockInfo=%v, status=%v",
  124. tc.desc, li, status, tc.wantLI, tc.wantStatus)
  125. continue
  126. }
  127. }
  128. }
  129. func TestReadPropfind(t *testing.T) {
  130. testCases := []struct {
  131. desc string
  132. input string
  133. wantPF propfind
  134. wantStatus int
  135. }{{
  136. desc: "propfind: propname",
  137. input: "" +
  138. "<A:propfind xmlns:A='DAV:'>\n" +
  139. " <A:propname/>\n" +
  140. "</A:propfind>",
  141. wantPF: propfind{
  142. XMLName: xml.Name{Space: "DAV:", Local: "propfind"},
  143. Propname: new(struct{}),
  144. },
  145. }, {
  146. desc: "propfind: empty body means allprop",
  147. input: "",
  148. wantPF: propfind{
  149. Allprop: new(struct{}),
  150. },
  151. }, {
  152. desc: "propfind: allprop",
  153. input: "" +
  154. "<A:propfind xmlns:A='DAV:'>\n" +
  155. " <A:allprop/>\n" +
  156. "</A:propfind>",
  157. wantPF: propfind{
  158. XMLName: xml.Name{Space: "DAV:", Local: "propfind"},
  159. Allprop: new(struct{}),
  160. },
  161. }, {
  162. desc: "propfind: allprop followed by include",
  163. input: "" +
  164. "<A:propfind xmlns:A='DAV:'>\n" +
  165. " <A:allprop/>\n" +
  166. " <A:include><A:displayname/></A:include>\n" +
  167. "</A:propfind>",
  168. wantPF: propfind{
  169. XMLName: xml.Name{Space: "DAV:", Local: "propfind"},
  170. Allprop: new(struct{}),
  171. Include: propfindProps{xml.Name{Space: "DAV:", Local: "displayname"}},
  172. },
  173. }, {
  174. desc: "propfind: include followed by allprop",
  175. input: "" +
  176. "<A:propfind xmlns:A='DAV:'>\n" +
  177. " <A:include><A:displayname/></A:include>\n" +
  178. " <A:allprop/>\n" +
  179. "</A:propfind>",
  180. wantPF: propfind{
  181. XMLName: xml.Name{Space: "DAV:", Local: "propfind"},
  182. Allprop: new(struct{}),
  183. Include: propfindProps{xml.Name{Space: "DAV:", Local: "displayname"}},
  184. },
  185. }, {
  186. desc: "propfind: propfind",
  187. input: "" +
  188. "<A:propfind xmlns:A='DAV:'>\n" +
  189. " <A:prop><A:displayname/></A:prop>\n" +
  190. "</A:propfind>",
  191. wantPF: propfind{
  192. XMLName: xml.Name{Space: "DAV:", Local: "propfind"},
  193. Prop: propfindProps{xml.Name{Space: "DAV:", Local: "displayname"}},
  194. },
  195. }, {
  196. desc: "propfind: prop with ignored comments",
  197. input: "" +
  198. "<A:propfind xmlns:A='DAV:'>\n" +
  199. " <A:prop>\n" +
  200. " <!-- ignore -->\n" +
  201. " <A:displayname><!-- ignore --></A:displayname>\n" +
  202. " </A:prop>\n" +
  203. "</A:propfind>",
  204. wantPF: propfind{
  205. XMLName: xml.Name{Space: "DAV:", Local: "propfind"},
  206. Prop: propfindProps{xml.Name{Space: "DAV:", Local: "displayname"}},
  207. },
  208. }, {
  209. desc: "propfind: propfind with ignored whitespace",
  210. input: "" +
  211. "<A:propfind xmlns:A='DAV:'>\n" +
  212. " <A:prop> <A:displayname/></A:prop>\n" +
  213. "</A:propfind>",
  214. wantPF: propfind{
  215. XMLName: xml.Name{Space: "DAV:", Local: "propfind"},
  216. Prop: propfindProps{xml.Name{Space: "DAV:", Local: "displayname"}},
  217. },
  218. }, {
  219. desc: "propfind: propfind with ignored mixed-content",
  220. input: "" +
  221. "<A:propfind xmlns:A='DAV:'>\n" +
  222. " <A:prop>foo<A:displayname/>bar</A:prop>\n" +
  223. "</A:propfind>",
  224. wantPF: propfind{
  225. XMLName: xml.Name{Space: "DAV:", Local: "propfind"},
  226. Prop: propfindProps{xml.Name{Space: "DAV:", Local: "displayname"}},
  227. },
  228. }, {
  229. desc: "propfind: propname with ignored element (section A.4)",
  230. input: "" +
  231. "<A:propfind xmlns:A='DAV:'>\n" +
  232. " <A:propname/>\n" +
  233. " <E:leave-out xmlns:E='E:'>*boss*</E:leave-out>\n" +
  234. "</A:propfind>",
  235. wantPF: propfind{
  236. XMLName: xml.Name{Space: "DAV:", Local: "propfind"},
  237. Propname: new(struct{}),
  238. },
  239. }, {
  240. desc: "propfind: bad: junk",
  241. input: "xxx",
  242. wantStatus: http.StatusBadRequest,
  243. }, {
  244. desc: "propfind: bad: propname and allprop (section A.3)",
  245. input: "" +
  246. "<A:propfind xmlns:A='DAV:'>\n" +
  247. " <A:propname/>" +
  248. " <A:allprop/>" +
  249. "</A:propfind>",
  250. wantStatus: http.StatusBadRequest,
  251. }, {
  252. desc: "propfind: bad: propname and prop",
  253. input: "" +
  254. "<A:propfind xmlns:A='DAV:'>\n" +
  255. " <A:prop><A:displayname/></A:prop>\n" +
  256. " <A:propname/>\n" +
  257. "</A:propfind>",
  258. wantStatus: http.StatusBadRequest,
  259. }, {
  260. desc: "propfind: bad: allprop and prop",
  261. input: "" +
  262. "<A:propfind xmlns:A='DAV:'>\n" +
  263. " <A:allprop/>\n" +
  264. " <A:prop><A:foo/><A:/prop>\n" +
  265. "</A:propfind>",
  266. wantStatus: http.StatusBadRequest,
  267. }, {
  268. desc: "propfind: bad: empty propfind with ignored element (section A.4)",
  269. input: "" +
  270. "<A:propfind xmlns:A='DAV:'>\n" +
  271. " <E:expired-props/>\n" +
  272. "</A:propfind>",
  273. wantStatus: http.StatusBadRequest,
  274. }, {
  275. desc: "propfind: bad: empty prop",
  276. input: "" +
  277. "<A:propfind xmlns:A='DAV:'>\n" +
  278. " <A:prop/>\n" +
  279. "</A:propfind>",
  280. wantStatus: http.StatusBadRequest,
  281. }, {
  282. desc: "propfind: bad: prop with just chardata",
  283. input: "" +
  284. "<A:propfind xmlns:A='DAV:'>\n" +
  285. " <A:prop>foo</A:prop>\n" +
  286. "</A:propfind>",
  287. wantStatus: http.StatusBadRequest,
  288. }, {
  289. desc: "bad: interrupted prop",
  290. input: "" +
  291. "<A:propfind xmlns:A='DAV:'>\n" +
  292. " <A:prop><A:foo></A:prop>\n",
  293. wantStatus: http.StatusBadRequest,
  294. }, {
  295. desc: "bad: malformed end element prop",
  296. input: "" +
  297. "<A:propfind xmlns:A='DAV:'>\n" +
  298. " <A:prop><A:foo/></A:bar></A:prop>\n",
  299. wantStatus: http.StatusBadRequest,
  300. }, {
  301. desc: "propfind: bad: property with chardata value",
  302. input: "" +
  303. "<A:propfind xmlns:A='DAV:'>\n" +
  304. " <A:prop><A:foo>bar</A:foo></A:prop>\n" +
  305. "</A:propfind>",
  306. wantStatus: http.StatusBadRequest,
  307. }, {
  308. desc: "propfind: bad: property with whitespace value",
  309. input: "" +
  310. "<A:propfind xmlns:A='DAV:'>\n" +
  311. " <A:prop><A:foo> </A:foo></A:prop>\n" +
  312. "</A:propfind>",
  313. wantStatus: http.StatusBadRequest,
  314. }, {
  315. desc: "propfind: bad: include without allprop",
  316. input: "" +
  317. "<A:propfind xmlns:A='DAV:'>\n" +
  318. " <A:include><A:foo/></A:include>\n" +
  319. "</A:propfind>",
  320. wantStatus: http.StatusBadRequest,
  321. }}
  322. for _, tc := range testCases {
  323. pf, status, err := readPropfind(strings.NewReader(tc.input))
  324. if tc.wantStatus != 0 {
  325. if err == nil {
  326. t.Errorf("%s: got nil error, want non-nil", tc.desc)
  327. continue
  328. }
  329. } else if err != nil {
  330. t.Errorf("%s: %v", tc.desc, err)
  331. continue
  332. }
  333. if !reflect.DeepEqual(pf, tc.wantPF) || status != tc.wantStatus {
  334. t.Errorf("%s:\ngot propfind=%v, status=%v\nwant propfind=%v, status=%v",
  335. tc.desc, pf, status, tc.wantPF, tc.wantStatus)
  336. continue
  337. }
  338. }
  339. }
  340. func TestMultistatusWriter(t *testing.T) {
  341. ///The "section x.y.z" test cases come from section x.y.z of the spec at
  342. // http://www.webdav.org/specs/rfc4918.html
  343. testCases := []struct {
  344. desc string
  345. responses []response
  346. respdesc string
  347. writeHeader bool
  348. wantXML string
  349. wantCode int
  350. wantErr error
  351. }{{
  352. desc: "section 9.2.2 (failed dependency)",
  353. responses: []response{{
  354. Href: []string{"http://example.com/foo"},
  355. Propstat: []propstat{{
  356. Prop: []Property{{
  357. XMLName: xml.Name{
  358. Space: "http://ns.example.com/",
  359. Local: "Authors",
  360. },
  361. }},
  362. Status: "HTTP/1.1 424 Failed Dependency",
  363. }, {
  364. Prop: []Property{{
  365. XMLName: xml.Name{
  366. Space: "http://ns.example.com/",
  367. Local: "Copyright-Owner",
  368. },
  369. }},
  370. Status: "HTTP/1.1 409 Conflict",
  371. }},
  372. ResponseDescription: "Copyright Owner cannot be deleted or altered.",
  373. }},
  374. wantXML: `` +
  375. `<?xml version="1.0" encoding="UTF-8"?>` +
  376. `<multistatus xmlns="DAV:">` +
  377. ` <response>` +
  378. ` <href>http://example.com/foo</href>` +
  379. ` <propstat>` +
  380. ` <prop>` +
  381. ` <Authors xmlns="http://ns.example.com/"></Authors>` +
  382. ` </prop>` +
  383. ` <status>HTTP/1.1 424 Failed Dependency</status>` +
  384. ` </propstat>` +
  385. ` <propstat xmlns="DAV:">` +
  386. ` <prop>` +
  387. ` <Copyright-Owner xmlns="http://ns.example.com/"></Copyright-Owner>` +
  388. ` </prop>` +
  389. ` <status>HTTP/1.1 409 Conflict</status>` +
  390. ` </propstat>` +
  391. ` <responsedescription>Copyright Owner cannot be deleted or altered.</responsedescription>` +
  392. `</response>` +
  393. `</multistatus>`,
  394. wantCode: StatusMulti,
  395. }, {
  396. desc: "section 9.6.2 (lock-token-submitted)",
  397. responses: []response{{
  398. Href: []string{"http://example.com/foo"},
  399. Status: "HTTP/1.1 423 Locked",
  400. Error: &xmlError{
  401. InnerXML: []byte(`<lock-token-submitted xmlns="DAV:"/>`),
  402. },
  403. }},
  404. wantXML: `` +
  405. `<?xml version="1.0" encoding="UTF-8"?>` +
  406. `<multistatus xmlns="DAV:">` +
  407. ` <response>` +
  408. ` <href>http://example.com/foo</href>` +
  409. ` <status>HTTP/1.1 423 Locked</status>` +
  410. ` <error><lock-token-submitted xmlns="DAV:"/></error>` +
  411. ` </response>` +
  412. `</multistatus>`,
  413. wantCode: StatusMulti,
  414. }, {
  415. desc: "section 9.1.3",
  416. responses: []response{{
  417. Href: []string{"http://example.com/foo"},
  418. Propstat: []propstat{{
  419. Prop: []Property{{
  420. XMLName: xml.Name{Space: "http://ns.example.com/boxschema/", Local: "bigbox"},
  421. InnerXML: []byte(`` +
  422. `<BoxType xmlns="http://ns.example.com/boxschema/">` +
  423. `Box type A` +
  424. `</BoxType>`),
  425. }, {
  426. XMLName: xml.Name{Space: "http://ns.example.com/boxschema/", Local: "author"},
  427. InnerXML: []byte(`` +
  428. `<Name xmlns="http://ns.example.com/boxschema/">` +
  429. `J.J. Johnson` +
  430. `</Name>`),
  431. }},
  432. Status: "HTTP/1.1 200 OK",
  433. }, {
  434. Prop: []Property{{
  435. XMLName: xml.Name{Space: "http://ns.example.com/boxschema/", Local: "DingALing"},
  436. }, {
  437. XMLName: xml.Name{Space: "http://ns.example.com/boxschema/", Local: "Random"},
  438. }},
  439. Status: "HTTP/1.1 403 Forbidden",
  440. ResponseDescription: "The user does not have access to the DingALing property.",
  441. }},
  442. }},
  443. respdesc: "There has been an access violation error.",
  444. wantXML: `` +
  445. `<?xml version="1.0" encoding="UTF-8"?>` +
  446. `<multistatus xmlns="DAV:">` +
  447. ` <response>` +
  448. ` <href>http://example.com/foo</href>` +
  449. ` <propstat>` +
  450. ` <prop>` +
  451. ` <bigbox xmlns="http://ns.example.com/boxschema/"><BoxType xmlns="http://ns.example.com/boxschema/">Box type A</BoxType></bigbox>` +
  452. ` <author xmlns="http://ns.example.com/boxschema/"><Name xmlns="http://ns.example.com/boxschema/">J.J. Johnson</Name></author>` +
  453. ` </prop>` +
  454. ` <status>HTTP/1.1 200 OK</status>` +
  455. ` </propstat>` +
  456. ` <propstat>` +
  457. ` <prop>` +
  458. ` <DingALing xmlns="http://ns.example.com/boxschema/"></DingALing>` +
  459. ` <Random xmlns="http://ns.example.com/boxschema/"></Random>` +
  460. ` </prop>` +
  461. ` <status>HTTP/1.1 403 Forbidden</status>` +
  462. ` <responsedescription>The user does not have access to the DingALing property.</responsedescription>` +
  463. ` </propstat>` +
  464. ` </response>` +
  465. ` <responsedescription>There has been an access violation error.</responsedescription>` +
  466. `</multistatus>`,
  467. wantCode: StatusMulti,
  468. }, {
  469. desc: "no response written",
  470. // default of http.responseWriter
  471. wantCode: http.StatusOK,
  472. }, {
  473. desc: "no response written (with description)",
  474. respdesc: "too bad",
  475. // default of http.responseWriter
  476. wantCode: http.StatusOK,
  477. }, {
  478. desc: "empty multistatus with header",
  479. writeHeader: true,
  480. wantXML: `<multistatus xmlns="DAV:"></multistatus>`,
  481. wantCode: StatusMulti,
  482. }, {
  483. desc: "bad: no href",
  484. responses: []response{{
  485. Propstat: []propstat{{
  486. Prop: []Property{{
  487. XMLName: xml.Name{
  488. Space: "http://example.com/",
  489. Local: "foo",
  490. },
  491. }},
  492. Status: "HTTP/1.1 200 OK",
  493. }},
  494. }},
  495. wantErr: errInvalidResponse,
  496. // default of http.responseWriter
  497. wantCode: http.StatusOK,
  498. }, {
  499. desc: "bad: multiple hrefs and no status",
  500. responses: []response{{
  501. Href: []string{"http://example.com/foo", "http://example.com/bar"},
  502. }},
  503. wantErr: errInvalidResponse,
  504. // default of http.responseWriter
  505. wantCode: http.StatusOK,
  506. }, {
  507. desc: "bad: one href and no propstat",
  508. responses: []response{{
  509. Href: []string{"http://example.com/foo"},
  510. }},
  511. wantErr: errInvalidResponse,
  512. // default of http.responseWriter
  513. wantCode: http.StatusOK,
  514. }, {
  515. desc: "bad: status with one href and propstat",
  516. responses: []response{{
  517. Href: []string{"http://example.com/foo"},
  518. Propstat: []propstat{{
  519. Prop: []Property{{
  520. XMLName: xml.Name{
  521. Space: "http://example.com/",
  522. Local: "foo",
  523. },
  524. }},
  525. Status: "HTTP/1.1 200 OK",
  526. }},
  527. Status: "HTTP/1.1 200 OK",
  528. }},
  529. wantErr: errInvalidResponse,
  530. // default of http.responseWriter
  531. wantCode: http.StatusOK,
  532. }, {
  533. desc: "bad: multiple hrefs and propstat",
  534. responses: []response{{
  535. Href: []string{
  536. "http://example.com/foo",
  537. "http://example.com/bar",
  538. },
  539. Propstat: []propstat{{
  540. Prop: []Property{{
  541. XMLName: xml.Name{
  542. Space: "http://example.com/",
  543. Local: "foo",
  544. },
  545. }},
  546. Status: "HTTP/1.1 200 OK",
  547. }},
  548. }},
  549. wantErr: errInvalidResponse,
  550. // default of http.responseWriter
  551. wantCode: http.StatusOK,
  552. }}
  553. loop:
  554. for _, tc := range testCases {
  555. rec := httptest.NewRecorder()
  556. w := multistatusWriter{w: rec, responseDescription: tc.respdesc}
  557. if tc.writeHeader {
  558. if err := w.writeHeader(); err != nil {
  559. t.Errorf("%s: got writeHeader error %v, want nil", tc.desc, err)
  560. continue
  561. }
  562. }
  563. for _, r := range tc.responses {
  564. if err := w.write(&r); err != nil {
  565. if err != tc.wantErr {
  566. t.Errorf("%s: got write error %v, want %v",
  567. tc.desc, err, tc.wantErr)
  568. }
  569. continue loop
  570. }
  571. }
  572. if err := w.close(); err != tc.wantErr {
  573. t.Errorf("%s: got close error %v, want %v",
  574. tc.desc, err, tc.wantErr)
  575. continue
  576. }
  577. if rec.Code != tc.wantCode {
  578. t.Errorf("%s: got HTTP status code %d, want %d\n",
  579. tc.desc, rec.Code, tc.wantCode)
  580. continue
  581. }
  582. // normalize returns the normalized XML content of s. In contrast to
  583. // the WebDAV specification, it ignores whitespace within property
  584. // values of mixed XML content.
  585. normalize := func(s string) string {
  586. d := xml.NewDecoder(strings.NewReader(s))
  587. var b bytes.Buffer
  588. e := xml.NewEncoder(&b)
  589. for {
  590. tok, err := d.Token()
  591. if err != nil {
  592. if err == io.EOF {
  593. break
  594. }
  595. t.Fatalf("%s: Token %v", tc.desc, err)
  596. }
  597. switch val := tok.(type) {
  598. case xml.Comment, xml.Directive, xml.ProcInst:
  599. continue
  600. case xml.CharData:
  601. if len(bytes.TrimSpace(val)) == 0 {
  602. continue
  603. }
  604. }
  605. if err := e.EncodeToken(tok); err != nil {
  606. t.Fatalf("%s: EncodeToken: %v", tc.desc, err)
  607. }
  608. }
  609. if err := e.Flush(); err != nil {
  610. t.Fatalf("%s: Flush: %v", tc.desc, err)
  611. }
  612. return b.String()
  613. }
  614. gotXML := normalize(rec.Body.String())
  615. wantXML := normalize(tc.wantXML)
  616. if gotXML != wantXML {
  617. t.Errorf("%s: XML body\ngot %q\nwant %q", tc.desc, gotXML, wantXML)
  618. }
  619. }
  620. }
  621. func TestReadProppatch(t *testing.T) {
  622. // TODO(rost): These "golden XML" tests easily break with changes in the
  623. // xml package. A whitespace-preserving normalizer of XML content is
  624. // required to make these tests more robust.
  625. testCases := []struct {
  626. desc string
  627. input string
  628. wantPP []Proppatch
  629. wantStatus int
  630. }{{
  631. desc: "proppatch: section 9.2",
  632. input: `` +
  633. `<?xml version="1.0" encoding="utf-8" ?>` +
  634. `<D:propertyupdate xmlns:D="DAV:"` +
  635. ` xmlns:Z="http://ns.example.com/z/">` +
  636. ` <D:set>` +
  637. ` <D:prop>` +
  638. ` <Z:Authors>` +
  639. ` <Z:Author>Jim Whitehead</Z:Author>` +
  640. ` <Z:Author>Roy Fielding</Z:Author>` +
  641. ` </Z:Authors>` +
  642. ` </D:prop>` +
  643. ` </D:set>` +
  644. ` <D:remove>` +
  645. ` <D:prop><Z:Copyright-Owner/></D:prop>` +
  646. ` </D:remove>` +
  647. `</D:propertyupdate>`,
  648. wantPP: []Proppatch{{
  649. Props: []Property{{
  650. xml.Name{Space: "http://ns.example.com/z/", Local: "Authors"},
  651. "",
  652. []byte(`` +
  653. ` ` +
  654. `<z:Author xmlns:z="http://ns.example.com/z/">` +
  655. `Jim Whitehead` +
  656. `</z:Author>` +
  657. ` ` +
  658. `<z:Author xmlns:z="http://ns.example.com/z/">` +
  659. `Roy Fielding` +
  660. `</z:Author>` +
  661. ` `,
  662. ),
  663. }},
  664. }, {
  665. Remove: true,
  666. Props: []Property{{
  667. xml.Name{Space: "http://ns.example.com/z/", Local: "Copyright-Owner"},
  668. "",
  669. nil,
  670. }},
  671. }},
  672. }, {
  673. desc: "proppatch: section 4.3.1 (mixed content)",
  674. input: `` +
  675. `<?xml version="1.0" encoding="utf-8" ?>` +
  676. `<D:propertyupdate xmlns:D="DAV:"` +
  677. ` xmlns:Z="http://ns.example.com/z/">` +
  678. ` <D:set>` +
  679. ` <D:prop xml:lang="en" xmlns:D="DAV:">` +
  680. ` <x:author xmlns:x='http://example.com/ns'>` +
  681. ` <x:name>Jane Doe</x:name>` +
  682. ` <!-- Jane's contact info -->` +
  683. ` <x:uri type='email'` +
  684. ` added='2005-11-26'>mailto:jane.doe@example.com</x:uri>` +
  685. ` <x:uri type='web'` +
  686. ` added='2005-11-27'>http://www.example.com</x:uri>` +
  687. ` <x:notes xmlns:h='http://www.w3.org/1999/xhtml'>` +
  688. ` Jane has been working way <h:em>too</h:em> long on the` +
  689. ` long-awaited revision of <![CDATA[<RFC2518>]]>.` +
  690. ` </x:notes>` +
  691. ` </x:author>` +
  692. ` </D:prop>` +
  693. ` </D:set>` +
  694. `</D:propertyupdate>`,
  695. wantPP: []Proppatch{{
  696. Props: []Property{{
  697. xml.Name{Space: "http://example.com/ns", Local: "author"},
  698. "en",
  699. []byte(`` +
  700. ` ` +
  701. `<ns:name xmlns:ns="http://example.com/ns">Jane Doe</ns:name>` +
  702. ` ` +
  703. `<ns:uri xmlns:ns="http://example.com/ns" type="email" added="2005-11-26">` +
  704. `mailto:jane.doe@example.com` +
  705. `</ns:uri>` +
  706. ` ` +
  707. `<ns:uri xmlns:ns="http://example.com/ns" type="web" added="2005-11-27">` +
  708. `http://www.example.com` +
  709. `</ns:uri>` +
  710. ` ` +
  711. `<ns:notes xmlns:ns="http://example.com/ns"` +
  712. ` xmlns:h="http://www.w3.org/1999/xhtml">` +
  713. ` ` +
  714. ` Jane has been working way` +
  715. ` <h:em>too</h:em>` +
  716. ` long on the` + ` ` +
  717. ` long-awaited revision of &lt;RFC2518&gt;.` +
  718. ` ` +
  719. `</ns:notes>` +
  720. ` `,
  721. ),
  722. }},
  723. }},
  724. }, {
  725. desc: "proppatch: lang attribute on prop",
  726. input: `` +
  727. `<?xml version="1.0" encoding="utf-8" ?>` +
  728. `<D:propertyupdate xmlns:D="DAV:">` +
  729. ` <D:set>` +
  730. ` <D:prop xml:lang="en">` +
  731. ` <foo xmlns="http://example.com/ns"/>` +
  732. ` </D:prop>` +
  733. ` </D:set>` +
  734. `</D:propertyupdate>`,
  735. wantPP: []Proppatch{{
  736. Props: []Property{{
  737. xml.Name{Space: "http://example.com/ns", Local: "foo"},
  738. "en",
  739. nil,
  740. }},
  741. }},
  742. }, {
  743. desc: "bad: remove with value",
  744. input: `` +
  745. `<?xml version="1.0" encoding="utf-8" ?>` +
  746. `<D:propertyupdate xmlns:D="DAV:"` +
  747. ` xmlns:Z="http://ns.example.com/z/">` +
  748. ` <D:remove>` +
  749. ` <D:prop>` +
  750. ` <Z:Authors>` +
  751. ` <Z:Author>Jim Whitehead</Z:Author>` +
  752. ` </Z:Authors>` +
  753. ` </D:prop>` +
  754. ` </D:remove>` +
  755. `</D:propertyupdate>`,
  756. wantStatus: http.StatusBadRequest,
  757. }, {
  758. desc: "bad: empty propertyupdate",
  759. input: `` +
  760. `<?xml version="1.0" encoding="utf-8" ?>` +
  761. `<D:propertyupdate xmlns:D="DAV:"` +
  762. `</D:propertyupdate>`,
  763. wantStatus: http.StatusBadRequest,
  764. }, {
  765. desc: "bad: empty prop",
  766. input: `` +
  767. `<?xml version="1.0" encoding="utf-8" ?>` +
  768. `<D:propertyupdate xmlns:D="DAV:"` +
  769. ` xmlns:Z="http://ns.example.com/z/">` +
  770. ` <D:remove>` +
  771. ` <D:prop/>` +
  772. ` </D:remove>` +
  773. `</D:propertyupdate>`,
  774. wantStatus: http.StatusBadRequest,
  775. }}
  776. for _, tc := range testCases {
  777. pp, status, err := readProppatch(strings.NewReader(tc.input))
  778. if tc.wantStatus != 0 {
  779. if err == nil {
  780. t.Errorf("%s: got nil error, want non-nil", tc.desc)
  781. continue
  782. }
  783. } else if err != nil {
  784. t.Errorf("%s: %v", tc.desc, err)
  785. continue
  786. }
  787. if status != tc.wantStatus {
  788. t.Errorf("%s: got status %d, want %d", tc.desc, status, tc.wantStatus)
  789. continue
  790. }
  791. if !reflect.DeepEqual(pp, tc.wantPP) || status != tc.wantStatus {
  792. t.Errorf("%s: proppatch\ngot %v\nwant %v", tc.desc, pp, tc.wantPP)
  793. }
  794. }
  795. }