| 586 | } |
| 587 | |
| 588 | func TestEchoFile(t *testing.T) { |
| 589 | var testCases = []struct { |
| 590 | name string |
| 591 | givenPath string |
| 592 | givenFile string |
| 593 | whenPath string |
| 594 | expectStartsWith string |
| 595 | expectCode int |
| 596 | }{ |
| 597 | { |
| 598 | name: "ok", |
| 599 | givenPath: "/walle", |
| 600 | givenFile: "_fixture/images/walle.png", |
| 601 | whenPath: "/walle", |
| 602 | expectCode: http.StatusOK, |
| 603 | expectStartsWith: string([]byte{0x89, 0x50, 0x4e}), |
| 604 | }, |
| 605 | { |
| 606 | name: "ok with relative path", |
| 607 | givenPath: "/", |
| 608 | givenFile: "./go.mod", |
| 609 | whenPath: "/", |
| 610 | expectCode: http.StatusOK, |
| 611 | expectStartsWith: "module github.com/labstack/echo/v", |
| 612 | }, |
| 613 | { |
| 614 | name: "nok file does not exist", |
| 615 | givenPath: "/", |
| 616 | givenFile: "./this-file-does-not-exist", |
| 617 | whenPath: "/", |
| 618 | expectCode: http.StatusNotFound, |
| 619 | expectStartsWith: "{\"message\":\"Not Found\"}\n", |
| 620 | }, |
| 621 | } |
| 622 | |
| 623 | for _, tc := range testCases { |
| 624 | t.Run(tc.name, func(t *testing.T) { |
| 625 | e := New() // we are using echo.defaultFS instance |
| 626 | e.File(tc.givenPath, tc.givenFile) |
| 627 | |
| 628 | c, b := request(http.MethodGet, tc.whenPath, e) |
| 629 | assert.Equal(t, tc.expectCode, c) |
| 630 | |
| 631 | if len(b) > len(tc.expectStartsWith) { |
| 632 | b = b[:len(tc.expectStartsWith)] |
| 633 | } |
| 634 | assert.Equal(t, tc.expectStartsWith, b) |
| 635 | }) |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | func TestEchoMiddleware(t *testing.T) { |
| 640 | e := New() |