Home / Function/ TestCustomJsonCodec() — gin Function Reference

TestCustomJsonCodec() — gin Function Reference

Architecture documentation for the TestCustomJsonCodec() function in json_test.go from the gin codebase.

Entity Profile

Relationship Graph

Source Code

binding/json_test.go lines 40–63

func TestCustomJsonCodec(t *testing.T) {
	// Restore json encoding configuration after testing
	oldMarshal := json.API
	defer func() {
		json.API = oldMarshal
	}()
	// Custom json api
	json.API = customJsonApi{}

	// test decode json
	obj := customReq{}
	err := jsonBinding{}.BindBody([]byte(`{"time_empty":null,"time_struct": "2001-12-05 10:01:02.345","time_nil":null,"time_pointer":"2002-12-05 10:01:02.345"}`), &obj)
	require.NoError(t, err)
	assert.Equal(t, zeroTime, obj.TimeEmpty)
	assert.Equal(t, time.Date(2001, 12, 5, 10, 1, 2, 345000000, time.Local), obj.TimeStruct)
	assert.Nil(t, obj.TimeNil)
	assert.Equal(t, time.Date(2002, 12, 5, 10, 1, 2, 345000000, time.Local), *obj.TimePointer)
	// test encode json
	w := httptest.NewRecorder()
	err2 := (render.PureJSON{Data: obj}).Render(w)
	require.NoError(t, err2)
	assert.JSONEq(t, "{\"time_empty\":null,\"time_struct\":\"2001-12-05 10:01:02.345\",\"time_nil\":null,\"time_pointer\":\"2002-12-05 10:01:02.345\"}\n", w.Body.String())
	assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
}

Domain

Subdomains

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free