summaryrefslogtreecommitdiffstats
path: root/internal/server/fileserver_test.go
blob: a6ef9268d2bc9cbea15dfc2f14a1c2c3c63ffc80 (plain) (blame)
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
package server_test

import (
	"net/http"
	"net/http/httptest"
	"testing"
	"testing/fstest"

	"github.com/gin-gonic/gin"
	"iblog/internal/server"
)

func init() { gin.SetMode(gin.TestMode) }

func TestEmbedHandler_ServesFile(t *testing.T) {
	fsys := fstest.MapFS{
		"root/hello.js": {Data: []byte(`console.log("hi")`)},
	}
	r := gin.New()
	r.GET("/assets/*filepath", server.EmbedHandler(fsys, "root"))

	w := httptest.NewRecorder()
	r.ServeHTTP(w, httptest.NewRequest("GET", "/assets/hello.js", nil))

	if w.Code != http.StatusOK {
		t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
	}
	if got := w.Body.String(); got != `console.log("hi")` {
		t.Fatalf("unexpected body: %q", got)
	}
}

func TestEmbedHandler_NotFound(t *testing.T) {
	fsys := fstest.MapFS{}
	r := gin.New()
	r.GET("/assets/*filepath", server.EmbedHandler(fsys, "root"))

	w := httptest.NewRecorder()
	r.ServeHTTP(w, httptest.NewRequest("GET", "/assets/missing.js", nil))

	if w.Code != http.StatusNotFound {
		t.Fatalf("expected 404, got %d", w.Code)
	}
}