1
2
3
4
5 package web
6
7 import (
8 "net/url"
9 "testing"
10 )
11
12 func TestURLToFilePath(t *testing.T) {
13 for _, tc := range urlTests {
14 if tc.url == "" {
15 continue
16 }
17 tc := tc
18
19 t.Run(tc.url, func(t *testing.T) {
20 u, err := url.Parse(tc.url)
21 if err != nil {
22 t.Fatalf("url.Parse(%q): %v", tc.url, err)
23 }
24
25 path, err := urlToFilePath(u)
26 if err != nil {
27 if err.Error() == tc.wantErr {
28 return
29 }
30 if tc.wantErr == "" {
31 t.Fatalf("urlToFilePath(%v): %v; want <nil>", u, err)
32 } else {
33 t.Fatalf("urlToFilePath(%v): %v; want %s", u, err, tc.wantErr)
34 }
35 }
36
37 if path != tc.filePath || tc.wantErr != "" {
38 t.Fatalf("urlToFilePath(%v) = %q, <nil>; want %q, %s", u, path, tc.filePath, tc.wantErr)
39 }
40 })
41 }
42 }
43
44 func TestURLFromFilePath(t *testing.T) {
45 for _, tc := range urlTests {
46 if tc.filePath == "" {
47 continue
48 }
49 tc := tc
50
51 t.Run(tc.filePath, func(t *testing.T) {
52 u, err := urlFromFilePath(tc.filePath)
53 if err != nil {
54 if err.Error() == tc.wantErr {
55 return
56 }
57 if tc.wantErr == "" {
58 t.Fatalf("urlFromFilePath(%v): %v; want <nil>", tc.filePath, err)
59 } else {
60 t.Fatalf("urlFromFilePath(%v): %v; want %s", tc.filePath, err, tc.wantErr)
61 }
62 }
63
64 if tc.wantErr != "" {
65 t.Fatalf("urlFromFilePath(%v) = <nil>; want error: %s", tc.filePath, tc.wantErr)
66 }
67
68 wantURL := tc.url
69 if tc.canonicalURL != "" {
70 wantURL = tc.canonicalURL
71 }
72 if u.String() != wantURL {
73 t.Errorf("urlFromFilePath(%v) = %v; want %s", tc.filePath, u, wantURL)
74 }
75 })
76 }
77 }
78
View as plain text