1 // Copyright 2011 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.
14 func checker(t
*testing
.T
) func(string, error
) {
15 return func(call
string, err error
) {
19 t
.Fatalf("%s: %v", call
, err
)
23 func TestFileTransport(t
*testing
.T
) {
26 dname
, err
:= ioutil
.TempDir("", "")
28 fname
:= filepath
.Join(dname
, "foo.txt")
29 err
= ioutil
.WriteFile(fname
, []byte("Bar"), 0644)
30 check("WriteFile", err
)
31 defer os
.Remove(dname
)
32 defer os
.Remove(fname
)
35 tr
.RegisterProtocol("file", NewFileTransport(Dir(dname
)))
36 c
:= &Client
{Transport
: tr
}
38 fooURLs
:= []string{"file:///foo.txt", "file://../foo.txt"}
39 for _
, urlstr
:= range fooURLs
{
40 res
, err
:= c
.Get(urlstr
)
41 check("Get "+urlstr
, err
)
42 if res
.StatusCode
!= 200 {
43 t
.Errorf("for %s, StatusCode = %d, want 200", urlstr
, res
.StatusCode
)
45 if res
.ContentLength
!= -1 {
46 t
.Errorf("for %s, ContentLength = %d, want -1", urlstr
, res
.ContentLength
)
49 t
.Fatalf("for %s, nil Body", urlstr
)
51 slurp
, err
:= ioutil
.ReadAll(res
.Body
)
53 check("ReadAll "+urlstr
, err
)
54 if string(slurp
) != "Bar" {
55 t
.Errorf("for %s, got content %q, want %q", urlstr
, string(slurp
), "Bar")
59 const badURL
= "file://../no-exist.txt"
60 res
, err
:= c
.Get(badURL
)
61 check("Get "+badURL
, err
)
62 if res
.StatusCode
!= 404 {
63 t
.Errorf("for %s, StatusCode = %d, want 404", badURL
, res
.StatusCode
)