MCPcopy
hub / github.com/gin-gonic/gin / TestContextRenderHTML2

Function TestContextRenderHTML2

context_test.go:1260–1282  ·  context_test.go::TestContextRenderHTML2
(t *testing.T)

Source from the content-addressed store, hash-verified

1258}
1259
1260func TestContextRenderHTML2(t *testing.T) {
1261 w := httptest.NewRecorder()
1262 c, router := CreateTestContext(w)
1263
1264 // print debug warning log when Engine.trees > 0
1265 router.addRoute(http.MethodGet, "/", HandlersChain{func(_ *Context) {}})
1266 assert.Len(t, router.trees, 1)
1267
1268 templ := template.Must(template.New("t").Parse(`Hello {{.name}}`))
1269 re := captureOutput(t, func() {
1270 SetMode(DebugMode)
1271 router.SetHTMLTemplate(templ)
1272 SetMode(TestMode)
1273 })
1274
1275 assert.Equal(t, "[GIN-debug] [WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called\nat initialization. ie. before any route is registered or the router is listening in a socket:\n\n\trouter := gin.Default()\n\trouter.SetHTMLTemplate(template) // << good place\n\n", re)
1276
1277 c.HTML(http.StatusCreated, "t", H{"name": "alexandernyquist"})
1278
1279 assert.Equal(t, http.StatusCreated, w.Code)
1280 assert.Equal(t, "Hello alexandernyquist", w.Body.String())
1281 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
1282}
1283
1284// Tests that no HTML is rendered if code is 204
1285func TestContextRenderNoContentHTML(t *testing.T) {

Callers

nothing calls this directly

Calls 9

CreateTestContextFunction · 0.85
captureOutputFunction · 0.85
SetModeFunction · 0.85
SetHTMLTemplateMethod · 0.80
HTMLMethod · 0.80
StringMethod · 0.65
addRouteMethod · 0.45
GetMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected