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.
22 "golang.org/x/net/context"
25 // TODO: add tests to check XML responses with the expected prefix path
26 func TestPrefix(t *testing.T) {
27 const dst, blah = "Destination", "blah blah blah"
29 // createLockBody comes from the example in Section 9.10.7.
30 const createLockBody = `<?xml version="1.0" encoding="utf-8" ?>
31 <D:lockinfo xmlns:D='DAV:'>
32 <D:lockscope><D:exclusive/></D:lockscope>
33 <D:locktype><D:write/></D:locktype>
35 <D:href>http://example.org/~ejw/contact.html</D:href>
40 do := func(method, urlStr string, body string, wantStatusCode int, headers ...string) (http.Header, error) {
41 var bodyReader io.Reader
43 bodyReader = strings.NewReader(body)
45 req, err := http.NewRequest(method, urlStr, bodyReader)
49 for len(headers) >= 2 {
50 req.Header.Add(headers[0], headers[1])
53 res, err := http.DefaultTransport.RoundTrip(req)
57 defer res.Body.Close()
58 if res.StatusCode != wantStatusCode {
59 return nil, fmt.Errorf("got status code %d, want %d", res.StatusCode, wantStatusCode)
61 return res.Header, nil
70 ctx := context.Background()
71 for _, prefix := range prefixes {
75 LockSystem: NewMemLS(),
77 mux := http.NewServeMux()
82 srv := httptest.NewServer(mux)
91 // MOVE /a/b/d /a/b/e/f
94 // which should yield the (possibly stripped) filenames /a/b/c,
95 // /a/b/e/f and /a/b/e/g, plus their parent directories.
97 wantA := map[string]int{
98 "/": http.StatusCreated,
99 "/a/": http.StatusMovedPermanently,
100 "/a/b/": http.StatusNotFound,
101 "/a/b/c/": http.StatusNotFound,
103 if _, err := do("MKCOL", srv.URL+"/a", "", wantA); err != nil {
104 t.Errorf("prefix=%-9q MKCOL /a: %v", prefix, err)
108 wantB := map[string]int{
109 "/": http.StatusCreated,
110 "/a/": http.StatusCreated,
111 "/a/b/": http.StatusMovedPermanently,
112 "/a/b/c/": http.StatusNotFound,
114 if _, err := do("MKCOL", srv.URL+"/a/b", "", wantB); err != nil {
115 t.Errorf("prefix=%-9q MKCOL /a/b: %v", prefix, err)
119 wantC := map[string]int{
120 "/": http.StatusCreated,
121 "/a/": http.StatusCreated,
122 "/a/b/": http.StatusCreated,
123 "/a/b/c/": http.StatusMovedPermanently,
125 if _, err := do("PUT", srv.URL+"/a/b/c", blah, wantC); err != nil {
126 t.Errorf("prefix=%-9q PUT /a/b/c: %v", prefix, err)
130 wantD := map[string]int{
131 "/": http.StatusCreated,
132 "/a/": http.StatusCreated,
133 "/a/b/": http.StatusCreated,
134 "/a/b/c/": http.StatusMovedPermanently,
136 if _, err := do("COPY", srv.URL+"/a/b/c", "", wantD, dst, srv.URL+"/a/b/d"); err != nil {
137 t.Errorf("prefix=%-9q COPY /a/b/c /a/b/d: %v", prefix, err)
141 wantE := map[string]int{
142 "/": http.StatusCreated,
143 "/a/": http.StatusCreated,
144 "/a/b/": http.StatusCreated,
145 "/a/b/c/": http.StatusNotFound,
147 if _, err := do("MKCOL", srv.URL+"/a/b/e", "", wantE); err != nil {
148 t.Errorf("prefix=%-9q MKCOL /a/b/e: %v", prefix, err)
152 wantF := map[string]int{
153 "/": http.StatusCreated,
154 "/a/": http.StatusCreated,
155 "/a/b/": http.StatusCreated,
156 "/a/b/c/": http.StatusNotFound,
158 if _, err := do("MOVE", srv.URL+"/a/b/d", "", wantF, dst, srv.URL+"/a/b/e/f"); err != nil {
159 t.Errorf("prefix=%-9q MOVE /a/b/d /a/b/e/f: %v", prefix, err)
164 wantG := map[string]int{
165 "/": http.StatusCreated,
166 "/a/": http.StatusCreated,
167 "/a/b/": http.StatusCreated,
168 "/a/b/c/": http.StatusNotFound,
170 if h, err := do("LOCK", srv.URL+"/a/b/e/g", createLockBody, wantG); err != nil {
171 t.Errorf("prefix=%-9q LOCK /a/b/e/g: %v", prefix, err)
174 lockToken = h.Get("Lock-Token")
177 ifHeader := fmt.Sprintf("<%s/a/b/e/g> (%s)", srv.URL, lockToken)
178 wantH := map[string]int{
179 "/": http.StatusCreated,
180 "/a/": http.StatusCreated,
181 "/a/b/": http.StatusCreated,
182 "/a/b/c/": http.StatusNotFound,
184 if _, err := do("PUT", srv.URL+"/a/b/e/g", blah, wantH, "If", ifHeader); err != nil {
185 t.Errorf("prefix=%-9q PUT /a/b/e/g: %v", prefix, err)
189 got, err := find(ctx, nil, fs, "/")
191 t.Errorf("prefix=%-9q find: %v", prefix, err)
195 want := map[string][]string{
196 "/": {"/", "/a", "/a/b", "/a/b/c", "/a/b/e", "/a/b/e/f", "/a/b/e/g"},
197 "/a/": {"/", "/b", "/b/c", "/b/e", "/b/e/f", "/b/e/g"},
198 "/a/b/": {"/", "/c", "/e", "/e/f", "/e/g"},
201 if !reflect.DeepEqual(got, want) {
202 t.Errorf("prefix=%-9q find:\ngot %v\nwant %v", prefix, got, want)
208 func TestEscapeXML(t *testing.T) {
209 // These test cases aren't exhaustive, and there is more than one way to
210 // escape e.g. a quot (as """ or """) or an apos. We presume that
211 // the encoding/xml package tests xml.EscapeText more thoroughly. This test
212 // here is just a sanity check for this package's escapeXML function, and
213 // its attempt to provide a fast path (and avoid a bytes.Buffer allocation)
214 // when escaping filenames is obviously a no-op.
215 testCases := map[string]string{
235 "&": "&amp;",
236 "foo&<b/ar>baz": "foo&<b/ar>baz",
239 for in, want := range testCases {
240 if got := escapeXML(in); got != want {
241 t.Errorf("in=%q: got %q, want %q", in, got, want)
246 func TestFilenameEscape(t *testing.T) {
247 hrefRe := regexp.MustCompile(`<D:href>([^<]*)</D:href>`)
248 displayNameRe := regexp.MustCompile(`<D:displayname>([^<]*)</D:displayname>`)
249 do := func(method, urlStr string) (string, string, error) {
250 req, err := http.NewRequest(method, urlStr, nil)
254 res, err := http.DefaultClient.Do(req)
258 defer res.Body.Close()
260 b, err := ioutil.ReadAll(res.Body)
264 hrefMatch := hrefRe.FindStringSubmatch(string(b))
265 if len(hrefMatch) != 2 {
266 return "", "", errors.New("D:href not found")
268 displayNameMatch := displayNameRe.FindStringSubmatch(string(b))
269 if len(displayNameMatch) != 2 {
270 return "", "", errors.New("D:displayname not found")
273 return hrefMatch[1], displayNameMatch[1], nil
276 testCases := []struct {
277 name, wantHref, wantDisplayName string
280 wantHref: `/foo%25bar`,
281 wantDisplayName: `foo%bar`,
284 wantHref: `/%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%82%8F%E4%B8%96%E7%95%8C`,
285 wantDisplayName: `こんにちわ世界`,
287 name: `/Program Files/`,
288 wantHref: `/Program%20Files`,
289 wantDisplayName: `Program Files`,
292 wantHref: `/go+lang`,
293 wantDisplayName: `go+lang`,
296 wantHref: `/go&lang`,
297 wantDisplayName: `go&lang`,
300 wantHref: `/go%3Clang`,
301 wantDisplayName: `go<lang`,
303 ctx := context.Background()
305 for _, tc := range testCases {
306 if strings.HasSuffix(tc.name, "/") {
307 if err := fs.Mkdir(ctx, tc.name, 0755); err != nil {
308 t.Fatalf("name=%q: Mkdir: %v", tc.name, err)
311 f, err := fs.OpenFile(ctx, tc.name, os.O_CREATE, 0644)
313 t.Fatalf("name=%q: OpenFile: %v", tc.name, err)
319 srv := httptest.NewServer(&Handler{
321 LockSystem: NewMemLS(),
325 u, err := url.Parse(srv.URL)
330 for _, tc := range testCases {
332 gotHref, gotDisplayName, err := do("PROPFIND", u.String())
334 t.Errorf("name=%q: PROPFIND: %v", tc.name, err)
337 if gotHref != tc.wantHref {
338 t.Errorf("name=%q: got href %q, want %q", tc.name, gotHref, tc.wantHref)
340 if gotDisplayName != tc.wantDisplayName {
341 t.Errorf("name=%q: got dispayname %q, want %q", tc.name, gotDisplayName, tc.wantDisplayName)