|
| 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 | + "fmt" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
| 12 | + "reflect" |
| 13 | + "sort" |
| 14 | + "strings" |
| 15 | + "testing" |
| 16 | +) |
| 17 | + |
| 18 | +// TestStripPrefix tests the StripPrefix function. We can't test the |
| 19 | +// StripPrefix function with the litmus test, even though all of the litmus |
| 20 | +// test paths start with "/litmus/", because one of the first things that the |
| 21 | +// litmus test does is "MKCOL /litmus/". That request succeeds without a |
| 22 | +// StripPrefix, but fails with a StripPrefix because you cannot MKCOL the root |
| 23 | +// directory of a FileSystem. |
| 24 | +func TestStripPrefix(t *testing.T) { |
| 25 | + const dst, blah = "Destination", "blah blah blah" |
| 26 | + |
| 27 | + do := func(method, urlStr string, body io.Reader, wantStatusCode int, headers ...string) error { |
| 28 | + req, err := http.NewRequest(method, urlStr, body) |
| 29 | + if err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + for len(headers) >= 2 { |
| 33 | + req.Header.Add(headers[0], headers[1]) |
| 34 | + headers = headers[2:] |
| 35 | + } |
| 36 | + res, err := http.DefaultClient.Do(req) |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + defer res.Body.Close() |
| 41 | + if res.StatusCode != wantStatusCode { |
| 42 | + return fmt.Errorf("got status code %d, want %d", res.StatusCode, wantStatusCode) |
| 43 | + } |
| 44 | + return nil |
| 45 | + } |
| 46 | + |
| 47 | + prefixes := []string{ |
| 48 | + "/", |
| 49 | + "/a/", |
| 50 | + "/a/b/", |
| 51 | + "/a/b/c/", |
| 52 | + } |
| 53 | + for _, prefix := range prefixes { |
| 54 | + fs := NewMemFS() |
| 55 | + h := http.Handler(&Handler{ |
| 56 | + FileSystem: fs, |
| 57 | + LockSystem: NewMemLS(), |
| 58 | + }) |
| 59 | + mux := http.NewServeMux() |
| 60 | + if prefix != "/" { |
| 61 | + // Note that this is webdav.StripPrefix, not http.StripPrefix. |
| 62 | + h = StripPrefix(prefix, h) |
| 63 | + } |
| 64 | + mux.Handle(prefix, h) |
| 65 | + srv := httptest.NewServer(mux) |
| 66 | + defer srv.Close() |
| 67 | + |
| 68 | + // The script is: |
| 69 | + // MKCOL /a |
| 70 | + // MKCOL /a/b |
| 71 | + // PUT /a/b/c |
| 72 | + // COPY /a/b/c /a/b/d |
| 73 | + // MKCOL /a/b/e |
| 74 | + // MOVE /a/b/d /a/b/e/f |
| 75 | + // which should yield the (possibly stripped) filenames /a/b/c and |
| 76 | + // /a/b/e/f, plus their parent directories. |
| 77 | + |
| 78 | + wantA := map[string]int{ |
| 79 | + "/": http.StatusCreated, |
| 80 | + "/a/": http.StatusMovedPermanently, |
| 81 | + "/a/b/": http.StatusNotFound, |
| 82 | + "/a/b/c/": http.StatusNotFound, |
| 83 | + }[prefix] |
| 84 | + if err := do("MKCOL", srv.URL+"/a", nil, wantA); err != nil { |
| 85 | + t.Errorf("prefix=%-9q MKCOL /a: %v", prefix, err) |
| 86 | + continue |
| 87 | + } |
| 88 | + |
| 89 | + wantB := map[string]int{ |
| 90 | + "/": http.StatusCreated, |
| 91 | + "/a/": http.StatusCreated, |
| 92 | + "/a/b/": http.StatusMovedPermanently, |
| 93 | + "/a/b/c/": http.StatusNotFound, |
| 94 | + }[prefix] |
| 95 | + if err := do("MKCOL", srv.URL+"/a/b", nil, wantB); err != nil { |
| 96 | + t.Errorf("prefix=%-9q MKCOL /a/b: %v", prefix, err) |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + wantC := map[string]int{ |
| 101 | + "/": http.StatusCreated, |
| 102 | + "/a/": http.StatusCreated, |
| 103 | + "/a/b/": http.StatusCreated, |
| 104 | + "/a/b/c/": http.StatusMovedPermanently, |
| 105 | + }[prefix] |
| 106 | + if err := do("PUT", srv.URL+"/a/b/c", strings.NewReader(blah), wantC); err != nil { |
| 107 | + t.Errorf("prefix=%-9q PUT /a/b/c: %v", prefix, err) |
| 108 | + continue |
| 109 | + } |
| 110 | + |
| 111 | + wantD := map[string]int{ |
| 112 | + "/": http.StatusCreated, |
| 113 | + "/a/": http.StatusCreated, |
| 114 | + "/a/b/": http.StatusCreated, |
| 115 | + "/a/b/c/": http.StatusMovedPermanently, |
| 116 | + }[prefix] |
| 117 | + if err := do("COPY", srv.URL+"/a/b/c", nil, wantD, dst, srv.URL+"/a/b/d"); err != nil { |
| 118 | + t.Errorf("prefix=%-9q COPY /a/b/c /a/b/d: %v", prefix, err) |
| 119 | + continue |
| 120 | + } |
| 121 | + |
| 122 | + wantE := map[string]int{ |
| 123 | + "/": http.StatusCreated, |
| 124 | + "/a/": http.StatusCreated, |
| 125 | + "/a/b/": http.StatusCreated, |
| 126 | + "/a/b/c/": http.StatusNotFound, |
| 127 | + }[prefix] |
| 128 | + if err := do("MKCOL", srv.URL+"/a/b/e", nil, wantE); err != nil { |
| 129 | + t.Errorf("prefix=%-9q MKCOL /a/b/e: %v", prefix, err) |
| 130 | + continue |
| 131 | + } |
| 132 | + |
| 133 | + wantF := map[string]int{ |
| 134 | + "/": http.StatusCreated, |
| 135 | + "/a/": http.StatusCreated, |
| 136 | + "/a/b/": http.StatusCreated, |
| 137 | + "/a/b/c/": http.StatusNotFound, |
| 138 | + }[prefix] |
| 139 | + if err := do("MOVE", srv.URL+"/a/b/d", nil, wantF, dst, srv.URL+"/a/b/e/f"); err != nil { |
| 140 | + t.Errorf("prefix=%-9q MOVE /a/b/d /a/b/e/f: %v", prefix, err) |
| 141 | + continue |
| 142 | + } |
| 143 | + |
| 144 | + got, err := find(nil, fs, "/") |
| 145 | + if err != nil { |
| 146 | + t.Errorf("prefix=%-9q find: %v", prefix, err) |
| 147 | + continue |
| 148 | + } |
| 149 | + sort.Strings(got) |
| 150 | + want := map[string][]string{ |
| 151 | + "/": []string{"/", "/a", "/a/b", "/a/b/c", "/a/b/e", "/a/b/e/f"}, |
| 152 | + "/a/": []string{"/", "/b", "/b/c", "/b/e", "/b/e/f"}, |
| 153 | + "/a/b/": []string{"/", "/c", "/e", "/e/f"}, |
| 154 | + "/a/b/c/": []string{"/"}, |
| 155 | + }[prefix] |
| 156 | + if !reflect.DeepEqual(got, want) { |
| 157 | + t.Errorf("prefix=%-9q find:\ngot %v\nwant %v", prefix, got, want) |
| 158 | + continue |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments