OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / net / webdav / webdav_test.go
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
5 package webdav
6
7 import (
8         "errors"
9         "fmt"
10         "io"
11         "io/ioutil"
12         "net/http"
13         "net/http/httptest"
14         "net/url"
15         "os"
16         "reflect"
17         "regexp"
18         "sort"
19         "strings"
20         "testing"
21
22         "golang.org/x/net/context"
23 )
24
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"
28
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>
34                         <D:owner>
35                                 <D:href>http://example.org/~ejw/contact.html</D:href>
36                         </D:owner>
37                 </D:lockinfo>
38         `
39
40         do := func(method, urlStr string, body string, wantStatusCode int, headers ...string) (http.Header, error) {
41                 var bodyReader io.Reader
42                 if body != "" {
43                         bodyReader = strings.NewReader(body)
44                 }
45                 req, err := http.NewRequest(method, urlStr, bodyReader)
46                 if err != nil {
47                         return nil, err
48                 }
49                 for len(headers) >= 2 {
50                         req.Header.Add(headers[0], headers[1])
51                         headers = headers[2:]
52                 }
53                 res, err := http.DefaultTransport.RoundTrip(req)
54                 if err != nil {
55                         return nil, err
56                 }
57                 defer res.Body.Close()
58                 if res.StatusCode != wantStatusCode {
59                         return nil, fmt.Errorf("got status code %d, want %d", res.StatusCode, wantStatusCode)
60                 }
61                 return res.Header, nil
62         }
63
64         prefixes := []string{
65                 "/",
66                 "/a/",
67                 "/a/b/",
68                 "/a/b/c/",
69         }
70         ctx := context.Background()
71         for _, prefix := range prefixes {
72                 fs := NewMemFS()
73                 h := &Handler{
74                         FileSystem: fs,
75                         LockSystem: NewMemLS(),
76                 }
77                 mux := http.NewServeMux()
78                 if prefix != "/" {
79                         h.Prefix = prefix
80                 }
81                 mux.Handle(prefix, h)
82                 srv := httptest.NewServer(mux)
83                 defer srv.Close()
84
85                 // The script is:
86                 //      MKCOL /a
87                 //      MKCOL /a/b
88                 //      PUT   /a/b/c
89                 //      COPY  /a/b/c /a/b/d
90                 //      MKCOL /a/b/e
91                 //      MOVE  /a/b/d /a/b/e/f
92                 //      LOCK  /a/b/e/g
93                 //      PUT   /a/b/e/g
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.
96
97                 wantA := map[string]int{
98                         "/":       http.StatusCreated,
99                         "/a/":     http.StatusMovedPermanently,
100                         "/a/b/":   http.StatusNotFound,
101                         "/a/b/c/": http.StatusNotFound,
102                 }[prefix]
103                 if _, err := do("MKCOL", srv.URL+"/a", "", wantA); err != nil {
104                         t.Errorf("prefix=%-9q MKCOL /a: %v", prefix, err)
105                         continue
106                 }
107
108                 wantB := map[string]int{
109                         "/":       http.StatusCreated,
110                         "/a/":     http.StatusCreated,
111                         "/a/b/":   http.StatusMovedPermanently,
112                         "/a/b/c/": http.StatusNotFound,
113                 }[prefix]
114                 if _, err := do("MKCOL", srv.URL+"/a/b", "", wantB); err != nil {
115                         t.Errorf("prefix=%-9q MKCOL /a/b: %v", prefix, err)
116                         continue
117                 }
118
119                 wantC := map[string]int{
120                         "/":       http.StatusCreated,
121                         "/a/":     http.StatusCreated,
122                         "/a/b/":   http.StatusCreated,
123                         "/a/b/c/": http.StatusMovedPermanently,
124                 }[prefix]
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)
127                         continue
128                 }
129
130                 wantD := map[string]int{
131                         "/":       http.StatusCreated,
132                         "/a/":     http.StatusCreated,
133                         "/a/b/":   http.StatusCreated,
134                         "/a/b/c/": http.StatusMovedPermanently,
135                 }[prefix]
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)
138                         continue
139                 }
140
141                 wantE := map[string]int{
142                         "/":       http.StatusCreated,
143                         "/a/":     http.StatusCreated,
144                         "/a/b/":   http.StatusCreated,
145                         "/a/b/c/": http.StatusNotFound,
146                 }[prefix]
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)
149                         continue
150                 }
151
152                 wantF := map[string]int{
153                         "/":       http.StatusCreated,
154                         "/a/":     http.StatusCreated,
155                         "/a/b/":   http.StatusCreated,
156                         "/a/b/c/": http.StatusNotFound,
157                 }[prefix]
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)
160                         continue
161                 }
162
163                 var lockToken string
164                 wantG := map[string]int{
165                         "/":       http.StatusCreated,
166                         "/a/":     http.StatusCreated,
167                         "/a/b/":   http.StatusCreated,
168                         "/a/b/c/": http.StatusNotFound,
169                 }[prefix]
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)
172                         continue
173                 } else {
174                         lockToken = h.Get("Lock-Token")
175                 }
176
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,
183                 }[prefix]
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)
186                         continue
187                 }
188
189                 got, err := find(ctx, nil, fs, "/")
190                 if err != nil {
191                         t.Errorf("prefix=%-9q find: %v", prefix, err)
192                         continue
193                 }
194                 sort.Strings(got)
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"},
199                         "/a/b/c/": {"/"},
200                 }[prefix]
201                 if !reflect.DeepEqual(got, want) {
202                         t.Errorf("prefix=%-9q find:\ngot  %v\nwant %v", prefix, got, want)
203                         continue
204                 }
205         }
206 }
207
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 "&#34;" or "&quot;") 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{
216                 "":              "",
217                 " ":             " ",
218                 "&":             "&amp;",
219                 "*":             "*",
220                 "+":             "+",
221                 ",":             ",",
222                 "-":             "-",
223                 ".":             ".",
224                 "/":             "/",
225                 "0":             "0",
226                 "9":             "9",
227                 ":":             ":",
228                 "<":             "&lt;",
229                 ">":             "&gt;",
230                 "A":             "A",
231                 "_":             "_",
232                 "a":             "a",
233                 "~":             "~",
234                 "\u0201":        "\u0201",
235                 "&amp;":         "&amp;amp;",
236                 "foo&<b/ar>baz": "foo&amp;&lt;b/ar&gt;baz",
237         }
238
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)
242                 }
243         }
244 }
245
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)
251                 if err != nil {
252                         return "", "", err
253                 }
254                 res, err := http.DefaultClient.Do(req)
255                 if err != nil {
256                         return "", "", err
257                 }
258                 defer res.Body.Close()
259
260                 b, err := ioutil.ReadAll(res.Body)
261                 if err != nil {
262                         return "", "", err
263                 }
264                 hrefMatch := hrefRe.FindStringSubmatch(string(b))
265                 if len(hrefMatch) != 2 {
266                         return "", "", errors.New("D:href not found")
267                 }
268                 displayNameMatch := displayNameRe.FindStringSubmatch(string(b))
269                 if len(displayNameMatch) != 2 {
270                         return "", "", errors.New("D:displayname not found")
271                 }
272
273                 return hrefMatch[1], displayNameMatch[1], nil
274         }
275
276         testCases := []struct {
277                 name, wantHref, wantDisplayName string
278         }{{
279                 name:            `/foo%bar`,
280                 wantHref:        `/foo%25bar`,
281                 wantDisplayName: `foo%bar`,
282         }, {
283                 name:            `/こんにちわ世界`,
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: `こんにちわ世界`,
286         }, {
287                 name:            `/Program Files/`,
288                 wantHref:        `/Program%20Files`,
289                 wantDisplayName: `Program Files`,
290         }, {
291                 name:            `/go+lang`,
292                 wantHref:        `/go+lang`,
293                 wantDisplayName: `go+lang`,
294         }, {
295                 name:            `/go&lang`,
296                 wantHref:        `/go&amp;lang`,
297                 wantDisplayName: `go&amp;lang`,
298         }, {
299                 name:            `/go<lang`,
300                 wantHref:        `/go%3Clang`,
301                 wantDisplayName: `go&lt;lang`,
302         }}
303         ctx := context.Background()
304         fs := NewMemFS()
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)
309                         }
310                 } else {
311                         f, err := fs.OpenFile(ctx, tc.name, os.O_CREATE, 0644)
312                         if err != nil {
313                                 t.Fatalf("name=%q: OpenFile: %v", tc.name, err)
314                         }
315                         f.Close()
316                 }
317         }
318
319         srv := httptest.NewServer(&Handler{
320                 FileSystem: fs,
321                 LockSystem: NewMemLS(),
322         })
323         defer srv.Close()
324
325         u, err := url.Parse(srv.URL)
326         if err != nil {
327                 t.Fatal(err)
328         }
329
330         for _, tc := range testCases {
331                 u.Path = tc.name
332                 gotHref, gotDisplayName, err := do("PROPFIND", u.String())
333                 if err != nil {
334                         t.Errorf("name=%q: PROPFIND: %v", tc.name, err)
335                         continue
336                 }
337                 if gotHref != tc.wantHref {
338                         t.Errorf("name=%q: got href %q, want %q", tc.name, gotHref, tc.wantHref)
339                 }
340                 if gotDisplayName != tc.wantDisplayName {
341                         t.Errorf("name=%q: got dispayname %q, want %q", tc.name, gotDisplayName, tc.wantDisplayName)
342                 }
343         }
344 }