ROOTPLOIT
Server: LiteSpeed
System: Linux in-mum-web1878.main-hosting.eu 5.14.0-570.21.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jun 11 07:22:35 EDT 2025 x86_64
User: u435929562 (435929562)
PHP: 7.4.33
Disabled: system, exec, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
Upload Files
File: //opt/go/pkg/mod/github.com/mailru/[email protected]/tests/required_test.go
package tests

import (
	"fmt"
	"reflect"
	"testing"
)

func TestRequiredField(t *testing.T) {
	cases := []struct{ json, errorMessage string }{
		{`{"first_name":"Foo", "last_name": "Bar"}`, ""},
		{`{"last_name":"Bar"}`, "key 'first_name' is required"},
		{"{}", "key 'first_name' is required"},
	}

	for _, tc := range cases {
		var v RequiredOptionalStruct
		err := v.UnmarshalJSON([]byte(tc.json))
		if tc.errorMessage == "" {
			if err != nil {
				t.Errorf("%s. UnmarshalJSON didn`t expect error: %v", tc.json, err)
			}
		} else {
			if fmt.Sprintf("%v", err) != tc.errorMessage {
				t.Errorf("%s. UnmarshalJSON expected error: %v. got: %v", tc.json, tc.errorMessage, err)
			}
		}
	}
}

func TestRequiredOptionalMap(t *testing.T) {
	baseJson := `{"req_map":{}, "oe_map":{}, "noe_map":{}, "oe_slice":[]}`
	wantDecoding := RequiredOptionalMap{MapIntString{}, nil, MapIntString{}}

	var v RequiredOptionalMap
	if err := v.UnmarshalJSON([]byte(baseJson)); err != nil {
		t.Errorf("%s. UnmarshalJSON didn't expect error: %v", baseJson, err)
	}
	if !reflect.DeepEqual(v, wantDecoding) {
		t.Errorf("%s. UnmarshalJSON expected to gen: %v. got: %v", baseJson, wantDecoding, v)
	}

	baseStruct := RequiredOptionalMap{MapIntString{}, MapIntString{}, MapIntString{}}
	wantJson := `{"req_map":{},"noe_map":{}}`
	data, err := baseStruct.MarshalJSON()
	if err != nil {
		t.Errorf("MarshalJSON didn't expect error: %v on %v", err, data)
	} else if string(data) != wantJson {
		t.Errorf("%v. MarshalJSON wanted: %s got %s", baseStruct, wantJson, string(data))
	}
}