//go:build !remote package utils import ( "net/http/httptest" "reflect" "strings" "testing" ) func TestErrorEncoderFuncOmit(t *testing.T) { data, err := json.Marshal(struct { Err error `json:"err,omitempty"` Errs []error `json:"errs,omitempty"` }{}) if err != nil { t.Fatal(err) } dataAsMap := make(map[string]interface{}) err = json.Unmarshal(data, &dataAsMap) if err != nil { t.Fatal(err) } _, ok := dataAsMap["err"] if ok { t.Errorf("the `err` field should have been omitted") } _, ok = dataAsMap["errs"] if ok { t.Errorf("the `errs` field should have been omitted") } dataAsMap = make(map[string]interface{}) data, err = json.Marshal(struct { Err error `json:"err"` Errs []error `json:"errs"` }{}) if err != nil { t.Fatal(err) } err = json.Unmarshal(data, &dataAsMap) if err != nil { t.Fatal(err) } _, ok = dataAsMap["err"] if !ok { t.Errorf("the `err` field shouldn't have been omitted") } _, ok = dataAsMap["errs"] if !ok { t.Errorf("the `errs` field shouldn't have been omitted") } } func TestWriteJSONNoHTMLEscape(t *testing.T) { // Test that WriteJSON does not HTML-escape JSON content // This test verifies the fix for issue #17769 recorder := httptest.NewRecorder() // Test data with characters that would be HTML-escaped testData := map[string]string{ "message": "Hello & \"friends\"", "script": "", "url": "https://example.com/path?param=value&other=", } WriteJSON(recorder, 200, testData) // Check response headers if contentType := recorder.Header().Get("Content-Type"); contentType != "application/json" { t.Errorf("Expected Content-Type 'application/json', got '%s'", contentType) } // Check that response contains unescaped characters body := recorder.Body.String() // These characters should NOT be HTML-escaped in JSON responses // (but quotes are still properly JSON-escaped) expectedUnescaped := []string{ "", "&", "\\\"friends\\\"", // JSON-escaped quotes, not HTML-escaped "