// ParseHistoryResponseForMultipleMessages unmarshalls the response of the history call to the // pubnub api and compares the received messages to the sent messages. If the response match the // test is successful. // // Parameters: // returnChannel: channel to read the response from, // t: a reference to *testing.T, // channel: the pubnub channel to publish the messages, // message: message to compare, // testname: the test name form where this method is called, // startMessagesFrom: the message identifer, // numberOfMessages: number of messages to send, // cipherKey: the cipher key if used. Can be empty. func ParseHistoryResponseForMultipleMessages(returnChannel chan []byte, channel string, message string, testName string, startMessagesFrom int, numberOfMessages int, cipherKey string, responseChannel chan string) { for { value, ok := <-returnChannel if !ok { break } if string(value) != "[]" { data, _, _, err := pubnubMessaging.ParseJson(value, cipherKey) if err != nil { //t.Error("Test '" + testName + "': failed.") responseChannel <- "Test '" + testName + "': failed. Message: " + err.Error() } else { var arr []string err2 := json.Unmarshal([]byte(data), &arr) if err2 != nil { //t.Error("Test '" + testName + "': failed."); responseChannel <- "Test '" + testName + "': failed. Message: " + err2.Error() } else { messagesReceived := 0 if len(arr) != numberOfMessages { responseChannel <- "Test '" + testName + "': failed." //t.Error("Test '" + testName + "': failed."); break } for i := 0; i < numberOfMessages; i++ { if arr[i] == message+strconv.Itoa(startMessagesFrom+i) { //fmt.Println("data:",arr[i]) messagesReceived++ } } if messagesReceived == numberOfMessages { fmt.Println("Test '" + testName + "': passed.") responseChannel <- "Test '" + testName + "': passed." } else { responseChannel <- "Test '" + testName + "': failed. Returned message mismatch" //t.Error("Test '" + testName + "': failed."); } break } } } } }
// TestGermanDecryption tests the German decryption. // Assumes that the input message is deserialized // Decrypted string should match ÜÖ func TestGermanDecryption(t *testing.T) { message := "stpgsG1DZZxb44J7mFNSzg==" //decrypt decrypted, decErr := pubnubMessaging.DecryptString("enigma", message) if decErr != nil { t.Error("German decryption: failed.") } else { data, _, _, err := pubnubMessaging.ParseJson([]byte(decrypted.(string)), "") if err != nil { t.Error("German decryption: failed.", err) } else { if "ÜÖ" == data { fmt.Println("German decryption: passed.") } else { t.Error("German decryption: failed.") } } } }
// TestUnicodeDecryption tests the Unicode decryption. // Assumes that the input message is deserialized // Decrypted string should match 漢語 func TestUnicodeDecryption(t *testing.T) { message := "+BY5/miAA8aeuhVl4d13Kg==" //decrypt decrypted, decErr := pubnubMessaging.DecryptString("enigma", message) if decErr != nil { t.Error("Unicode decryption: failed.") } else { data, _, _, err := pubnubMessaging.ParseJson([]byte(decrypted.(string)), "") if err != nil { t.Error("Unicode decryption: failed.", err) } else { if "漢語" == data { fmt.Println("Unicode decryption: passed.") } else { t.Error("Unicode decryption: failed.") } } } }
// ParseSubscribeResponseForPresence will look for the connection status in the response // received on the go channel. func ParseSubscribeResponseForPresence(pubnubInstance *pubnubMessaging.Pubnub, customUuid string, returnChannel chan []byte, channel string, testName string, responseChannel chan string) { for { value, ok := <-returnChannel if !ok { break } //response := fmt.Sprintf("%s", value) if string(value) != "[]" { if (testName == "CustomUuid") || (testName == "HereNow") || (testName == "HereNowWithCipher") { response := fmt.Sprintf("%s", value) message := "'" + channel + "' connected" messageReconn := "'" + channel + "' reconnected" if (strings.Contains(response, message)) || (strings.Contains(response, messageReconn)) { errorChannel := make(chan []byte) returnChannel := make(chan []byte) time.Sleep(3 * time.Second) go pubnubInstance.HereNow(channel, returnChannel, errorChannel) go ParseHereNowResponse(returnChannel, channel, customUuid, testName, responseChannel) go ParseErrorResponse(errorChannel, responseChannel) break } } else { response := fmt.Sprintf("%s", value) message := "'" + channel + "' connected" messageReconn := "'" + channel + "' reconnected" //fmt.Println("Test3 '" + testName + "':" +response) if (strings.Contains(response, message)) || (strings.Contains(response, messageReconn)) { errorChannel2 := make(chan []byte) returnSubscribeChannel := make(chan []byte) time.Sleep(1 * time.Second) go pubnubInstance.Subscribe(channel, "", returnSubscribeChannel, false, errorChannel2) go ParseResponseDummy(returnSubscribeChannel) go ParseResponseDummy(errorChannel2) } else { if testName == "Presence" { data, _, returnedChannel, err2 := pubnubMessaging.ParseJson(value, "") var occupants []struct { Action string Uuid string Timestamp float64 Occupancy int } if err2 != nil { responseChannel <- "Test '" + testName + "': failed. Message: 1 :" + err2.Error() break } //fmt.Println("Test3 '" + testName + "':" +data) err := json.Unmarshal([]byte(data), &occupants) if err != nil { //fmt.Println("err '" + testName + "':",err) responseChannel <- "Test '" + testName + "': failed. Message: 2 :" + err.Error() break } else { channelSubRepsonseReceived := false for i := 0; i < len(occupants); i++ { if (occupants[i].Action == "join") && occupants[i].Uuid == customUuid { channelSubRepsonseReceived = true break } } if !channelSubRepsonseReceived { responseChannel <- "Test '" + testName + "': failed. Message: err3" break } if channel == returnedChannel { responseChannel <- "Test '" + testName + "': passed." break } else { responseChannel <- "Test '" + testName + "': failed. Message: err4" break } } } } } } } }