~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to src/pkg/http/fs_test.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
85
85
        }
86
86
}
87
87
 
 
88
type testFileSystem struct {
 
89
        open func(name string) (File, os.Error)
 
90
}
 
91
 
 
92
func (fs *testFileSystem) Open(name string) (File, os.Error) {
 
93
        return fs.open(name)
 
94
}
 
95
 
 
96
func TestFileServerCleans(t *testing.T) {
 
97
        ch := make(chan string, 1)
 
98
        fs := FileServer(&testFileSystem{func(name string) (File, os.Error) {
 
99
                ch <- name
 
100
                return nil, os.ENOENT
 
101
        }})
 
102
        tests := []struct {
 
103
                reqPath, openArg string
 
104
        }{
 
105
                {"/foo.txt", "/foo.txt"},
 
106
                {"//foo.txt", "/foo.txt"},
 
107
                {"/../foo.txt", "/foo.txt"},
 
108
        }
 
109
        req, _ := NewRequest("GET", "http://example.com", nil)
 
110
        for n, test := range tests {
 
111
                rec := httptest.NewRecorder()
 
112
                req.URL.Path = test.reqPath
 
113
                fs.ServeHTTP(rec, req)
 
114
                if got := <-ch; got != test.openArg {
 
115
                        t.Errorf("test %d: got %q, want %q", n, got, test.openArg)
 
116
                }
 
117
        }
 
118
}
 
119
 
 
120
func TestDirJoin(t *testing.T) {
 
121
        wfi, err := os.Stat("/etc/hosts")
 
122
        if err != nil {
 
123
                t.Logf("skipping test; no /etc/hosts file")
 
124
                return
 
125
        }
 
126
        test := func(d Dir, name string) {
 
127
                f, err := d.Open(name)
 
128
                if err != nil {
 
129
                        t.Fatalf("open of %s: %v", name, err)
 
130
                }
 
131
                defer f.Close()
 
132
                gfi, err := f.Stat()
 
133
                if err != nil {
 
134
                        t.Fatalf("stat of %s: %v", err)
 
135
                }
 
136
                if gfi.Ino != wfi.Ino {
 
137
                        t.Errorf("%s got different inode")
 
138
                }
 
139
        }
 
140
        test(Dir("/etc/"), "/hosts")
 
141
        test(Dir("/etc/"), "hosts")
 
142
        test(Dir("/etc/"), "../../../../hosts")
 
143
        test(Dir("/etc"), "/hosts")
 
144
        test(Dir("/etc"), "hosts")
 
145
        test(Dir("/etc"), "../../../../hosts")
 
146
 
 
147
        // Not really directories, but since we use this trick in
 
148
        // ServeFile, test it:
 
149
        test(Dir("/etc/hosts"), "")
 
150
        test(Dir("/etc/hosts"), "/")
 
151
        test(Dir("/etc/hosts"), "../")
 
152
}
 
153
 
88
154
func TestServeFileContentType(t *testing.T) {
89
155
        const ctype = "icecream/chocolate"
90
156
        override := false