xml_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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: propnames{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: propnames{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: propnames{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: propnames{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: propnames{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: propnames{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. wantXML string
  348. wantCode int
  349. wantErr error
  350. }{{
  351. desc: "section 9.2.2 (failed dependency)",
  352. responses: []response{{
  353. Href: []string{"http://example.com/foo"},
  354. Propstat: []propstat{{
  355. Prop: []Property{{
  356. XMLName: xml.Name{
  357. Space: "http://ns.example.com/",
  358. Local: "Authors",
  359. },
  360. }},
  361. Status: "HTTP/1.1 424 Failed Dependency",
  362. }, {
  363. Prop: []Property{{
  364. XMLName: xml.Name{
  365. Space: "http://ns.example.com/",
  366. Local: "Copyright-Owner",
  367. },
  368. }},
  369. Status: "HTTP/1.1 409 Conflict",
  370. }},
  371. ResponseDescription: "Copyright Owner cannot be deleted or altered.",
  372. }},
  373. wantXML: `` +
  374. `<?xml version="1.0" encoding="UTF-8"?>` +
  375. `<multistatus xmlns="DAV:">` +
  376. ` <response>` +
  377. ` <href>http://example.com/foo</href>` +
  378. ` <propstat>` +
  379. ` <prop>` +
  380. ` <Authors xmlns="http://ns.example.com/"></Authors>` +
  381. ` </prop>` +
  382. ` <status>HTTP/1.1 424 Failed Dependency</status>` +
  383. ` </propstat>` +
  384. ` <propstat xmlns="DAV:">` +
  385. ` <prop>` +
  386. ` <Copyright-Owner xmlns="http://ns.example.com/"></Copyright-Owner>` +
  387. ` </prop>` +
  388. ` <status>HTTP/1.1 409 Conflict</status>` +
  389. ` </propstat>` +
  390. ` <responsedescription>Copyright Owner cannot be deleted or altered.</responsedescription>` +
  391. `</response>` +
  392. `</multistatus>`,
  393. wantCode: StatusMulti,
  394. }, {
  395. desc: "section 9.6.2 (lock-token-submitted)",
  396. responses: []response{{
  397. Href: []string{"http://example.com/foo"},
  398. Status: "HTTP/1.1 423 Locked",
  399. Error: &xmlError{
  400. InnerXML: []byte(`<lock-token-submitted xmlns="DAV:"/>`),
  401. },
  402. }},
  403. wantXML: `` +
  404. `<?xml version="1.0" encoding="UTF-8"?>` +
  405. `<multistatus xmlns="DAV:">` +
  406. ` <response>` +
  407. ` <href>http://example.com/foo</href>` +
  408. ` <status>HTTP/1.1 423 Locked</status>` +
  409. ` <error><lock-token-submitted xmlns="DAV:"/></error>` +
  410. ` </response>` +
  411. `</multistatus>`,
  412. wantCode: StatusMulti,
  413. }, {
  414. desc: "section 9.1.3",
  415. responses: []response{{
  416. Href: []string{"http://example.com/foo"},
  417. Propstat: []propstat{{
  418. Prop: []Property{{
  419. XMLName: xml.Name{Space: "http://ns.example.com/boxschema/", Local: "bigbox"},
  420. InnerXML: []byte(`` +
  421. `<BoxType xmlns="http://ns.example.com/boxschema/">` +
  422. `Box type A` +
  423. `</BoxType>`),
  424. }, {
  425. XMLName: xml.Name{Space: "http://ns.example.com/boxschema/", Local: "author"},
  426. InnerXML: []byte(`` +
  427. `<Name xmlns="http://ns.example.com/boxschema/">` +
  428. `J.J. Johnson` +
  429. `</Name>`),
  430. }},
  431. Status: "HTTP/1.1 200 OK",
  432. }, {
  433. Prop: []Property{{
  434. XMLName: xml.Name{Space: "http://ns.example.com/boxschema/", Local: "DingALing"},
  435. }, {
  436. XMLName: xml.Name{Space: "http://ns.example.com/boxschema/", Local: "Random"},
  437. }},
  438. Status: "HTTP/1.1 403 Forbidden",
  439. ResponseDescription: "The user does not have access to the DingALing property.",
  440. }},
  441. }},
  442. respdesc: "There has been an access violation error.",
  443. wantXML: `` +
  444. `<?xml version="1.0" encoding="UTF-8"?>` +
  445. `<multistatus xmlns="DAV:">` +
  446. ` <response>` +
  447. ` <href>http://example.com/foo</href>` +
  448. ` <propstat>` +
  449. ` <prop>` +
  450. ` <bigbox xmlns="http://ns.example.com/boxschema/"><BoxType xmlns="http://ns.example.com/boxschema/">Box type A</BoxType></bigbox>` +
  451. ` <author xmlns="http://ns.example.com/boxschema/"><Name xmlns="http://ns.example.com/boxschema/">J.J. Johnson</Name></author>` +
  452. ` </prop>` +
  453. ` <status>HTTP/1.1 200 OK</status>` +
  454. ` </propstat>` +
  455. ` <propstat>` +
  456. ` <prop>` +
  457. ` <DingALing xmlns="http://ns.example.com/boxschema/"></DingALing>` +
  458. ` <Random xmlns="http://ns.example.com/boxschema/"></Random>` +
  459. ` </prop>` +
  460. ` <status>HTTP/1.1 403 Forbidden</status>` +
  461. ` <responsedescription>The user does not have access to the DingALing property.</responsedescription>` +
  462. ` </propstat>` +
  463. ` </response>` +
  464. ` <responsedescription>There has been an access violation error.</responsedescription>` +
  465. `</multistatus>`,
  466. wantCode: StatusMulti,
  467. }, {
  468. desc: "bad: no response written",
  469. // default of http.responseWriter
  470. wantCode: http.StatusOK,
  471. }, {
  472. desc: "bad: no response written (with description)",
  473. respdesc: "too bad",
  474. // default of http.responseWriter
  475. wantCode: http.StatusOK,
  476. }, {
  477. desc: "bad: no href",
  478. responses: []response{{
  479. Propstat: []propstat{{
  480. Prop: []Property{{
  481. XMLName: xml.Name{
  482. Space: "http://example.com/",
  483. Local: "foo",
  484. },
  485. }},
  486. Status: "HTTP/1.1 200 OK",
  487. }},
  488. }},
  489. wantErr: errInvalidResponse,
  490. // default of http.responseWriter
  491. wantCode: http.StatusOK,
  492. }, {
  493. desc: "bad: multiple hrefs and no status",
  494. responses: []response{{
  495. Href: []string{"http://example.com/foo", "http://example.com/bar"},
  496. }},
  497. wantErr: errInvalidResponse,
  498. // default of http.responseWriter
  499. wantCode: http.StatusOK,
  500. }, {
  501. desc: "bad: one href and no propstat",
  502. responses: []response{{
  503. Href: []string{"http://example.com/foo"},
  504. }},
  505. wantErr: errInvalidResponse,
  506. // default of http.responseWriter
  507. wantCode: http.StatusOK,
  508. }, {
  509. desc: "bad: status with one href and propstat",
  510. responses: []response{{
  511. Href: []string{"http://example.com/foo"},
  512. Propstat: []propstat{{
  513. Prop: []Property{{
  514. XMLName: xml.Name{
  515. Space: "http://example.com/",
  516. Local: "foo",
  517. },
  518. }},
  519. Status: "HTTP/1.1 200 OK",
  520. }},
  521. Status: "HTTP/1.1 200 OK",
  522. }},
  523. wantErr: errInvalidResponse,
  524. // default of http.responseWriter
  525. wantCode: http.StatusOK,
  526. }, {
  527. desc: "bad: multiple hrefs and propstat",
  528. responses: []response{{
  529. Href: []string{
  530. "http://example.com/foo",
  531. "http://example.com/bar",
  532. },
  533. Propstat: []propstat{{
  534. Prop: []Property{{
  535. XMLName: xml.Name{
  536. Space: "http://example.com/",
  537. Local: "foo",
  538. },
  539. }},
  540. Status: "HTTP/1.1 200 OK",
  541. }},
  542. }},
  543. wantErr: errInvalidResponse,
  544. // default of http.responseWriter
  545. wantCode: http.StatusOK,
  546. }}
  547. loop:
  548. for _, tc := range testCases {
  549. rec := httptest.NewRecorder()
  550. w := multistatusWriter{w: rec, responseDescription: tc.respdesc}
  551. for _, r := range tc.responses {
  552. if err := w.write(&r); err != nil {
  553. if err != tc.wantErr {
  554. t.Errorf("%s: got write error %v, want %v",
  555. tc.desc, err, tc.wantErr)
  556. }
  557. continue loop
  558. }
  559. }
  560. if err := w.close(); err != tc.wantErr {
  561. t.Errorf("%s: got close error %v, want %v",
  562. tc.desc, err, tc.wantErr)
  563. continue
  564. }
  565. if rec.Code != tc.wantCode {
  566. t.Errorf("%s: got HTTP status code %d, want %d\n",
  567. tc.desc, rec.Code, tc.wantCode)
  568. continue
  569. }
  570. // normalize returns the normalized XML content of s. In contrast to
  571. // the WebDAV specification, it ignores whitespace within property
  572. // values of mixed XML content.
  573. normalize := func(s string) string {
  574. d := xml.NewDecoder(strings.NewReader(s))
  575. var b bytes.Buffer
  576. e := xml.NewEncoder(&b)
  577. for {
  578. tok, err := d.Token()
  579. if err != nil {
  580. if err == io.EOF {
  581. break
  582. }
  583. t.Fatalf("%s: Token %v", tc.desc, err)
  584. }
  585. switch val := tok.(type) {
  586. case xml.Comment, xml.Directive, xml.ProcInst:
  587. continue
  588. case xml.CharData:
  589. if len(bytes.TrimSpace(val)) == 0 {
  590. continue
  591. }
  592. }
  593. if err := e.EncodeToken(tok); err != nil {
  594. t.Fatalf("%s: EncodeToken: %v", tc.desc, err)
  595. }
  596. }
  597. if err := e.Flush(); err != nil {
  598. t.Fatalf("%s: Flush: %v", tc.desc, err)
  599. }
  600. return b.String()
  601. }
  602. gotXML := normalize(rec.Body.String())
  603. wantXML := normalize(tc.wantXML)
  604. if gotXML != wantXML {
  605. t.Errorf("%s: XML body\ngot %q\nwant %q", tc.desc, gotXML, wantXML)
  606. }
  607. }
  608. }