@@ -942,3 +942,124 @@ func TestUpdateUserChatColor(t *testing.T) {
942
942
}
943
943
}
944
944
}
945
+
946
+ func TestSendChatMessage (t * testing.T ) {
947
+ t .Parallel ()
948
+
949
+ testCases := []struct {
950
+ statusCode int
951
+ options * Options
952
+ params * SendChatMessageParams
953
+ respBody string
954
+ err string
955
+ }{
956
+ {
957
+ http .StatusOK ,
958
+ & Options {ClientID : "my-client-id" },
959
+ & SendChatMessageParams {
960
+ BroadcasterID : "1234" ,
961
+ SenderID : "5678" ,
962
+ Message : "Hello, world! twitchdevHype" ,
963
+ },
964
+ `{"data":[{"message_id": "abc-123-def","is_sent": true}]}` ,
965
+ `` ,
966
+ },
967
+ {
968
+ http .StatusOK ,
969
+ & Options {ClientID : "my-client-id" },
970
+ & SendChatMessageParams {
971
+ BroadcasterID : "" ,
972
+ SenderID : "5678" ,
973
+ Message : "Hello, world! twitchdevHype" ,
974
+ },
975
+ `` ,
976
+ `error: broadcaster id must be specified` ,
977
+ },
978
+ {
979
+ http .StatusOK ,
980
+ & Options {ClientID : "my-client-id" },
981
+ & SendChatMessageParams {
982
+ BroadcasterID : "1234" ,
983
+ SenderID : "" ,
984
+ Message : "Hello, world! twitchdevHype" ,
985
+ },
986
+ `` ,
987
+ `error: sender id must be specified` ,
988
+ },
989
+ {
990
+ http .StatusUnauthorized ,
991
+ & Options {ClientID : "my-client-id" },
992
+ & SendChatMessageParams {
993
+ BroadcasterID : "1234" ,
994
+ SenderID : "5678" ,
995
+ Message : "Hello, world! twitchdevHype" ,
996
+ },
997
+ `{"error":"Unauthorized","status":401,"message":"Missing user:write:chat scope"}` , // missing required scope
998
+ `` ,
999
+ },
1000
+ }
1001
+
1002
+ for _ , testCase := range testCases {
1003
+ c := newMockClient (testCase .options , newMockHandler (testCase .statusCode , testCase .respBody , nil ))
1004
+
1005
+ resp , err := c .SendChatMessage (testCase .params )
1006
+ if err != nil {
1007
+ if err .Error () == testCase .err {
1008
+ continue
1009
+ }
1010
+ t .Errorf ("Unmatched error, expected '%v', got '%v'" , testCase .err , err )
1011
+ continue
1012
+ }
1013
+
1014
+ if resp .StatusCode != testCase .statusCode {
1015
+ t .Errorf ("expected status code to be %d, got %d" , testCase .statusCode , resp .StatusCode )
1016
+ }
1017
+
1018
+ if resp .StatusCode == http .StatusUnauthorized {
1019
+ if resp .Error != "Unauthorized" {
1020
+ t .Errorf ("expected error to be \" %s\" , got \" %s\" " , "Unauthorized" , resp .Error )
1021
+ }
1022
+
1023
+ if resp .ErrorStatus != testCase .statusCode {
1024
+ t .Errorf ("expected error status to be %d, got %d" , testCase .statusCode , resp .ErrorStatus )
1025
+ }
1026
+
1027
+ if resp .ErrorMessage != "Missing user:write:chat scope" {
1028
+ t .Errorf ("expected error message to be \" %s\" , got \" %s\" " , "Missing user:write:chat scope" , resp .ErrorMessage )
1029
+ }
1030
+
1031
+ continue
1032
+ }
1033
+
1034
+ if len (resp .Data .Messages ) < 1 {
1035
+ t .Errorf ("Expected the number of messages to be a positive number" )
1036
+ }
1037
+
1038
+ if len (resp .Data .Messages [0 ].MessageID ) == 0 {
1039
+ t .Errorf ("Expected message_id not to be empty" )
1040
+ }
1041
+ }
1042
+
1043
+ // Test with HTTP Failure
1044
+ options := & Options {
1045
+ ClientID : "my-client-id" ,
1046
+ HTTPClient : & badMockHTTPClient {
1047
+ newMockHandler (0 , "" , nil ),
1048
+ },
1049
+ }
1050
+ c := & Client {
1051
+ opts : options ,
1052
+ ctx : context .Background (),
1053
+ }
1054
+
1055
+ _ , err := c .SendChatMessage (& SendChatMessageParams {BroadcasterID : "123" , SenderID : "456" , Message : "Hello, world! twitchdevHype" })
1056
+ if err == nil {
1057
+ t .Error ("expected error but got nil" )
1058
+ }
1059
+
1060
+ const expectedHTTPError = "Failed to execute API request: Oops, that's bad :("
1061
+
1062
+ if err .Error () != expectedHTTPError {
1063
+ t .Errorf ("expected error does match return error, got '%s'" , err .Error ())
1064
+ }
1065
+ }
0 commit comments