xml_test.go 25 KB

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