libgo: update to Go 1.11
[official-gcc.git] / libgo / go / net / http / filetransport_test.go
blob2a2f32c7694bb779c6dde133f8d23f2f99e701e6
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.
5 package http
7 import (
8 "io/ioutil"
9 "os"
10 "path/filepath"
11 "testing"
14 func checker(t *testing.T) func(string, error) {
15 return func(call string, err error) {
16 if err == nil {
17 return
19 t.Fatalf("%s: %v", call, err)
23 func TestFileTransport(t *testing.T) {
24 check := checker(t)
26 dname, err := ioutil.TempDir("", "")
27 check("TempDir", err)
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)
34 tr := &Transport{}
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)
48 if res.Body == nil {
49 t.Fatalf("for %s, nil Body", urlstr)
51 slurp, err := ioutil.ReadAll(res.Body)
52 res.Body.Close()
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)
65 res.Body.Close()