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