File: context_fs_test.go

package info (click to toggle)
golang-github-labstack-echo 4.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,524 kB
  • sloc: makefile: 31
file content (135 lines) | stat: -rw-r--r-- 3,245 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors

package echo

import (
	"github.com/stretchr/testify/assert"
	"io/fs"
	"net/http"
	"net/http/httptest"
	"os"
	"testing"
)

func TestContext_File(t *testing.T) {
	var testCases = []struct {
		name             string
		whenFile         string
		whenFS           fs.FS
		expectStatus     int
		expectStartsWith []byte
		expectError      string
	}{
		{
			name:             "ok, from default file system",
			whenFile:         "_fixture/images/walle.png",
			whenFS:           nil,
			expectStatus:     http.StatusOK,
			expectStartsWith: []byte{0x89, 0x50, 0x4e},
		},
		{
			name:             "ok, from custom file system",
			whenFile:         "walle.png",
			whenFS:           os.DirFS("_fixture/images"),
			expectStatus:     http.StatusOK,
			expectStartsWith: []byte{0x89, 0x50, 0x4e},
		},
		{
			name:             "nok, not existent file",
			whenFile:         "not.png",
			whenFS:           os.DirFS("_fixture/images"),
			expectStatus:     http.StatusOK,
			expectStartsWith: nil,
			expectError:      "code=404, message=Not Found",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			e := New()
			if tc.whenFS != nil {
				e.Filesystem = tc.whenFS
			}

			handler := func(ec Context) error {
				return ec.(*context).File(tc.whenFile)
			}

			req := httptest.NewRequest(http.MethodGet, "/match.png", nil)
			rec := httptest.NewRecorder()
			c := e.NewContext(req, rec)

			err := handler(c)

			assert.Equal(t, tc.expectStatus, rec.Code)
			if tc.expectError != "" {
				assert.EqualError(t, err, tc.expectError)
			} else {
				assert.NoError(t, err)
			}

			body := rec.Body.Bytes()
			if len(body) > len(tc.expectStartsWith) {
				body = body[:len(tc.expectStartsWith)]
			}
			assert.Equal(t, tc.expectStartsWith, body)
		})
	}
}

func TestContext_FileFS(t *testing.T) {
	var testCases = []struct {
		name             string
		whenFile         string
		whenFS           fs.FS
		expectStatus     int
		expectStartsWith []byte
		expectError      string
	}{
		{
			name:             "ok",
			whenFile:         "walle.png",
			whenFS:           os.DirFS("_fixture/images"),
			expectStatus:     http.StatusOK,
			expectStartsWith: []byte{0x89, 0x50, 0x4e},
		},
		{
			name:             "nok, not existent file",
			whenFile:         "not.png",
			whenFS:           os.DirFS("_fixture/images"),
			expectStatus:     http.StatusOK,
			expectStartsWith: nil,
			expectError:      "code=404, message=Not Found",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			e := New()

			handler := func(ec Context) error {
				return ec.(*context).FileFS(tc.whenFile, tc.whenFS)
			}

			req := httptest.NewRequest(http.MethodGet, "/match.png", nil)
			rec := httptest.NewRecorder()
			c := e.NewContext(req, rec)

			err := handler(c)

			assert.Equal(t, tc.expectStatus, rec.Code)
			if tc.expectError != "" {
				assert.EqualError(t, err, tc.expectError)
			} else {
				assert.NoError(t, err)
			}

			body := rec.Body.Bytes()
			if len(body) > len(tc.expectStartsWith) {
				body = body[:len(tc.expectStartsWith)]
			}
			assert.Equal(t, tc.expectStartsWith, body)
		})
	}
}