-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve_file_test.go
62 lines (44 loc) · 1.79 KB
/
serve_file_test.go
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
package main
import (
"embed"
"io"
"io/fs"
"net/http"
"net/http/httptest"
"testing"
"github.com/julienschmidt/httprouter"
"github.com/stretchr/testify/assert"
)
//go:embed resources
var folderResources embed.FS
// ============================================================================================== //
// 5. Serv file => Untuk get suatu file, tp pake httprouter
func TestServeFile(t *testing.T) {
router := httprouter.New()
// Buat masuk ke sub directory, krn kita gk mau ada keliatan folder (resources/) di dlm url
// kita
directory, _ := fs.Sub(folderResources, "resources")
// NOTES: HARUS PAKE /*filepath => SUDAH DI HARDCODE BAWAAN HTTPROUTER
router.ServeFiles("/*filepath", http.FS(directory))
request := httptest.NewRequest(http.MethodGet, "http://localhost:2020/hello.txt", nil)
recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, request)
response := recorder.Result()
body, _ := io.ReadAll(response.Body)
assert.Equal(t, "This is a test message for serve file module in golang http router", string(body))
}
func TestServeFileSecondFile(t *testing.T) {
router := httprouter.New()
// Buat masuk ke sub directory, krn kita gk mau ada keliatan folder (resources/) di dlm url
// kita
directory, _ := fs.Sub(folderResources, "resources")
// NOTES: HARUS PAKE /*filepath => SUDAH DI HARDCODE BAWAAN HTTPROUTER
router.ServeFiles("/octaDummy/*filepath", http.FS(directory))
request := httptest.NewRequest(http.MethodGet, "http://localhost:2020/octaDummy/goodbye.txt", nil)
recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, request)
response := recorder.Result()
body, _ := io.ReadAll(response.Body)
assert.Equal(t, "Dummy text", string(body))
}
// ============================================================================================== //