]>
Commit | Line | Data |
---|---|---|
aa8901e9 ILT |
1 | // Copyright 2019 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 web | |
6 | ||
7 | import ( | |
8 | "errors" | |
9 | "io/ioutil" | |
10 | "os" | |
11 | "path/filepath" | |
12 | "testing" | |
13 | ) | |
14 | ||
15 | func TestGetFileURL(t *testing.T) { | |
16 | const content = "Hello, file!\n" | |
17 | ||
18 | f, err := ioutil.TempFile("", "web-TestGetFileURL") | |
19 | if err != nil { | |
20 | t.Fatal(err) | |
21 | } | |
5a8ea165 ILT |
22 | defer os.Remove(f.Name()) |
23 | ||
aa8901e9 ILT |
24 | if _, err := f.WriteString(content); err != nil { |
25 | t.Error(err) | |
26 | } | |
27 | if err := f.Close(); err != nil { | |
28 | t.Fatal(err) | |
29 | } | |
30 | ||
31 | u, err := urlFromFilePath(f.Name()) | |
32 | if err != nil { | |
33 | t.Fatal(err) | |
34 | } | |
35 | ||
36 | b, err := GetBytes(u) | |
37 | if err != nil { | |
38 | t.Fatalf("GetBytes(%v) = _, %v", u, err) | |
39 | } | |
40 | if string(b) != content { | |
41 | t.Fatalf("after writing %q to %s, GetBytes(%v) read %q", content, f.Name(), u, b) | |
42 | } | |
43 | } | |
44 | ||
45 | func TestGetNonexistentFile(t *testing.T) { | |
46 | path, err := filepath.Abs("nonexistent") | |
47 | if err != nil { | |
48 | t.Fatal(err) | |
49 | } | |
50 | ||
51 | u, err := urlFromFilePath(path) | |
52 | if err != nil { | |
53 | t.Fatal(err) | |
54 | } | |
55 | ||
56 | b, err := GetBytes(u) | |
57 | if !errors.Is(err, os.ErrNotExist) { | |
58 | t.Fatalf("GetBytes(%v) = %q, %v; want _, os.ErrNotExist", u, b, err) | |
59 | } | |
60 | } |