github.com/puellanivis/breton@v0.2.16/lib/files/datafiles/data_test.go (about) 1 package datafiles 2 3 import ( 4 "bytes" 5 "context" 6 "io/ioutil" 7 "net/http" 8 "net/url" 9 "testing" 10 "time" 11 ) 12 13 type headerer interface { 14 Header() http.Header 15 } 16 17 func TestDataURL(t *testing.T) { 18 uri, err := url.Parse("data:,ohai%2A") 19 if err != nil { 20 t.Fatal("unexpected error parsing constant URL", err) 21 } 22 23 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 24 defer cancel() 25 26 f, err := (&handler{}).Open(ctx, uri) 27 if err != nil { 28 t.Fatal("unexpected error", err) 29 } 30 defer f.Close() 31 32 b, err := ioutil.ReadAll(f) 33 if err != nil { 34 t.Fatal("unexpected error", err) 35 } 36 37 expected := []byte("ohai*") 38 39 if !bytes.Equal(b, expected) { 40 t.Errorf("got wrong content for data:,ohai%%2A got %v, wanted %v", b, expected) 41 } 42 43 h, ok := f.(headerer) 44 if !ok { 45 t.Fatalf("returned files.Reader does not implement interface{ Header() (http.Header, error}") 46 } 47 48 header := h.Header() 49 if err != nil { 50 t.Fatal("unexpected error", err) 51 } 52 53 expectedContentType := "text/plain;charset=US-ASCII" 54 if got := header.Get("Content-Type"); got != expectedContentType { 55 t.Errorf("unexpected Content-Type header, got %q, wanted %q", got, expectedContentType) 56 } 57 } 58 59 func TestDataURLBadBase64(t *testing.T) { 60 uri, err := url.Parse("data:base64,b2hhaSo=") 61 if err != nil { 62 t.Fatal("unexpected error parsing constant URL", err) 63 } 64 65 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 66 defer cancel() 67 68 f, err := (&handler{}).Open(ctx, uri) 69 if err != nil { 70 t.Fatal("unexpected error", err) 71 } 72 defer f.Close() 73 74 b, err := ioutil.ReadAll(f) 75 if err != nil { 76 t.Fatal("unexpected error", err) 77 } 78 79 expected := []byte("b2hhaSo=") 80 81 if !bytes.Equal(b, expected) { 82 t.Errorf("got wrong content for data:base64,b2hhaSo= got %v, wanted %v", b, expected) 83 } 84 } 85 86 func TestDataURLSimpleBase64(t *testing.T) { 87 uri, err := url.Parse("data:;base64,b2hhaSo=") 88 if err != nil { 89 t.Fatal("unexpected error parsing constant URL", err) 90 } 91 92 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 93 defer cancel() 94 95 f, err := (&handler{}).Open(ctx, uri) 96 if err != nil { 97 t.Fatal("unexpected error", err) 98 } 99 defer f.Close() 100 101 b, err := ioutil.ReadAll(f) 102 if err != nil { 103 t.Fatal("unexpected error", err) 104 } 105 106 expected := []byte("ohai*") 107 108 if !bytes.Equal(b, expected) { 109 t.Errorf("got wrong content for data:base64,b2hhaSo= got %v, wanted %v", b, expected) 110 } 111 } 112 113 func TestDataURLComplexBase64(t *testing.T) { 114 uri, err := url.Parse("data:;base64,ohai+/Z=") 115 if err != nil { 116 t.Fatal("unexpected error parsing constant URL", err) 117 } 118 119 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 120 defer cancel() 121 122 f, err := (&handler{}).Open(ctx, uri) 123 if err != nil { 124 t.Fatal("unexpected error", err) 125 } 126 defer f.Close() 127 128 b, err := ioutil.ReadAll(f) 129 if err != nil { 130 t.Fatal("unexpected error", err) 131 } 132 133 expected := []byte{162, 22, 162, 251, 246} 134 135 if !bytes.Equal(b, expected) { 136 t.Errorf("got wrong content for data:base64,ohai+/Z= got %v, wanted %v", b, expected) 137 } 138 } 139 140 func TestDataURLNoComma(t *testing.T) { 141 uri, err := url.Parse("data:ohai%2A") 142 if err != nil { 143 t.Fatal("unexpected error parsing constant URL", err) 144 } 145 146 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 147 defer cancel() 148 149 f, err := (&handler{}).Open(ctx, uri) 150 if err == nil { 151 f.Close() 152 t.Fatal("expected error but got none") 153 } 154 } 155 156 func TestDataURLWithHost(t *testing.T) { 157 uri, err := url.Parse("data://host/,ohai%2A") 158 if err != nil { 159 t.Fatal("unexpected error parsing constant URL", err) 160 } 161 162 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 163 defer cancel() 164 165 f, err := (&handler{}).Open(ctx, uri) 166 if err == nil { 167 f.Close() 168 t.Fatal("expected error but got none") 169 } 170 } 171 172 func TestDataURLWithUser(t *testing.T) { 173 uri, err := url.Parse("data://user@/,ohai%2A") 174 if err != nil { 175 t.Fatal("unexpected error parsing constant URL", err) 176 } 177 178 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 179 defer cancel() 180 181 f, err := (&handler{}).Open(ctx, uri) 182 if err == nil { 183 f.Close() 184 t.Fatal("expected error but got none") 185 } 186 } 187 188 func TestDataWithHeader(t *testing.T) { 189 uriString := "data:type/subtype;foo=bar;base64,b2hhaSo=" 190 uri, err := url.Parse(uriString) 191 if err != nil { 192 t.Fatal("unexpected error parsing constant URL", err) 193 } 194 195 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 196 defer cancel() 197 198 f, err := (&handler{}).Open(ctx, uri) 199 if err != nil { 200 t.Fatal("unexpected error", err) 201 } 202 defer f.Close() 203 204 b, err := ioutil.ReadAll(f) 205 if err != nil { 206 t.Fatal("unexpected error", err) 207 } 208 209 expected := []byte("ohai*") 210 211 if !bytes.Equal(b, expected) { 212 t.Errorf("got wrong content for %s got %v, wanted %v", uriString, b, expected) 213 } 214 215 h, ok := f.(headerer) 216 if !ok { 217 t.Fatalf("returned files.Reader does not implement interface{ Header() (http.Header, error}") 218 } 219 220 header := h.Header() 221 if err != nil { 222 t.Fatal("unexpected error", err) 223 } 224 225 expectedContentType := "type/subtype;foo=bar" 226 if got := header.Get("Content-Type"); got != expectedContentType { 227 t.Errorf("unexpected Content-Type header, got %q, wanted %q", got, expectedContentType) 228 } 229 }