xml_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. "fmt"
  8. "io"
  9. "net/http"
  10. "net/http/httptest"
  11. "reflect"
  12. "sort"
  13. "strings"
  14. "testing"
  15. ixml "golang.org/x/net/webdav/internal/xml"
  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: ixml.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: ixml.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: ixml.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: ixml.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: ixml.Name{Space: "DAV:", Local: "propfind"},
  172. Allprop: new(struct{}),
  173. Include: propfindProps{ixml.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: ixml.Name{Space: "DAV:", Local: "propfind"},
  184. Allprop: new(struct{}),
  185. Include: propfindProps{ixml.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: ixml.Name{Space: "DAV:", Local: "propfind"},
  195. Prop: propfindProps{ixml.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: ixml.Name{Space: "DAV:", Local: "propfind"},
  208. Prop: propfindProps{ixml.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: ixml.Name{Space: "DAV:", Local: "propfind"},
  218. Prop: propfindProps{ixml.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: ixml.Name{Space: "DAV:", Local: "propfind"},
  228. Prop: propfindProps{ixml.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: ixml.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. ///The "section x.y.z" test cases come from section x.y.z of the spec at
  344. // http://www.webdav.org/specs/rfc4918.html
  345. testCases := []struct {
  346. desc string
  347. responses []response
  348. respdesc string
  349. writeHeader bool
  350. wantXML string
  351. wantCode int
  352. wantErr error
  353. }{{
  354. desc: "section 9.2.2 (failed dependency)",
  355. responses: []response{{
  356. Href: []string{"http://example.com/foo"},
  357. Propstat: []propstat{{
  358. Prop: []Property{{
  359. XMLName: ixml.Name{
  360. Space: "http://ns.example.com/",
  361. Local: "Authors",
  362. },
  363. }},
  364. Status: "HTTP/1.1 424 Failed Dependency",
  365. }, {
  366. Prop: []Property{{
  367. XMLName: ixml.Name{
  368. Space: "http://ns.example.com/",
  369. Local: "Copyright-Owner",
  370. },
  371. }},
  372. Status: "HTTP/1.1 409 Conflict",
  373. }},
  374. ResponseDescription: "Copyright Owner cannot be deleted or altered.",
  375. }},
  376. wantXML: `` +
  377. `<?xml version="1.0" encoding="UTF-8"?>` +
  378. `<multistatus xmlns="DAV:">` +
  379. ` <response>` +
  380. ` <href>http://example.com/foo</href>` +
  381. ` <propstat>` +
  382. ` <prop>` +
  383. ` <Authors xmlns="http://ns.example.com/"></Authors>` +
  384. ` </prop>` +
  385. ` <status>HTTP/1.1 424 Failed Dependency</status>` +
  386. ` </propstat>` +
  387. ` <propstat xmlns="DAV:">` +
  388. ` <prop>` +
  389. ` <Copyright-Owner xmlns="http://ns.example.com/"></Copyright-Owner>` +
  390. ` </prop>` +
  391. ` <status>HTTP/1.1 409 Conflict</status>` +
  392. ` </propstat>` +
  393. ` <responsedescription>Copyright Owner cannot be deleted or altered.</responsedescription>` +
  394. `</response>` +
  395. `</multistatus>`,
  396. wantCode: StatusMulti,
  397. }, {
  398. desc: "section 9.6.2 (lock-token-submitted)",
  399. responses: []response{{
  400. Href: []string{"http://example.com/foo"},
  401. Status: "HTTP/1.1 423 Locked",
  402. Error: &xmlError{
  403. InnerXML: []byte(`<lock-token-submitted xmlns="DAV:"/>`),
  404. },
  405. }},
  406. wantXML: `` +
  407. `<?xml version="1.0" encoding="UTF-8"?>` +
  408. `<multistatus xmlns="DAV:">` +
  409. ` <response>` +
  410. ` <href>http://example.com/foo</href>` +
  411. ` <status>HTTP/1.1 423 Locked</status>` +
  412. ` <error><lock-token-submitted xmlns="DAV:"/></error>` +
  413. ` </response>` +
  414. `</multistatus>`,
  415. wantCode: StatusMulti,
  416. }, {
  417. desc: "section 9.1.3",
  418. responses: []response{{
  419. Href: []string{"http://example.com/foo"},
  420. Propstat: []propstat{{
  421. Prop: []Property{{
  422. XMLName: ixml.Name{Space: "http://ns.example.com/boxschema/", Local: "bigbox"},
  423. InnerXML: []byte(`` +
  424. `<BoxType xmlns="http://ns.example.com/boxschema/">` +
  425. `Box type A` +
  426. `</BoxType>`),
  427. }, {
  428. XMLName: ixml.Name{Space: "http://ns.example.com/boxschema/", Local: "author"},
  429. InnerXML: []byte(`` +
  430. `<Name xmlns="http://ns.example.com/boxschema/">` +
  431. `J.J. Johnson` +
  432. `</Name>`),
  433. }},
  434. Status: "HTTP/1.1 200 OK",
  435. }, {
  436. Prop: []Property{{
  437. XMLName: ixml.Name{Space: "http://ns.example.com/boxschema/", Local: "DingALing"},
  438. }, {
  439. XMLName: ixml.Name{Space: "http://ns.example.com/boxschema/", Local: "Random"},
  440. }},
  441. Status: "HTTP/1.1 403 Forbidden",
  442. ResponseDescription: "The user does not have access to the DingALing property.",
  443. }},
  444. }},
  445. respdesc: "There has been an access violation error.",
  446. wantXML: `` +
  447. `<?xml version="1.0" encoding="UTF-8"?>` +
  448. `<multistatus xmlns="DAV:" xmlns:B="http://ns.example.com/boxschema/">` +
  449. ` <response>` +
  450. ` <href>http://example.com/foo</href>` +
  451. ` <propstat>` +
  452. ` <prop>` +
  453. ` <B:bigbox><B:BoxType>Box type A</B:BoxType></B:bigbox>` +
  454. ` <B:author><B:Name>J.J. Johnson</B:Name></B:author>` +
  455. ` </prop>` +
  456. ` <status>HTTP/1.1 200 OK</status>` +
  457. ` </propstat>` +
  458. ` <propstat>` +
  459. ` <prop>` +
  460. ` <B:DingALing/>` +
  461. ` <B:Random/>` +
  462. ` </prop>` +
  463. ` <status>HTTP/1.1 403 Forbidden</status>` +
  464. ` <responsedescription>The user does not have access to the DingALing property.</responsedescription>` +
  465. ` </propstat>` +
  466. ` </response>` +
  467. ` <responsedescription>There has been an access violation error.</responsedescription>` +
  468. `</multistatus>`,
  469. wantCode: StatusMulti,
  470. }, {
  471. desc: "no response written",
  472. // default of http.responseWriter
  473. wantCode: http.StatusOK,
  474. }, {
  475. desc: "no response written (with description)",
  476. respdesc: "too bad",
  477. // default of http.responseWriter
  478. wantCode: http.StatusOK,
  479. }, {
  480. desc: "empty multistatus with header",
  481. writeHeader: true,
  482. wantXML: `<multistatus xmlns="DAV:"></multistatus>`,
  483. wantCode: StatusMulti,
  484. }, {
  485. desc: "bad: no href",
  486. responses: []response{{
  487. Propstat: []propstat{{
  488. Prop: []Property{{
  489. XMLName: ixml.Name{
  490. Space: "http://example.com/",
  491. Local: "foo",
  492. },
  493. }},
  494. Status: "HTTP/1.1 200 OK",
  495. }},
  496. }},
  497. wantErr: errInvalidResponse,
  498. // default of http.responseWriter
  499. wantCode: http.StatusOK,
  500. }, {
  501. desc: "bad: multiple hrefs and no status",
  502. responses: []response{{
  503. Href: []string{"http://example.com/foo", "http://example.com/bar"},
  504. }},
  505. wantErr: errInvalidResponse,
  506. // default of http.responseWriter
  507. wantCode: http.StatusOK,
  508. }, {
  509. desc: "bad: one href and no propstat",
  510. responses: []response{{
  511. Href: []string{"http://example.com/foo"},
  512. }},
  513. wantErr: errInvalidResponse,
  514. // default of http.responseWriter
  515. wantCode: http.StatusOK,
  516. }, {
  517. desc: "bad: status with one href and propstat",
  518. responses: []response{{
  519. Href: []string{"http://example.com/foo"},
  520. Propstat: []propstat{{
  521. Prop: []Property{{
  522. XMLName: ixml.Name{
  523. Space: "http://example.com/",
  524. Local: "foo",
  525. },
  526. }},
  527. Status: "HTTP/1.1 200 OK",
  528. }},
  529. Status: "HTTP/1.1 200 OK",
  530. }},
  531. wantErr: errInvalidResponse,
  532. // default of http.responseWriter
  533. wantCode: http.StatusOK,
  534. }, {
  535. desc: "bad: multiple hrefs and propstat",
  536. responses: []response{{
  537. Href: []string{
  538. "http://example.com/foo",
  539. "http://example.com/bar",
  540. },
  541. Propstat: []propstat{{
  542. Prop: []Property{{
  543. XMLName: ixml.Name{
  544. Space: "http://example.com/",
  545. Local: "foo",
  546. },
  547. }},
  548. Status: "HTTP/1.1 200 OK",
  549. }},
  550. }},
  551. wantErr: errInvalidResponse,
  552. // default of http.responseWriter
  553. wantCode: http.StatusOK,
  554. }}
  555. n := xmlNormalizer{omitWhitespace: true}
  556. loop:
  557. for _, tc := range testCases {
  558. rec := httptest.NewRecorder()
  559. w := multistatusWriter{w: rec, responseDescription: tc.respdesc}
  560. if tc.writeHeader {
  561. if err := w.writeHeader(); err != nil {
  562. t.Errorf("%s: got writeHeader error %v, want nil", tc.desc, err)
  563. continue
  564. }
  565. }
  566. for _, r := range tc.responses {
  567. if err := w.write(&r); err != nil {
  568. if err != tc.wantErr {
  569. t.Errorf("%s: got write error %v, want %v",
  570. tc.desc, err, tc.wantErr)
  571. }
  572. continue loop
  573. }
  574. }
  575. if err := w.close(); err != tc.wantErr {
  576. t.Errorf("%s: got close error %v, want %v",
  577. tc.desc, err, tc.wantErr)
  578. continue
  579. }
  580. if rec.Code != tc.wantCode {
  581. t.Errorf("%s: got HTTP status code %d, want %d\n",
  582. tc.desc, rec.Code, tc.wantCode)
  583. continue
  584. }
  585. gotXML := rec.Body.String()
  586. eq, err := n.equalXML(strings.NewReader(gotXML), strings.NewReader(tc.wantXML))
  587. if err != nil {
  588. t.Errorf("%s: equalXML: %v", tc.desc, err)
  589. continue
  590. }
  591. if !eq {
  592. t.Errorf("%s: XML body\ngot %s\nwant %s", tc.desc, gotXML, tc.wantXML)
  593. }
  594. }
  595. }
  596. func TestReadProppatch(t *testing.T) {
  597. ppStr := func(pps []Proppatch) string {
  598. var outer []string
  599. for _, pp := range pps {
  600. var inner []string
  601. for _, p := range pp.Props {
  602. inner = append(inner, fmt.Sprintf("{XMLName: %q, Lang: %q, InnerXML: %q}",
  603. p.XMLName, p.Lang, p.InnerXML))
  604. }
  605. outer = append(outer, fmt.Sprintf("{Remove: %t, Props: [%s]}",
  606. pp.Remove, strings.Join(inner, ", ")))
  607. }
  608. return "[" + strings.Join(outer, ", ") + "]"
  609. }
  610. testCases := []struct {
  611. desc string
  612. input string
  613. wantPP []Proppatch
  614. wantStatus int
  615. }{{
  616. desc: "proppatch: section 9.2 (with simple property value)",
  617. input: `` +
  618. `<?xml version="1.0" encoding="utf-8" ?>` +
  619. `<D:propertyupdate xmlns:D="DAV:"` +
  620. ` xmlns:Z="http://ns.example.com/z/">` +
  621. ` <D:set>` +
  622. ` <D:prop><Z:Authors>somevalue</Z:Authors></D:prop>` +
  623. ` </D:set>` +
  624. ` <D:remove>` +
  625. ` <D:prop><Z:Copyright-Owner/></D:prop>` +
  626. ` </D:remove>` +
  627. `</D:propertyupdate>`,
  628. wantPP: []Proppatch{{
  629. Props: []Property{{
  630. ixml.Name{Space: "http://ns.example.com/z/", Local: "Authors"},
  631. "",
  632. []byte(`somevalue`),
  633. }},
  634. }, {
  635. Remove: true,
  636. Props: []Property{{
  637. ixml.Name{Space: "http://ns.example.com/z/", Local: "Copyright-Owner"},
  638. "",
  639. nil,
  640. }},
  641. }},
  642. }, {
  643. desc: "proppatch: lang attribute on prop",
  644. input: `` +
  645. `<?xml version="1.0" encoding="utf-8" ?>` +
  646. `<D:propertyupdate xmlns:D="DAV:">` +
  647. ` <D:set>` +
  648. ` <D:prop xml:lang="en">` +
  649. ` <foo xmlns="http://example.com/ns"/>` +
  650. ` </D:prop>` +
  651. ` </D:set>` +
  652. `</D:propertyupdate>`,
  653. wantPP: []Proppatch{{
  654. Props: []Property{{
  655. ixml.Name{Space: "http://example.com/ns", Local: "foo"},
  656. "en",
  657. nil,
  658. }},
  659. }},
  660. }, {
  661. desc: "bad: remove with value",
  662. input: `` +
  663. `<?xml version="1.0" encoding="utf-8" ?>` +
  664. `<D:propertyupdate xmlns:D="DAV:"` +
  665. ` xmlns:Z="http://ns.example.com/z/">` +
  666. ` <D:remove>` +
  667. ` <D:prop>` +
  668. ` <Z:Authors>` +
  669. ` <Z:Author>Jim Whitehead</Z:Author>` +
  670. ` </Z:Authors>` +
  671. ` </D:prop>` +
  672. ` </D:remove>` +
  673. `</D:propertyupdate>`,
  674. wantStatus: http.StatusBadRequest,
  675. }, {
  676. desc: "bad: empty propertyupdate",
  677. input: `` +
  678. `<?xml version="1.0" encoding="utf-8" ?>` +
  679. `<D:propertyupdate xmlns:D="DAV:"` +
  680. `</D:propertyupdate>`,
  681. wantStatus: http.StatusBadRequest,
  682. }, {
  683. desc: "bad: empty prop",
  684. input: `` +
  685. `<?xml version="1.0" encoding="utf-8" ?>` +
  686. `<D:propertyupdate xmlns:D="DAV:"` +
  687. ` xmlns:Z="http://ns.example.com/z/">` +
  688. ` <D:remove>` +
  689. ` <D:prop/>` +
  690. ` </D:remove>` +
  691. `</D:propertyupdate>`,
  692. wantStatus: http.StatusBadRequest,
  693. }}
  694. for _, tc := range testCases {
  695. pp, status, err := readProppatch(strings.NewReader(tc.input))
  696. if tc.wantStatus != 0 {
  697. if err == nil {
  698. t.Errorf("%s: got nil error, want non-nil", tc.desc)
  699. continue
  700. }
  701. } else if err != nil {
  702. t.Errorf("%s: %v", tc.desc, err)
  703. continue
  704. }
  705. if status != tc.wantStatus {
  706. t.Errorf("%s: got status %d, want %d", tc.desc, status, tc.wantStatus)
  707. continue
  708. }
  709. if !reflect.DeepEqual(pp, tc.wantPP) || status != tc.wantStatus {
  710. t.Errorf("%s: proppatch\ngot %v\nwant %v", tc.desc, ppStr(pp), ppStr(tc.wantPP))
  711. }
  712. }
  713. }
  714. func TestUnmarshalXMLValue(t *testing.T) {
  715. testCases := []struct {
  716. desc string
  717. input string
  718. wantVal string
  719. }{{
  720. desc: "simple char data",
  721. input: "<root>foo</root>",
  722. wantVal: "foo",
  723. }, {
  724. desc: "empty element",
  725. input: "<root><foo/></root>",
  726. wantVal: "<foo/>",
  727. }, {
  728. desc: "preserve namespace",
  729. input: `<root><foo xmlns="bar"/></root>`,
  730. wantVal: `<foo xmlns="bar"/>`,
  731. }, {
  732. desc: "preserve root element namespace",
  733. input: `<root xmlns:bar="bar"><bar:foo/></root>`,
  734. wantVal: `<foo xmlns="bar"/>`,
  735. }, {
  736. desc: "preserve whitespace",
  737. input: "<root> \t </root>",
  738. wantVal: " \t ",
  739. }, {
  740. desc: "preserve mixed content",
  741. input: `<root xmlns="bar"> <foo>a<bam xmlns="baz"/> </foo> </root>`,
  742. wantVal: ` <foo xmlns="bar">a<bam xmlns="baz"/> </foo> `,
  743. }, {
  744. desc: "section 9.2",
  745. input: `` +
  746. `<Z:Authors xmlns:Z="http://ns.example.com/z/">` +
  747. ` <Z:Author>Jim Whitehead</Z:Author>` +
  748. ` <Z:Author>Roy Fielding</Z:Author>` +
  749. `</Z:Authors>`,
  750. wantVal: `` +
  751. ` <Author xmlns="http://ns.example.com/z/">Jim Whitehead</Author>` +
  752. ` <Author xmlns="http://ns.example.com/z/">Roy Fielding</Author>`,
  753. }, {
  754. desc: "section 4.3.1 (mixed content)",
  755. input: `` +
  756. `<x:author ` +
  757. ` xmlns:x='http://example.com/ns' ` +
  758. ` xmlns:D="DAV:">` +
  759. ` <x:name>Jane Doe</x:name>` +
  760. ` <!-- Jane's contact info -->` +
  761. ` <x:uri type='email'` +
  762. ` added='2005-11-26'>mailto:jane.doe@example.com</x:uri>` +
  763. ` <x:uri type='web'` +
  764. ` added='2005-11-27'>http://www.example.com</x:uri>` +
  765. ` <x:notes xmlns:h='http://www.w3.org/1999/xhtml'>` +
  766. ` Jane has been working way <h:em>too</h:em> long on the` +
  767. ` long-awaited revision of <![CDATA[<RFC2518>]]>.` +
  768. ` </x:notes>` +
  769. `</x:author>`,
  770. wantVal: `` +
  771. ` <name xmlns="http://example.com/ns">Jane Doe</name>` +
  772. ` ` +
  773. ` <uri type='email'` +
  774. ` xmlns="http://example.com/ns" ` +
  775. ` added='2005-11-26'>mailto:jane.doe@example.com</uri>` +
  776. ` <uri added='2005-11-27'` +
  777. ` type='web'` +
  778. ` xmlns="http://example.com/ns">http://www.example.com</uri>` +
  779. ` <notes xmlns="http://example.com/ns" ` +
  780. ` xmlns:h="http://www.w3.org/1999/xhtml">` +
  781. ` Jane has been working way <h:em>too</h:em> long on the` +
  782. ` long-awaited revision of &lt;RFC2518&gt;.` +
  783. ` </notes>`,
  784. }}
  785. var n xmlNormalizer
  786. for _, tc := range testCases {
  787. d := ixml.NewDecoder(strings.NewReader(tc.input))
  788. var v xmlValue
  789. if err := d.Decode(&v); err != nil {
  790. t.Errorf("%s: got error %v, want nil", tc.desc, err)
  791. continue
  792. }
  793. eq, err := n.equalXML(bytes.NewReader(v), strings.NewReader(tc.wantVal))
  794. if err != nil {
  795. t.Errorf("%s: equalXML: %v", tc.desc, err)
  796. continue
  797. }
  798. if !eq {
  799. t.Errorf("%s:\ngot %s\nwant %s", tc.desc, string(v), tc.wantVal)
  800. }
  801. }
  802. }
  803. // xmlNormalizer normalizes XML.
  804. type xmlNormalizer struct {
  805. // omitWhitespace instructs to ignore whitespace between element tags.
  806. omitWhitespace bool
  807. // omitComments instructs to ignore XML comments.
  808. omitComments bool
  809. }
  810. // normalize writes the normalized XML content of r to w. It applies the
  811. // following rules
  812. //
  813. // * Rename namespace prefixes according to an internal heuristic.
  814. // * Remove unnecessary namespace declarations.
  815. // * Sort attributes in XML start elements in lexical order of their
  816. // fully qualified name.
  817. // * Remove XML directives and processing instructions.
  818. // * Remove CDATA between XML tags that only contains whitespace, if
  819. // instructed to do so.
  820. // * Remove comments, if instructed to do so.
  821. //
  822. func (n *xmlNormalizer) normalize(w io.Writer, r io.Reader) error {
  823. d := ixml.NewDecoder(r)
  824. e := ixml.NewEncoder(w)
  825. for {
  826. t, err := d.Token()
  827. if err != nil {
  828. if t == nil && err == io.EOF {
  829. break
  830. }
  831. return err
  832. }
  833. switch val := t.(type) {
  834. case ixml.Directive, ixml.ProcInst:
  835. continue
  836. case ixml.Comment:
  837. if n.omitComments {
  838. continue
  839. }
  840. case ixml.CharData:
  841. if n.omitWhitespace && len(bytes.TrimSpace(val)) == 0 {
  842. continue
  843. }
  844. case ixml.StartElement:
  845. start, _ := ixml.CopyToken(val).(ixml.StartElement)
  846. attr := start.Attr[:0]
  847. for _, a := range start.Attr {
  848. if a.Name.Space == "xmlns" || a.Name.Local == "xmlns" {
  849. continue
  850. }
  851. attr = append(attr, a)
  852. }
  853. sort.Sort(byName(attr))
  854. start.Attr = attr
  855. t = start
  856. }
  857. err = e.EncodeToken(t)
  858. if err != nil {
  859. return err
  860. }
  861. }
  862. return e.Flush()
  863. }
  864. // equalXML tests for equality of the normalized XML contents of a and b.
  865. func (n *xmlNormalizer) equalXML(a, b io.Reader) (bool, error) {
  866. var buf bytes.Buffer
  867. if err := n.normalize(&buf, a); err != nil {
  868. return false, err
  869. }
  870. normA := buf.String()
  871. buf.Reset()
  872. if err := n.normalize(&buf, b); err != nil {
  873. return false, err
  874. }
  875. normB := buf.String()
  876. return normA == normB, nil
  877. }
  878. type byName []ixml.Attr
  879. func (a byName) Len() int { return len(a) }
  880. func (a byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  881. func (a byName) Less(i, j int) bool {
  882. if a[i].Name.Space != a[j].Name.Space {
  883. return a[i].Name.Space < a[j].Name.Space
  884. }
  885. return a[i].Name.Local < a[j].Name.Local
  886. }