xml_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. "encoding/xml"
  7. "net/http"
  8. "reflect"
  9. "strings"
  10. "testing"
  11. )
  12. func TestParseLockInfo(t *testing.T) {
  13. // The "section x.y.z" test cases come from section x.y.z of the spec at
  14. // http://www.webdav.org/specs/rfc4918.html
  15. testCases := []struct {
  16. desc string
  17. input string
  18. wantLI lockInfo
  19. wantStatus int
  20. }{{
  21. "bad: junk",
  22. "xxx",
  23. lockInfo{},
  24. http.StatusBadRequest,
  25. }, {
  26. "bad: invalid owner XML",
  27. "" +
  28. "<D:lockinfo xmlns:D='DAV:'>\n" +
  29. " <D:lockscope><D:exclusive/></D:lockscope>\n" +
  30. " <D:locktype><D:write/></D:locktype>\n" +
  31. " <D:owner>\n" +
  32. " <D:href> no end tag \n" +
  33. " </D:owner>\n" +
  34. "</D:lockinfo>",
  35. lockInfo{},
  36. http.StatusBadRequest,
  37. }, {
  38. "bad: invalid UTF-8",
  39. "" +
  40. "<D:lockinfo xmlns:D='DAV:'>\n" +
  41. " <D:lockscope><D:exclusive/></D:lockscope>\n" +
  42. " <D:locktype><D:write/></D:locktype>\n" +
  43. " <D:owner>\n" +
  44. " <D:href> \xff </D:href>\n" +
  45. " </D:owner>\n" +
  46. "</D:lockinfo>",
  47. lockInfo{},
  48. http.StatusBadRequest,
  49. }, {
  50. "bad: unfinished XML #1",
  51. "" +
  52. "<D:lockinfo xmlns:D='DAV:'>\n" +
  53. " <D:lockscope><D:exclusive/></D:lockscope>\n" +
  54. " <D:locktype><D:write/></D:locktype>\n",
  55. lockInfo{},
  56. http.StatusBadRequest,
  57. }, {
  58. "bad: unfinished XML #2",
  59. "" +
  60. "<D:lockinfo xmlns:D='DAV:'>\n" +
  61. " <D:lockscope><D:exclusive/></D:lockscope>\n" +
  62. " <D:locktype><D:write/></D:locktype>\n" +
  63. " <D:owner>\n",
  64. lockInfo{},
  65. http.StatusBadRequest,
  66. }, {
  67. "good: empty",
  68. "",
  69. lockInfo{},
  70. 0,
  71. }, {
  72. "good: plain-text owner",
  73. "" +
  74. "<D:lockinfo xmlns:D='DAV:'>\n" +
  75. " <D:lockscope><D:exclusive/></D:lockscope>\n" +
  76. " <D:locktype><D:write/></D:locktype>\n" +
  77. " <D:owner>gopher</D:owner>\n" +
  78. "</D:lockinfo>",
  79. lockInfo{
  80. XMLName: xml.Name{Space: "DAV:", Local: "lockinfo"},
  81. Exclusive: new(struct{}),
  82. Write: new(struct{}),
  83. Owner: owner{
  84. InnerXML: "gopher",
  85. },
  86. },
  87. 0,
  88. }, {
  89. "section 9.10.7",
  90. "" +
  91. "<D:lockinfo xmlns:D='DAV:'>\n" +
  92. " <D:lockscope><D:exclusive/></D:lockscope>\n" +
  93. " <D:locktype><D:write/></D:locktype>\n" +
  94. " <D:owner>\n" +
  95. " <D:href>http://example.org/~ejw/contact.html</D:href>\n" +
  96. " </D:owner>\n" +
  97. "</D:lockinfo>",
  98. lockInfo{
  99. XMLName: xml.Name{Space: "DAV:", Local: "lockinfo"},
  100. Exclusive: new(struct{}),
  101. Write: new(struct{}),
  102. Owner: owner{
  103. InnerXML: "\n <D:href>http://example.org/~ejw/contact.html</D:href>\n ",
  104. },
  105. },
  106. 0,
  107. }}
  108. for _, tc := range testCases {
  109. li, status, err := readLockInfo(strings.NewReader(tc.input))
  110. if tc.wantStatus != 0 {
  111. if err == nil {
  112. t.Errorf("%s: got nil error, want non-nil", tc.desc)
  113. continue
  114. }
  115. } else if err != nil {
  116. t.Errorf("%s: %v", tc.desc, err)
  117. continue
  118. }
  119. if !reflect.DeepEqual(li, tc.wantLI) || status != tc.wantStatus {
  120. t.Errorf("%s:\ngot lockInfo=%v, status=%v\nwant lockInfo=%v, status=%v",
  121. tc.desc, li, status, tc.wantLI, tc.wantStatus)
  122. continue
  123. }
  124. }
  125. }