(t *testing.T)
| 1376 | } |
| 1377 | |
| 1378 | func TestContext_File(t *testing.T) { |
| 1379 | var testCases = []struct { |
| 1380 | whenFS fs.FS |
| 1381 | name string |
| 1382 | whenFile string |
| 1383 | expectError string |
| 1384 | expectStartsWith []byte |
| 1385 | expectStatus int |
| 1386 | }{ |
| 1387 | { |
| 1388 | name: "ok, from default file system", |
| 1389 | whenFile: "_fixture/images/walle.png", |
| 1390 | whenFS: nil, |
| 1391 | expectStatus: http.StatusOK, |
| 1392 | expectStartsWith: []byte{0x89, 0x50, 0x4e}, |
| 1393 | }, |
| 1394 | { |
| 1395 | name: "ok, from custom file system", |
| 1396 | whenFile: "walle.png", |
| 1397 | whenFS: os.DirFS("_fixture/images"), |
| 1398 | expectStatus: http.StatusOK, |
| 1399 | expectStartsWith: []byte{0x89, 0x50, 0x4e}, |
| 1400 | }, |
| 1401 | { |
| 1402 | name: "nok, not existent file", |
| 1403 | whenFile: "not.png", |
| 1404 | whenFS: os.DirFS("_fixture/images"), |
| 1405 | expectStatus: http.StatusOK, |
| 1406 | expectStartsWith: nil, |
| 1407 | expectError: "Not Found", |
| 1408 | }, |
| 1409 | } |
| 1410 | |
| 1411 | for _, tc := range testCases { |
| 1412 | t.Run(tc.name, func(t *testing.T) { |
| 1413 | e := New() |
| 1414 | if tc.whenFS != nil { |
| 1415 | e.Filesystem = tc.whenFS |
| 1416 | } |
| 1417 | |
| 1418 | handler := func(ec *Context) error { |
| 1419 | return ec.File(tc.whenFile) |
| 1420 | } |
| 1421 | |
| 1422 | req := httptest.NewRequest(http.MethodGet, "/match.png", nil) |
| 1423 | rec := httptest.NewRecorder() |
| 1424 | c := e.NewContext(req, rec) |
| 1425 | |
| 1426 | err := handler(c) |
| 1427 | |
| 1428 | assert.Equal(t, tc.expectStatus, rec.Code) |
| 1429 | if tc.expectError != "" { |
| 1430 | assert.EqualError(t, err, tc.expectError) |
| 1431 | } else { |
| 1432 | assert.NoError(t, err) |
| 1433 | } |
| 1434 | |
| 1435 | body := rec.Body.Bytes() |
nothing calls this directly
no test coverage detected
searching dependent graphs…