(t *testing.T)
| 140 | } |
| 141 | |
| 142 | func TestNewFileFromURLTimeout(t *testing.T) { |
| 143 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 144 | if r.URL.Path == "/error" { |
| 145 | w.WriteHeader(http.StatusInternalServerError) |
| 146 | } |
| 147 | |
| 148 | fmt.Fprintf(w, "test") |
| 149 | })) |
| 150 | defer srv.Close() |
| 151 | |
| 152 | // cancelled context |
| 153 | { |
| 154 | ctx, cancel := context.WithCancel(context.Background()) |
| 155 | cancel() |
| 156 | f, err := filesystem.NewFileFromURL(ctx, srv.URL+"/cancel") |
| 157 | if err == nil { |
| 158 | t.Fatal("[ctx_cancel] Expected error, got nil") |
| 159 | } |
| 160 | if f != nil { |
| 161 | t.Fatalf("[ctx_cancel] Expected file to be nil, got %v", f) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // error response |
| 166 | { |
| 167 | f, err := filesystem.NewFileFromURL(context.Background(), srv.URL+"/error") |
| 168 | if err == nil { |
| 169 | t.Fatal("[error_status] Expected error, got nil") |
| 170 | } |
| 171 | if f != nil { |
| 172 | t.Fatalf("[error_status] Expected file to be nil, got %v", f) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // valid response |
| 177 | { |
| 178 | originalName := "image_!@ special" |
| 179 | normalizedNamePattern := regexp.QuoteMeta("image_special_") + `\w{10}` + regexp.QuoteMeta(".txt") |
| 180 | |
| 181 | f, err := filesystem.NewFileFromURL(context.Background(), srv.URL+"/"+originalName) |
| 182 | if err != nil { |
| 183 | t.Fatalf("[valid] Unexpected error %v", err) |
| 184 | } |
| 185 | if f == nil { |
| 186 | t.Fatal("[valid] Expected non-nil file") |
| 187 | } |
| 188 | |
| 189 | // check the created file fields |
| 190 | if f.OriginalName != originalName { |
| 191 | t.Fatalf("Expected OriginalName %q, got %q", originalName, f.OriginalName) |
| 192 | } |
| 193 | if match, err := regexp.Match(normalizedNamePattern, []byte(f.Name)); !match { |
| 194 | t.Fatalf("Expected Name to match %v, got %q (%v)", normalizedNamePattern, f.Name, err) |
| 195 | } |
| 196 | if f.Size != 4 { |
| 197 | t.Fatalf("Expected Size %v, got %v", 4, f.Size) |
| 198 | } |
| 199 | if _, ok := f.Reader.(*filesystem.BytesReader); !ok { |
nothing calls this directly
no test coverage detected
searching dependent graphs…