webdav_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2015 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. "fmt"
  7. "io"
  8. "net/http"
  9. "net/http/httptest"
  10. "reflect"
  11. "sort"
  12. "strings"
  13. "testing"
  14. )
  15. // TODO: add tests to check XML responses with the expected prefix path
  16. func TestPrefix(t *testing.T) {
  17. const dst, blah = "Destination", "blah blah blah"
  18. do := func(method, urlStr string, body io.Reader, wantStatusCode int, headers ...string) error {
  19. req, err := http.NewRequest(method, urlStr, body)
  20. if err != nil {
  21. return err
  22. }
  23. for len(headers) >= 2 {
  24. req.Header.Add(headers[0], headers[1])
  25. headers = headers[2:]
  26. }
  27. res, err := http.DefaultClient.Do(req)
  28. if err != nil {
  29. return err
  30. }
  31. defer res.Body.Close()
  32. if res.StatusCode != wantStatusCode {
  33. return fmt.Errorf("got status code %d, want %d", res.StatusCode, wantStatusCode)
  34. }
  35. return nil
  36. }
  37. prefixes := []string{
  38. "/",
  39. "/a/",
  40. "/a/b/",
  41. "/a/b/c/",
  42. }
  43. for _, prefix := range prefixes {
  44. fs := NewMemFS()
  45. h := &Handler{
  46. FileSystem: fs,
  47. LockSystem: NewMemLS(),
  48. }
  49. mux := http.NewServeMux()
  50. if prefix != "/" {
  51. h.Prefix = prefix
  52. }
  53. mux.Handle(prefix, h)
  54. srv := httptest.NewServer(mux)
  55. defer srv.Close()
  56. // The script is:
  57. // MKCOL /a
  58. // MKCOL /a/b
  59. // PUT /a/b/c
  60. // COPY /a/b/c /a/b/d
  61. // MKCOL /a/b/e
  62. // MOVE /a/b/d /a/b/e/f
  63. // which should yield the (possibly stripped) filenames /a/b/c and
  64. // /a/b/e/f, plus their parent directories.
  65. wantA := map[string]int{
  66. "/": http.StatusCreated,
  67. "/a/": http.StatusMovedPermanently,
  68. "/a/b/": http.StatusNotFound,
  69. "/a/b/c/": http.StatusNotFound,
  70. }[prefix]
  71. if err := do("MKCOL", srv.URL+"/a", nil, wantA); err != nil {
  72. t.Errorf("prefix=%-9q MKCOL /a: %v", prefix, err)
  73. continue
  74. }
  75. wantB := map[string]int{
  76. "/": http.StatusCreated,
  77. "/a/": http.StatusCreated,
  78. "/a/b/": http.StatusMovedPermanently,
  79. "/a/b/c/": http.StatusNotFound,
  80. }[prefix]
  81. if err := do("MKCOL", srv.URL+"/a/b", nil, wantB); err != nil {
  82. t.Errorf("prefix=%-9q MKCOL /a/b: %v", prefix, err)
  83. continue
  84. }
  85. wantC := map[string]int{
  86. "/": http.StatusCreated,
  87. "/a/": http.StatusCreated,
  88. "/a/b/": http.StatusCreated,
  89. "/a/b/c/": http.StatusMovedPermanently,
  90. }[prefix]
  91. if err := do("PUT", srv.URL+"/a/b/c", strings.NewReader(blah), wantC); err != nil {
  92. t.Errorf("prefix=%-9q PUT /a/b/c: %v", prefix, err)
  93. continue
  94. }
  95. wantD := map[string]int{
  96. "/": http.StatusCreated,
  97. "/a/": http.StatusCreated,
  98. "/a/b/": http.StatusCreated,
  99. "/a/b/c/": http.StatusMovedPermanently,
  100. }[prefix]
  101. if err := do("COPY", srv.URL+"/a/b/c", nil, wantD, dst, srv.URL+"/a/b/d"); err != nil {
  102. t.Errorf("prefix=%-9q COPY /a/b/c /a/b/d: %v", prefix, err)
  103. continue
  104. }
  105. wantE := map[string]int{
  106. "/": http.StatusCreated,
  107. "/a/": http.StatusCreated,
  108. "/a/b/": http.StatusCreated,
  109. "/a/b/c/": http.StatusNotFound,
  110. }[prefix]
  111. if err := do("MKCOL", srv.URL+"/a/b/e", nil, wantE); err != nil {
  112. t.Errorf("prefix=%-9q MKCOL /a/b/e: %v", prefix, err)
  113. continue
  114. }
  115. wantF := map[string]int{
  116. "/": http.StatusCreated,
  117. "/a/": http.StatusCreated,
  118. "/a/b/": http.StatusCreated,
  119. "/a/b/c/": http.StatusNotFound,
  120. }[prefix]
  121. if err := do("MOVE", srv.URL+"/a/b/d", nil, wantF, dst, srv.URL+"/a/b/e/f"); err != nil {
  122. t.Errorf("prefix=%-9q MOVE /a/b/d /a/b/e/f: %v", prefix, err)
  123. continue
  124. }
  125. got, err := find(nil, fs, "/")
  126. if err != nil {
  127. t.Errorf("prefix=%-9q find: %v", prefix, err)
  128. continue
  129. }
  130. sort.Strings(got)
  131. want := map[string][]string{
  132. "/": []string{"/", "/a", "/a/b", "/a/b/c", "/a/b/e", "/a/b/e/f"},
  133. "/a/": []string{"/", "/b", "/b/c", "/b/e", "/b/e/f"},
  134. "/a/b/": []string{"/", "/c", "/e", "/e/f"},
  135. "/a/b/c/": []string{"/"},
  136. }[prefix]
  137. if !reflect.DeepEqual(got, want) {
  138. t.Errorf("prefix=%-9q find:\ngot %v\nwant %v", prefix, got, want)
  139. continue
  140. }
  141. }
  142. }