summaryrefslogtreecommitdiffstats
path: root/vendor/golang.org/x/net/webdav/webdav_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/golang.org/x/net/webdav/webdav_test.go')
-rw-r--r--vendor/golang.org/x/net/webdav/webdav_test.go113
1 files changed, 86 insertions, 27 deletions
diff --git a/vendor/golang.org/x/net/webdav/webdav_test.go b/vendor/golang.org/x/net/webdav/webdav_test.go
index b068aab32..25e0d5421 100644
--- a/vendor/golang.org/x/net/webdav/webdav_test.go
+++ b/vendor/golang.org/x/net/webdav/webdav_test.go
@@ -18,6 +18,8 @@ import (
"sort"
"strings"
"testing"
+
+ "golang.org/x/net/context"
)
// TODO: add tests to check XML responses with the expected prefix path
@@ -48,7 +50,7 @@ func TestPrefix(t *testing.T) {
req.Header.Add(headers[0], headers[1])
headers = headers[2:]
}
- res, err := http.DefaultClient.Do(req)
+ res, err := http.DefaultTransport.RoundTrip(req)
if err != nil {
return nil, err
}
@@ -65,6 +67,7 @@ func TestPrefix(t *testing.T) {
"/a/b/",
"/a/b/c/",
}
+ ctx := context.Background()
for _, prefix := range prefixes {
fs := NewMemFS()
h := &Handler{
@@ -183,7 +186,7 @@ func TestPrefix(t *testing.T) {
continue
}
- got, err := find(nil, fs, "/")
+ got, err := find(ctx, nil, fs, "/")
if err != nil {
t.Errorf("prefix=%-9q find: %v", prefix, err)
continue
@@ -202,57 +205,110 @@ func TestPrefix(t *testing.T) {
}
}
+func TestEscapeXML(t *testing.T) {
+ // These test cases aren't exhaustive, and there is more than one way to
+ // escape e.g. a quot (as """ or """) or an apos. We presume that
+ // the encoding/xml package tests xml.EscapeText more thoroughly. This test
+ // here is just a sanity check for this package's escapeXML function, and
+ // its attempt to provide a fast path (and avoid a bytes.Buffer allocation)
+ // when escaping filenames is obviously a no-op.
+ testCases := map[string]string{
+ "": "",
+ " ": " ",
+ "&": "&",
+ "*": "*",
+ "+": "+",
+ ",": ",",
+ "-": "-",
+ ".": ".",
+ "/": "/",
+ "0": "0",
+ "9": "9",
+ ":": ":",
+ "<": "&lt;",
+ ">": "&gt;",
+ "A": "A",
+ "_": "_",
+ "a": "a",
+ "~": "~",
+ "\u0201": "\u0201",
+ "&amp;": "&amp;amp;",
+ "foo&<b/ar>baz": "foo&amp;&lt;b/ar&gt;baz",
+ }
+
+ for in, want := range testCases {
+ if got := escapeXML(in); got != want {
+ t.Errorf("in=%q: got %q, want %q", in, got, want)
+ }
+ }
+}
+
func TestFilenameEscape(t *testing.T) {
- re := regexp.MustCompile(`<D:href>([^<]*)</D:href>`)
- do := func(method, urlStr string) (string, error) {
+ hrefRe := regexp.MustCompile(`<D:href>([^<]*)</D:href>`)
+ displayNameRe := regexp.MustCompile(`<D:displayname>([^<]*)</D:displayname>`)
+ do := func(method, urlStr string) (string, string, error) {
req, err := http.NewRequest(method, urlStr, nil)
if err != nil {
- return "", err
+ return "", "", err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
- return "", err
+ return "", "", err
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
- return "", err
+ return "", "", err
+ }
+ hrefMatch := hrefRe.FindStringSubmatch(string(b))
+ if len(hrefMatch) != 2 {
+ return "", "", errors.New("D:href not found")
}
- m := re.FindStringSubmatch(string(b))
- if len(m) != 2 {
- return "", errors.New("D:href not found")
+ displayNameMatch := displayNameRe.FindStringSubmatch(string(b))
+ if len(displayNameMatch) != 2 {
+ return "", "", errors.New("D:displayname not found")
}
- return m[1], nil
+ return hrefMatch[1], displayNameMatch[1], nil
}
testCases := []struct {
- name, want string
+ name, wantHref, wantDisplayName string
}{{
- name: `/foo%bar`,
- want: `/foo%25bar`,
+ name: `/foo%bar`,
+ wantHref: `/foo%25bar`,
+ wantDisplayName: `foo%bar`,
}, {
- name: `/こんにちわ世界`,
- want: `/%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%82%8F%E4%B8%96%E7%95%8C`,
+ name: `/こんにちわ世界`,
+ wantHref: `/%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%82%8F%E4%B8%96%E7%95%8C`,
+ wantDisplayName: `こんにちわ世界`,
}, {
- name: `/Program Files/`,
- want: `/Program%20Files`,
+ name: `/Program Files/`,
+ wantHref: `/Program%20Files`,
+ wantDisplayName: `Program Files`,
}, {
- name: `/go+lang`,
- want: `/go+lang`,
+ name: `/go+lang`,
+ wantHref: `/go+lang`,
+ wantDisplayName: `go+lang`,
}, {
- name: `/go&lang`,
- want: `/go&amp;lang`,
+ name: `/go&lang`,
+ wantHref: `/go&amp;lang`,
+ wantDisplayName: `go&amp;lang`,
+ }, {
+ name: `/go<lang`,
+ wantHref: `/go%3Clang`,
+ wantDisplayName: `go&lt;lang`,
}}
+ ctx := context.Background()
fs := NewMemFS()
for _, tc := range testCases {
if strings.HasSuffix(tc.name, "/") {
- if err := fs.Mkdir(tc.name, 0755); err != nil {
+ if err := fs.Mkdir(ctx, tc.name, 0755); err != nil {
t.Fatalf("name=%q: Mkdir: %v", tc.name, err)
}
} else {
- f, err := fs.OpenFile(tc.name, os.O_CREATE, 0644)
+ f, err := fs.OpenFile(ctx, tc.name, os.O_CREATE, 0644)
if err != nil {
t.Fatalf("name=%q: OpenFile: %v", tc.name, err)
}
@@ -273,13 +329,16 @@ func TestFilenameEscape(t *testing.T) {
for _, tc := range testCases {
u.Path = tc.name
- got, err := do("PROPFIND", u.String())
+ gotHref, gotDisplayName, err := do("PROPFIND", u.String())
if err != nil {
t.Errorf("name=%q: PROPFIND: %v", tc.name, err)
continue
}
- if got != tc.want {
- t.Errorf("name=%q: got %q, want %q", tc.name, got, tc.want)
+ if gotHref != tc.wantHref {
+ t.Errorf("name=%q: got href %q, want %q", tc.name, gotHref, tc.wantHref)
+ }
+ if gotDisplayName != tc.wantDisplayName {
+ t.Errorf("name=%q: got dispayname %q, want %q", tc.name, gotDisplayName, tc.wantDisplayName)
}
}
}