aboutsummaryrefslogtreecommitdiff
path: root/assetembed/assetembed_test.go
blob: 916d4dcd8ad29af81a66542d115fdf608f95903e (plain)
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
// SPDX-FileCopyrightText: 2025 Stefan Majewsky <majewsky@gmx.net>
// SPDX-License-Identifier: Apache-2.0

package assetembed_test

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"strconv"
	"strings"
	"testing"
	"testing/fstest"
	"time"

	"go.xyrillian.de/gg/assetembed"
	. "go.xyrillian.de/gg/internal/test"
	. "go.xyrillian.de/gg/option"
)

func TestAssetembed(t *testing.T) {
	const (
		fooTxt    = "hello world"
		fooSHA384 = "/b2OdaZ/KfcBpOBAOF4uI5hjA+oQI5IRr5B/y7g1eLPkF8txzmRu/QgZ3YwIjeG9"
		barJS     = "alert('hello')"
		barSHA384 = "nK45OZX/RRKGmhPEj7lSXYQ3NDNNqiBUgbymhjhJ/jpCg0eAyZQ5UuzakE/UFcBd"
	)
	h, err := assetembed.NewHandler(fstest.MapFS{
		"foo.txt": &fstest.MapFile{
			Data:    []byte(fooTxt),
			Mode:    0644,
			ModTime: time.Unix(1, 0),
		},
		"res/assets/bar.js": &fstest.MapFile{
			Data:    []byte(barJS),
			Mode:    0644,
			ModTime: time.Unix(2, 0),
		},
	})
	if err != nil {
		t.Fatal(err.Error())
	}

	// test Handler.AssetPath()
	urlSafe := func(base64str string) string {
		base64str = strings.ReplaceAll(base64str, "+", "-")
		base64str = strings.ReplaceAll(base64str, "/", "_")
		return base64str
	}
	fooTxtPath := fmt.Sprintf("foo-%s.txt", urlSafe(fooSHA384))
	AssertEqual(t, h.AssetPath("foo.txt"), Some(fooTxtPath))
	barJSPath := fmt.Sprintf("res/assets/bar-%s.js", urlSafe(barSHA384))
	AssertEqual(t, h.AssetPath("res/assets/bar.js"), Some(barJSPath))
	AssertEqual(t, h.AssetPath("res/assets/unknown.css"), None[string]())

	// test Handler.SubresourceIntegrity()
	AssertEqual(t, h.SubresourceIntegrity("foo.txt"), Some("sha384-"+fooSHA384))
	AssertEqual(t, h.SubresourceIntegrity("res/assets/bar.js"), Some("sha384-"+barSHA384))
	AssertEqual(t, h.SubresourceIntegrity("res/assets/unknown.css"), None[string]())

	// test HTTP handler
	for _, method := range []string{http.MethodHead, http.MethodGet} {
		t.Logf("testing with method = %q", method)
		probe := func(path string) *httptest.ResponseRecorder {
			req := httptest.NewRequest(method, path, http.NoBody)
			resp := httptest.NewRecorder()
			h.ServeHTTP(resp, req)
			return resp
		}
		ifGet := func(payload string) string {
			if method == http.MethodGet {
				return payload
			} else {
				return ""
			}
		}

		resp := probe("/" + fooTxtPath)
		AssertEqual(t, resp.Code, http.StatusOK)
		AssertEqual(t, resp.Header().Get("Content-Type"), "text/plain; charset=utf-8")
		AssertEqual(t, resp.Header().Get("Content-Length"), strconv.Itoa(len(fooTxt)))
		AssertEqual(t, resp.Body.String(), ifGet(fooTxt))

		resp = probe("/" + barJSPath)
		AssertEqual(t, resp.Code, http.StatusOK)
		AssertEqual(t, resp.Header().Get("Content-Type"), "text/javascript; charset=utf-8")
		AssertEqual(t, resp.Header().Get("Content-Length"), strconv.Itoa(len(barJS)))
		AssertEqual(t, resp.Body.String(), ifGet(barJS))

		resp = probe("/foo.txt") // without digest!
		AssertEqual(t, resp.Code, http.StatusNotFound)

		resp = probe("/res/assets/unknown.css") // entirely unknown file
		AssertEqual(t, resp.Code, http.StatusNotFound)
	}

	req := httptest.NewRequest(http.MethodPut, "/"+fooTxtPath, http.NoBody)
	resp := httptest.NewRecorder()
	h.ServeHTTP(resp, req)
	AssertEqual(t, resp.Code, http.StatusMethodNotAllowed)
}