diff --git a/server/test/message_test.go b/server/test/message_test.go index 2dd7e85..8747a12 100644 --- a/server/test/message_test.go +++ b/server/test/message_test.go @@ -42,7 +42,8 @@ func TestSendSimpleMessageJSON(t *testing.T) { }, 401, 1311) tt.AssertEqual(t, "messageCount", 1, len(pusher.Data)) - tt.AssertEqual(t, "msg.title", "HelloWorld_001", pusher.Last().Message.Title) + tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_001", pusher.Last().Message.Title) + tt.AssertStrRepEqual(t, "msg.content", nil, pusher.Last().Message.Content) tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.SCNMessageID) type mglist struct { @@ -53,8 +54,8 @@ func TestSendSimpleMessageJSON(t *testing.T) { tt.AssertEqual(t, "len(messages)", 1, len(msgList1.Messages)) msg1Get := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/messages/"+fmt.Sprintf("%v", msg1["scn_msg_id"])) - tt.AssertEqual(t, "msg.title", "HelloWorld_001", msg1Get["title"]) - tt.AssertEqual(t, "msg.channel_name", "main", msg1Get["channel_name"]) + tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_001", msg1Get["title"]) + tt.AssertStrRepEqual(t, "msg.channel_name", "main", msg1Get["channel_name"]) } func TestSendSimpleMessageQuery(t *testing.T) { @@ -79,7 +80,8 @@ func TestSendSimpleMessageQuery(t *testing.T) { msg1 := tt.RequestPost[gin.H](t, baseUrl, fmt.Sprintf("/?user_id=%d&user_key=%s&title=%s", uid, sendtok, url.QueryEscape("Hello World 2134")), nil) tt.AssertEqual(t, "messageCount", 1, len(pusher.Data)) - tt.AssertEqual(t, "msg.title", "Hello World 2134", pusher.Last().Message.Title) + tt.AssertStrRepEqual(t, "msg.title", "Hello World 2134", pusher.Last().Message.Title) + tt.AssertStrRepEqual(t, "msg.content", nil, pusher.Last().Message.Content) tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.SCNMessageID) type mglist struct { @@ -90,8 +92,8 @@ func TestSendSimpleMessageQuery(t *testing.T) { tt.AssertEqual(t, "len(messages)", 1, len(msgList1.Messages)) msg1Get := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/messages/"+fmt.Sprintf("%v", msg1["scn_msg_id"])) - tt.AssertEqual(t, "msg.title", "Hello World 2134", msg1Get["title"]) - tt.AssertEqual(t, "msg.channel_name", "main", msg1Get["channel_name"]) + tt.AssertStrRepEqual(t, "msg.title", "Hello World 2134", msg1Get["title"]) + tt.AssertStrRepEqual(t, "msg.channel_name", "main", msg1Get["channel_name"]) } func TestSendSimpleMessageForm(t *testing.T) { @@ -120,7 +122,8 @@ func TestSendSimpleMessageForm(t *testing.T) { }) tt.AssertEqual(t, "messageCount", 1, len(pusher.Data)) - tt.AssertEqual(t, "msg.title", "Hello World 9999 [$$$]", pusher.Last().Message.Title) + tt.AssertStrRepEqual(t, "msg.title", "Hello World 9999 [$$$]", pusher.Last().Message.Title) + tt.AssertStrRepEqual(t, "msg.content", nil, pusher.Last().Message.Content) tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.SCNMessageID) type mglist struct { @@ -131,8 +134,8 @@ func TestSendSimpleMessageForm(t *testing.T) { tt.AssertEqual(t, "len(messages)", 1, len(msgList1.Messages)) msg1Get := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/messages/"+fmt.Sprintf("%v", msg1["scn_msg_id"])) - tt.AssertEqual(t, "msg.title", "Hello World 9999 [$$$]", msg1Get["title"]) - tt.AssertEqual(t, "msg.channel_name", "main", msg1Get["channel_name"]) + tt.AssertStrRepEqual(t, "msg.title", "Hello World 9999 [$$$]", msg1Get["title"]) + tt.AssertStrRepEqual(t, "msg.channel_name", "main", msg1Get["channel_name"]) } func TestSendSimpleMessageFormAndQuery(t *testing.T) { @@ -160,7 +163,7 @@ func TestSendSimpleMessageFormAndQuery(t *testing.T) { }) tt.AssertEqual(t, "messageCount", 1, len(pusher.Data)) - tt.AssertEqual(t, "msg.title", "1111111", pusher.Last().Message.Title) + tt.AssertStrRepEqual(t, "msg.title", "1111111", pusher.Last().Message.Title) tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.SCNMessageID) } @@ -189,11 +192,41 @@ func TestSendSimpleMessageJSONAndQuery(t *testing.T) { }) tt.AssertEqual(t, "messageCount", 1, len(pusher.Data)) - tt.AssertEqual(t, "msg.title", "1111111", pusher.Last().Message.Title) + tt.AssertStrRepEqual(t, "msg.title", "1111111", pusher.Last().Message.Title) + tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.SCNMessageID) +} + +func TestSendContentMessage(t *testing.T) { + ws, stop := tt.StartSimpleWebserver(t) + defer stop() + + pusher := ws.Pusher.(*push.TestSink) + + baseUrl := "http://127.0.0.1:" + ws.Port + + r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/users", gin.H{ + "agent_model": "DUMMY_PHONE", + "agent_version": "4X", + "client_type": "ANDROID", + "fcm_token": "DUMMY_FCM", + }) + + uid := int(r0["user_id"].(float64)) + sendtok := r0["send_key"].(string) + + msg1 := tt.RequestPost[gin.H](t, baseUrl, "/", gin.H{ + "user_key": sendtok, + "user_id": uid, + "title": "HelloWorld_042", + "content": "I am Content\nasdf", + }) + + tt.AssertEqual(t, "messageCount", 1, len(pusher.Data)) + tt.AssertStrRepEqual(t, "msg.title", "HelloWorld_042", pusher.Last().Message.Title) + tt.AssertStrRepEqual(t, "msg.content", "I am Content\nasdf", pusher.Last().Message.Content) tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.SCNMessageID) } -//TODO send content //TODO sendername //TODO trim too-long content diff --git a/server/test/util/common.go b/server/test/util/common.go index 1cc0190..ae45d89 100644 --- a/server/test/util/common.go +++ b/server/test/util/common.go @@ -2,6 +2,7 @@ package util import ( "fmt" + "reflect" "runtime/debug" "strings" "testing" @@ -34,19 +35,19 @@ func AssertEqual(t *testing.T, key string, expected any, actual any) { if expected != actual { t.Errorf("Value [%s] differs (%T <-> %T):\n", key, expected, actual) - str1 := fmt.Sprintf("%v", expected) - str2 := fmt.Sprintf("%v", actual) + strExp := fmt.Sprintf("%v", expected) + strAct := fmt.Sprintf("%v", actual) - if strings.Contains(str1, "\n") { - t.Errorf("Actual:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", expected) + if strings.Contains(strAct, "\n") { + t.Errorf("Actual:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", actual) } else { - t.Errorf("Actual := \"%v\"\n", expected) + t.Errorf("Actual := \"%v\"\n", actual) } - if strings.Contains(str2, "\n") { - t.Errorf("Expected:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", actual) + if strings.Contains(strExp, "\n") { + t.Errorf("Expected:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", expected) } else { - t.Errorf("Expected := \"%v\"\n", actual) + t.Errorf("Expected := \"%v\"\n", expected) } t.Error(string(debug.Stack())) @@ -56,22 +57,22 @@ func AssertEqual(t *testing.T, key string, expected any, actual any) { } func AssertStrRepEqual(t *testing.T, key string, expected any, actual any) { - str1 := fmt.Sprintf("%v", expected) - str2 := fmt.Sprintf("%v", actual) + strExp := fmt.Sprintf("%v", unpointer(expected)) + strAct := fmt.Sprintf("%v", unpointer(actual)) - if str1 != str2 { + if strAct != strExp { t.Errorf("Value [%s] differs (%T <-> %T):\n", key, expected, actual) - if strings.Contains(str1, "\n") { - t.Errorf("Actual:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", expected) + if strings.Contains(strAct, "\n") { + t.Errorf("Actual:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", strAct) } else { - t.Errorf("Actual := \"%v\"\n", expected) + t.Errorf("Actual := \"%v\"\n", strAct) } - if strings.Contains(str2, "\n") { - t.Errorf("Expected:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", actual) + if strings.Contains(strExp, "\n") { + t.Errorf("Expected:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", strExp) } else { - t.Errorf("Expected := \"%v\"\n", actual) + t.Errorf("Expected := \"%v\"\n", strExp) } t.Error(string(debug.Stack())) @@ -119,3 +120,12 @@ func TestFailErr(t *testing.T, e error) { t.Error(fmt.Sprintf("Failed with error:\n%s\n\nError:\n%+v\n\nTrace:\n%s", e.Error(), e, string(debug.Stack()))) t.FailNow() } + +func unpointer(v any) any { + val := reflect.ValueOf(v) + if val.Kind() == reflect.Ptr { + val = val.Elem() + return unpointer(val.Interface()) + } + return v +}