// DetailedHistoryFor10Messages is a common method used by both TestDetailedHistoryFor10EncryptedMessages // and TestDetailedHistoryFor10Messages to publish's 10 messages to a pubnub channel, and after that // call the history method of the pubnubMessaging package to fetch last 10 messages. These received // messages are compared to the messages sent and if all match test is successful. func DetailedHistoryFor10Messages(t *testing.T, cipherKey string, testName string) { numberOfMessages := 10 startMessagesFrom := 0 pubnubInstance := pubnubMessaging.PubnubInit("demo", "demo", "", cipherKey, false, "") message := "Test Message " channel := "testChannel" messagesSent := PublishMessages(pubnubInstance, channel, t, startMessagesFrom, numberOfMessages, message) if messagesSent { returnHistoryChannel := make(chan []byte) errorChannel := make(chan []byte) responseChannel := make(chan string) waitChannel := make(chan string) go pubnubInstance.History(channel, numberOfMessages, 0, 0, false, returnHistoryChannel, errorChannel) go ParseHistoryResponseForMultipleMessages(returnHistoryChannel, channel, message, testName, startMessagesFrom, numberOfMessages, cipherKey, responseChannel) go ParseErrorResponse(errorChannel, responseChannel) go WaitForCompletion(responseChannel, waitChannel) ParseWaitResponse(waitChannel, t, testName) } else { t.Error("Test '" + testName + "': failed.") } }
// TestLargeMessage tests the client by publshing a large message // An error "message to large" should be returned from the server func TestLargeMessage(t *testing.T) { pubnubInstance := pubnubMessaging.PubnubInit("demo", "demo", "", "", false, "") channel := "testChannel" message := "This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. This is a large message test which will return an error message. " returnChannel := make(chan []byte) errorChannel := make(chan []byte) responseChannel := make(chan string) waitChannel := make(chan string) go pubnubInstance.Publish(channel, message, returnChannel, errorChannel) go ParseLargeResponse("Message Too Large", errorChannel, responseChannel) go WaitForCompletion(responseChannel, waitChannel) ParseWaitResponse(waitChannel, t, "MessageTooLarge") }
// TestServerTime calls the GetTime method of the pubnubMessaging to test the time func TestServerTime(t *testing.T) { pubnubInstance := pubnubMessaging.PubnubInit("demo", "demo", "", "", false, "") returnTimeChannel := make(chan []byte) errorChannel := make(chan []byte) responseChannel := make(chan string) waitChannel := make(chan string) go pubnubInstance.GetTime(returnTimeChannel, errorChannel) go ParseTimeResponse(returnTimeChannel, responseChannel) go ParseErrorResponse(errorChannel, responseChannel) go WaitForCompletion(responseChannel, waitChannel) ParseWaitResponse(waitChannel, t, "Time") }
// DetailedHistoryFor10Messages is a common method used by both TestDetailedHistoryFor10EncryptedMessages // and TestDetailedHistoryFor10Messages to publish's 10 messages to a pubnub channel, and after that // call the history method of the pubnubMessaging package to fetch last 10 messages with time parameters // between which the messages were sent. These received message is compared to the messages sent and // if all match test is successful. func DetailedHistoryParamsFor10Messages(t *testing.T, cipherKey string, secretKey string, testName string) { numberOfMessages := 5 pubnubInstance := pubnubMessaging.PubnubInit("demo", "demo", secretKey, cipherKey, false, "") message := "Test Message " channel := "testChannel" startTime := GetServerTime(pubnubInstance, t, testName) startMessagesFrom := 0 messagesSent := PublishMessages(pubnubInstance, channel, t, startMessagesFrom, numberOfMessages, message) midTime := GetServerTime(pubnubInstance, t, testName) startMessagesFrom = 5 messagesSent2 := PublishMessages(pubnubInstance, channel, t, startMessagesFrom, numberOfMessages, message) endTime := GetServerTime(pubnubInstance, t, testName) startMessagesFrom = 0 if messagesSent { returnHistoryChannel := make(chan []byte) responseChannel := make(chan string) errorChannel := make(chan []byte) waitChannel := make(chan string) go pubnubInstance.History(channel, numberOfMessages, startTime, midTime, false, returnHistoryChannel, errorChannel) go ParseHistoryResponseForMultipleMessages(returnHistoryChannel, channel, message, testName, startMessagesFrom, numberOfMessages, cipherKey, responseChannel) go ParseErrorResponse(errorChannel, responseChannel) go WaitForCompletion(responseChannel, waitChannel) ParseWaitResponse(waitChannel, t, testName) } else { t.Error("Test '" + testName + "': failed.") } startMessagesFrom = 5 if messagesSent2 { returnHistoryChannel2 := make(chan []byte) errorChannel2 := make(chan []byte) responseChannel2 := make(chan string) waitChannel2 := make(chan string) go pubnubInstance.History(channel, numberOfMessages, midTime, endTime, false, returnHistoryChannel2, errorChannel2) go ParseHistoryResponseForMultipleMessages(returnHistoryChannel2, channel, message, testName, startMessagesFrom, numberOfMessages, cipherKey, responseChannel2) go ParseErrorResponse(errorChannel2, responseChannel2) go WaitForCompletion(responseChannel2, waitChannel2) ParseWaitResponse(waitChannel2, t, testName) } else { t.Error("Test '" + testName + "': failed.") } }
// TestSubscriptionConnectStatus sends out a subscribe request to a pubnub channel // and validates the response for the connect status. func TestSubscriptionConnectStatus(t *testing.T) { pubnubInstance := pubnubMessaging.PubnubInit("demo", "demo", "", "", false, "") channel := "testChannel" returnSubscribeChannel := make(chan []byte) errorChannel := make(chan []byte) responseChannel := make(chan string) waitChannel := make(chan string) go pubnubInstance.Subscribe(channel, "", returnSubscribeChannel, false, errorChannel) go ParseSubscribeResponse(pubnubInstance, returnSubscribeChannel, t, channel, "", "SubscriptionConnectStatus", "", responseChannel) go ParseErrorResponse(errorChannel, responseChannel) go WaitForCompletion(responseChannel, waitChannel) ParseWaitResponse(waitChannel, t, "SubscriptionConnectStatus") }
// HereNow is a common method used by the tests TestHereNow, HereNowWithCipher, CustomUuid // It subscribes to a pubnub channel and then // makes a call to the herenow method of the pubnub api. func HereNow(t *testing.T, cipherKey string, customUuid string, testName string) { pubnubInstance := pubnubMessaging.PubnubInit("demo", "demo", "", cipherKey, false, customUuid) channel := "testChannel" returnSubscribeChannel := make(chan []byte) errorChannel := make(chan []byte) responseChannel := make(chan string) waitChannel := make(chan string) go pubnubInstance.Subscribe(channel, "", returnSubscribeChannel, false, errorChannel) go ParseSubscribeResponseForPresence(pubnubInstance, customUuid, returnSubscribeChannel, channel, testName, responseChannel) go ParseErrorResponse(errorChannel, responseChannel) go WaitForCompletion(responseChannel, waitChannel) ParseWaitResponse(waitChannel, t, testName) }
// TestEncryptedDetailedHistory publish's an encrypted message to a pubnub channel and when the // sent response is received, calls the history method of the pubnubMessaging package to fetch // 1 message. This received message is compared to the message sent and if both match test is successful. func TestEncryptedDetailedHistory(t *testing.T) { pubnubInstance := pubnubMessaging.PubnubInit("demo", "demo", "", "enigma", false, "") channel := "testChannel" message := "Test Message" returnPublishChannel := make(chan []byte) errorChannel := make(chan []byte) responseChannel := make(chan string) waitChannel := make(chan string) go pubnubInstance.Publish(channel, message, returnPublishChannel, errorChannel) go ParseResponse(returnPublishChannel, pubnubInstance, channel, message, "EncryptedDetailedHistory", 1, responseChannel) go ParseErrorResponse(errorChannel, responseChannel) go WaitForCompletion(responseChannel, waitChannel) ParseWaitResponse(waitChannel, t, "EncryptedDetailedHistory") }
// TestSuccessCodeAndInfoWithEncryption sends out an encrypted // message to the pubnub channel // The response is parsed and should match the 'sent' status. // _publishSuccessMessage is defined in the common.go file func TestSuccessCodeAndInfoWithEncryption(t *testing.T) { pubnubInstance := pubnubMessaging.PubnubInit("demo", "demo", "", "enigma", false, "") channel := "testChannel" message := "Pubnub API Usage Example" returnChannel := make(chan []byte) errorChannel := make(chan []byte) responseChannel := make(chan string) waitChannel := make(chan string) go pubnubInstance.Publish(channel, message, returnChannel, errorChannel) go ParsePublishResponse(returnChannel, channel, _publishSuccessMessage, "SuccessCodeAndInfoWithEncryption", responseChannel) go ParseErrorResponse(errorChannel, responseChannel) go WaitForCompletion(responseChannel, waitChannel) ParseWaitResponse(waitChannel, t, "SuccessCodeAndInfoWithEncryption") time.Sleep(2 * time.Second) }
// TestPresence subscribes to the presence notifications on a pubnub channel and // then subscribes to a pubnub channel. The test waits till we get a response from // the subscribe call. The method that parses the presence response sets the global // variable _endPresenceTestAsSuccess to true if the presence contains a join info // on the channel and _endPresenceTestAsFailure is otherwise. func TestPresence(t *testing.T) { customUuid := "customuuid" testName := "Presence" pubnubInstance := pubnubMessaging.PubnubInit("demo", "demo", "", "", false, customUuid) channel := "testForPresenceChannel" returnPresenceChannel := make(chan []byte) errorChannel := make(chan []byte) responseChannel := make(chan string) waitChannel := make(chan string) go pubnubInstance.Subscribe(channel, "", returnPresenceChannel, true, errorChannel) go ParseSubscribeResponseForPresence(pubnubInstance, customUuid, returnPresenceChannel, channel, testName, responseChannel) go ParseResponseDummy(errorChannel) go WaitForCompletion(responseChannel, waitChannel) ParseWaitResponse(waitChannel, t, testName) }
// TestMultiSubscriptionConnectStatus send out a pubnub multi channel subscribe request and // parses the response for multiple connection status. func TestMultiSubscriptionConnectStatus(t *testing.T) { pubnubInstance := pubnubMessaging.PubnubInit("demo", "demo", "", "", false, "") testName := "TestMultiSubscriptionConnectStatus" channels := "testChannel1,testChannel2" returnSubscribeChannel := make(chan []byte) errorChannel := make(chan []byte) responseChannel := make(chan string) waitChannel := make(chan string) go pubnubInstance.Subscribe(channels, "", returnSubscribeChannel, false, errorChannel) go ParseSubscribeResponseForMultipleChannels(returnSubscribeChannel, channels, testName, responseChannel) go ParseErrorResponse(errorChannel, responseChannel) go WaitForCompletion(responseChannel, waitChannel) ParseWaitResponse(waitChannel, t, testName) }
// TestUnsubscribeNotSubscribed will try to unsubscribe a non subscribed pubnub channel. // The response should contain 'not subscribed' func TestUnsubscribeNotSubscribed(t *testing.T) { pubnubInstance := pubnubMessaging.PubnubInit("demo", "demo", "", "", false, "") currentTime := time.Now() channel := "testChannel" + currentTime.Format("20060102150405") returnUnsubscribeChannel := make(chan []byte) errorChannel := make(chan []byte) responseChannel := make(chan string) waitChannel := make(chan string) go pubnubInstance.Unsubscribe(channel, returnUnsubscribeChannel, errorChannel) go ParseUnsubscribeResponse(returnUnsubscribeChannel, channel, "not subscribed", responseChannel) go ParseErrorResponse(errorChannel, responseChannel) go WaitForCompletion(responseChannel, waitChannel) ParseWaitResponse(waitChannel, t, "UnsubscribeNotSubscribed") }
// SendMultipleResponse is the common implementation for TestMultipleResponsed and // TestMultipleResponseEncrypted func SendMultipleResponse(t *testing.T, encrypted bool) { cipher := "" testName := "TestMultipleResponse" if encrypted { cipher = "enigma" testName = "TestMultipleResponseEncrypted" } pubnubInstance := pubnubMessaging.PubnubInit("demo", "demo", "", cipher, false, "") pubnubChannel := "testChannel" returnTimeChannel := make(chan []byte) errorChannelTime := make(chan []byte) go pubnubInstance.GetTime(returnTimeChannel, errorChannelTime) retTime, errTime := ParseTimeFromServer(returnTimeChannel, errorChannelTime) if errTime == nil { message1 := "message1" message2 := "message2" returnPublishChannel := make(chan []byte) errorChannelPub := make(chan []byte) go pubnubInstance.Publish(pubnubChannel, message1, returnPublishChannel, errorChannelPub) b1, _ := ParsePublishResponseFromServer(returnPublishChannel, errorChannelPub) returnPublishChannel2 := make(chan []byte) errorChannelPub2 := make(chan []byte) go pubnubInstance.Publish(pubnubChannel, message2, returnPublishChannel2, errorChannelPub2) b2, _ := ParsePublishResponseFromServer(returnPublishChannel2, errorChannelPub2) if b1 && b2 { returnSubscribeChannel := make(chan []byte) errorChannelSub := make(chan []byte) responseChannelSub := make(chan string) waitChannelSub := make(chan string) go pubnubInstance.Subscribe(pubnubChannel, retTime, returnSubscribeChannel, false, errorChannelSub) go ParseSubscribeMultiResponse(pubnubChannel, returnSubscribeChannel, message1, message2, cipher, testName, responseChannelSub) go ParseErrorResponse(errorChannelSub, responseChannelSub) go WaitForCompletion(responseChannelSub, waitChannelSub) ParseWaitResponse(waitChannelSub, t, testName) } } }
// TestNullMessage sends out a null message to a pubnub channel. The response should // be an "Invalid Message". func TestNullMessage(t *testing.T) { pubnubInstance := pubnubMessaging.PubnubInit("demo", "demo", "", "", false, "") channel := "testChannel" var message interface{} message = nil returnChannel := make(chan []byte) errorChannel := make(chan []byte) responseChannel := make(chan string) waitChannel := make(chan string) go pubnubInstance.Publish(channel, message, returnChannel, errorChannel) //go ParsePublishResponse(returnChannel, channel, "Invalid Message", "NullMessage", responseChannel) go ParseResponseDummy(returnChannel) go ParseErrorResponseForTestSuccess("Invalid Message", errorChannel, responseChannel) //go ParseErrorResponse(errorChannel, responseChannel) go WaitForCompletion(responseChannel, waitChannel) ParseWaitResponse(waitChannel, t, "NullMessage") }
// TestSuccessCodeAndInfoForComplexMessage2 sends out a complex message to the pubnub channel // The response is parsed and should match the 'sent' status. // _publishSuccessMessage and InitComplexMessage is defined in the common.go file func TestSuccessCodeAndInfoForComplexMessage2(t *testing.T) { pubnubInstance := pubnubMessaging.PubnubInit("demo", "demo", "", "", false, "") channel := "testChannel" customComplexMessage := InitComplexMessage() returnChannel := make(chan []byte) errorChannel := make(chan []byte) responseChannel := make(chan string) waitChannel := make(chan string) go pubnubInstance.Publish(channel, customComplexMessage, returnChannel, errorChannel) go ParsePublishResponse(returnChannel, channel, _publishSuccessMessage, "SuccessCodeAndInfoForComplexMessage2", responseChannel) go ParseErrorResponse(errorChannel, responseChannel) go WaitForCompletion(responseChannel, waitChannel) ParseWaitResponse(waitChannel, t, "SuccessCodeAndInfoForComplexMessage2") time.Sleep(2 * time.Second) }
// TestSuccessCodeAndInfoForComplexMessage sends out a complex message to the pubnub channel // The response is parsed and should match the 'sent' status. // _publishSuccessMessage and customstruct is defined in the common.go file func TestSuccessCodeAndInfoForComplexMessage(t *testing.T) { pubnubInstance := pubnubMessaging.PubnubInit("demo", "demo", "", "", false, "") channel := "testChannel" customStruct := CustomStruct{ Foo: "hi!", Bar: []int{1, 2, 3, 4, 5}, } returnChannel := make(chan []byte) errorChannel := make(chan []byte) responseChannel := make(chan string) waitChannel := make(chan string) go pubnubInstance.Publish(channel, customStruct, returnChannel, errorChannel) go ParsePublishResponse(returnChannel, channel, _publishSuccessMessage, "SuccessCodeAndInfoForComplexMessage", responseChannel) go ParseErrorResponse(errorChannel, responseChannel) go WaitForCompletion(responseChannel, waitChannel) ParseWaitResponse(waitChannel, t, "SuccessCodeAndInfoForComplexMessage") time.Sleep(2 * time.Second) }
// ResumeOnReconnect contains the actual impementation of both TestResumeOnReconnectFalse and TestResumeOnReconnectTrue // the parameter b determines of resume on reconnect setting is true or false. func ResumeOnReconnect(t *testing.T, b bool) { testName := "ResumeOnReconnectFalse" if b { pubnubMessaging.SetResumeOnReconnect(true) testName = "ResumeOnReconnectTrue" } else { pubnubMessaging.SetResumeOnReconnect(false) } pubnubChannel := "testChannel" pubnubInstance := pubnubMessaging.PubnubInit("demo", "demo", "", "", false, "") returnSubscribeChannel := make(chan []byte) errorChannelSub := make(chan []byte) responseChannelSub := make(chan string) waitChannelSub := make(chan string) pubnubMessaging.SetSubscribeTimeout(12) go pubnubInstance.Subscribe(pubnubChannel, "", returnSubscribeChannel, false, errorChannelSub) go ParseSubscribeForTimetoken(pubnubInstance, pubnubChannel, returnSubscribeChannel, errorChannelSub, testName, responseChannelSub) go WaitForCompletion(responseChannelSub, waitChannelSub) ParseWaitResponse(waitChannelSub, t, testName) pubnubMessaging.SetSubscribeTimeout(310) }
// SendMultiplexingRequest is the common method to test TestMultiplexing, // TestMultiplexingSSL, TestEncryptedMultiplexing, TestEncryptedMultiplexingWithSSL. // // It subscribes to 2 channels in the same request and then calls the ParseSubscribeMultiplexedResponse // for further processing. // // Parameters: // t: *testing.T instance. // testName: testname for display. // ssl: ssl setting. // encrypted: encryption setting. func SendMultiplexingRequest(t *testing.T, testName string, ssl bool, encrypted bool) { cipher := "" if encrypted { cipher = "enigma" } message1 := "message1" message2 := "message2" pubnubChannel1 := "testChannel1" pubnubChannel2 := "testChannel2" pubnubChannel := pubnubChannel1 + "," + pubnubChannel2 pubnubInstance := pubnubMessaging.PubnubInit("demo", "demo", "", cipher, ssl, "") returnSubscribeChannel := make(chan []byte) errorChannelSub := make(chan []byte) responseChannelSub := make(chan string) waitChannelSub := make(chan string) go pubnubInstance.Subscribe(pubnubChannel, "", returnSubscribeChannel, false, errorChannelSub) go ParseSubscribeMultiplexedResponse(pubnubInstance, returnSubscribeChannel, message1, message2, pubnubChannel1, pubnubChannel2, testName, responseChannelSub) go ParseErrorResponse(errorChannelSub, responseChannelSub) go WaitForCompletion(responseChannelSub, waitChannelSub) ParseWaitResponse(waitChannelSub, t, testName) }
// Init asks the user the basic settings to initialize to the pubnub struct. // Settings include the pubnub channel(s) to connect to. // Ssl settings // Cipher key // Secret Key // Custom Uuid // Proxy details // // The method returns false if the channel name is not provided. // // returns: a bool, true if the user completed the initail settings. func Init() (b bool) { fmt.Println("PubNub Api for go;", pubnubMessaging.VersionInfo()) fmt.Println("Please enter the channel name(s). Enter multiple channels separated by comma without spaces.") reader := bufio.NewReader(os.Stdin) line, _, err := reader.ReadLine() if err != nil { fmt.Println(err) } else { _connectChannels = string(line) if strings.TrimSpace(_connectChannels) != "" { fmt.Println("Channel: ", _connectChannels) fmt.Println("Enable SSL? Enter y for Yes, n for No.") var enableSsl string fmt.Scanln(&enableSsl) if enableSsl == "y" || enableSsl == "Y" { _ssl = true fmt.Println("SSL enabled") } else { _ssl = false fmt.Println("SSL disabled") } fmt.Println("Please enter a CIPHER key, leave blank if you don't want to use this.") fmt.Scanln(&_cipher) fmt.Println("Cipher: ", _cipher) fmt.Println("Please enter a Custom UUID, leave blank for default.") fmt.Scanln(&_uuid) fmt.Println("UUID: ", _uuid) fmt.Println("Display error messages? Enter y for Yes, n for No. Default is Yes") var enableErrorMessages = "y" fmt.Scanln(&enableErrorMessages) if enableErrorMessages == "y" || enableErrorMessages == "Y" { _displayError = true fmt.Println("Error messages will be displayed") } else { _displayError = false fmt.Println("Error messages will not be displayed") } fmt.Println("Enable resume on reconnect? Enter y for Yes, n for No. Default is Yes") var enableResumeOnReconnect = "y" fmt.Scanln(&enableResumeOnReconnect) if enableResumeOnReconnect == "y" || enableResumeOnReconnect == "Y" { pubnubMessaging.SetResumeOnReconnect(true) fmt.Println("Resume on reconnect enabled") } else { pubnubMessaging.SetResumeOnReconnect(false) fmt.Println("Resume on reconnect disabled") } fmt.Println("Set subscribe timeout? Enter numerals.") var subscribeTimeout = "" fmt.Scanln(&subscribeTimeout) val, err := strconv.Atoi(subscribeTimeout) if err != nil { fmt.Println("Entered value is invalid. Using default value.") } else { pubnubMessaging.SetSubscribeTimeout(int64(val)) } pubnubMessaging.SetOrigin("pubsub.pubnub.com") pubInstance := pubnubMessaging.PubnubInit("demo", "demo", "", _cipher, _ssl, _uuid) _pub = pubInstance SetupProxy() return true } else { fmt.Println("Channel cannot be empty.") } } return false }