package web

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

	"github.com/gin-gonic/gin"
	"github.com/stretchr/testify/assert"
)

func Test_render(t *testing.T) {
	c, _ := gin.CreateTestContext(httptest.NewRecorder())

	data := gin.H{"foo": "bar"}
	returnStatus := 500
	assert.Equal(t, 200, c.Writer.Status())
	render(c, returnStatus, data)
	assert.Equal(t, 500, c.Writer.Status())
}

func Test_render_rendering(t *testing.T) {
	mode := gin.Mode()
	defer gin.SetMode(mode)

	// gin.IsDebugging() == true
	gin.SetMode(gin.DebugMode)
	rec := httptest.NewRecorder()
	c, _ := gin.CreateTestContext(rec)

	data := gin.H{"foo": "bar"}
	render(c, 200, data)
	expectedResp := "{\n    \"foo\": \"bar\"\n}"
	assert.Equal(t, expectedResp, rec.Body.String())

	// gin.IsDebugging() == false
	gin.SetMode(gin.ReleaseMode)
	rec = httptest.NewRecorder()
	c, _ = gin.CreateTestContext(rec)

	render(c, 200, data)
	expectedResp = `{"foo":"bar"}`
	assert.Equal(t, expectedResp, rec.Body.String())
}
