MCPcopy
hub / github.com/labstack/echo / TestContext_FileFS

Function TestContext_FileFS

context_test.go:1444–1498  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1442}
1443
1444func TestContext_FileFS(t *testing.T) {
1445 var testCases = []struct {
1446 whenFS fs.FS
1447 name string
1448 whenFile string
1449 expectError string
1450 expectStartsWith []byte
1451 expectStatus int
1452 }{
1453 {
1454 name: "ok",
1455 whenFile: "walle.png",
1456 whenFS: os.DirFS("_fixture/images"),
1457 expectStatus: http.StatusOK,
1458 expectStartsWith: []byte{0x89, 0x50, 0x4e},
1459 },
1460 {
1461 name: "nok, not existent file",
1462 whenFile: "not.png",
1463 whenFS: os.DirFS("_fixture/images"),
1464 expectStatus: http.StatusOK,
1465 expectStartsWith: nil,
1466 expectError: "Not Found",
1467 },
1468 }
1469
1470 for _, tc := range testCases {
1471 t.Run(tc.name, func(t *testing.T) {
1472 e := New()
1473
1474 handler := func(ec *Context) error {
1475 return ec.FileFS(tc.whenFile, tc.whenFS)
1476 }
1477
1478 req := httptest.NewRequest(http.MethodGet, "/match.png", nil)
1479 rec := httptest.NewRecorder()
1480 c := e.NewContext(req, rec)
1481
1482 err := handler(c)
1483
1484 assert.Equal(t, tc.expectStatus, rec.Code)
1485 if tc.expectError != "" {
1486 assert.EqualError(t, err, tc.expectError)
1487 } else {
1488 assert.NoError(t, err)
1489 }
1490
1491 body := rec.Body.Bytes()
1492 if len(body) > len(tc.expectStartsWith) {
1493 body = body[:len(tc.expectStartsWith)]
1494 }
1495 assert.Equal(t, tc.expectStartsWith, body)
1496 })
1497 }
1498}
1499
1500func TestLogger(t *testing.T) {
1501 e := New()

Callers

nothing calls this directly

Calls 3

NewFunction · 0.85
NewContextMethod · 0.80
FileFSMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…