Testing

Asset Tests

Verify public files, fingerprinted URLs, ETags, and importmaps.

By Guillermo Alvarez - Published - Updated

Request public files

Use the application handler:

response := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodGet, "/styles.css", nil)

appinit.App().ServeHTTP(response, request)

if response.Code != http.StatusOK {
    t.Fatalf("status = %d", response.Code)
}

Check cache headers and validators:

if response.Header().Get("ETag") == "" {
    t.Fatal("ETag is empty")
}

Render a page and extract the permanent stylesheet path:

body := response.Body.String()
match := regexp.MustCompile(`href="([^"]*styles-[^"]+\.css)"`).FindStringSubmatch(body)
if match == nil {
    t.Fatal("fingerprinted stylesheet URL not found")
}

Then request the permanent URL and assert immutable caching:

asset := httptest.NewRecorder()
appinit.App().ServeHTTP(asset, httptest.NewRequest(http.MethodGet, match[1], nil))

if got := asset.Header().Get("Cache-Control"); !strings.Contains(got, "immutable") {
    t.Fatalf("Cache-Control = %q", got)
}

Verify the importmap

Request the generated importmap:

response := httptest.NewRecorder()
appinit.App().ServeHTTP(
    response,
    httptest.NewRequest(http.MethodGet, "/assets/importmap.json", nil),
)

if !strings.Contains(response.Body.String(), `"/js/app.js"`) {
    t.Fatal("importmap does not include app entry")
}