func TestClient_OnfailureInvalidParams(t *testing.T) {
	category := strings.Repeat("testing", 150)
	u, _ := testServer()
	opts := ClientOptions{
		SubscribeUrl:         u,
		Category:             category,
		PollTimeoutSeconds:   1,
		ReattemptWaitSeconds: 1,
		LoggingEnabled:       true,
		OnFailure:            getOnFailureTestHandler(),
	}
	c, err := NewClient(opts)
	if err != nil {
		t.Fatalf("Error creating client: %v", err)
	}
	onFailureTestCase(t, c)
}
Ensure OnFailure gets called and works as intended.
In this test, an invalid category is used.
This will trigger an api level error where HTTP 200 response but
the returned json will have an error message.
package client
import (
	"fmt"
	"log"
	"net"
	"net/http"
	"net/url"
	"strings"
	"testing"
	"time"
	"github.com/jcuga/golongpoll"
)
func testEventsManager() *golongpoll.LongpollManager {
	eventsManager, err := golongpoll.StartLongpoll(golongpoll.Options{
		LoggingEnabled: true,
	})
	if err != nil {
		fmt.Println("Failed to create manager: ", err)
	}
	return eventsManager
}
func testServer() (url.URL, *golongpoll.LongpollManager) {
	eventsManager := testEventsManager()
	mux := http.NewServeMux()
	mux.HandleFunc("/events", eventsManager.SubscriptionHandler)
	mux.HandleFunc("/publish", eventsManager.PublishHandler)
	listener, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		panic(fmt.Sprintf("Failed to listen on address, error: %v", err))
	}
	server := &http.Server{Handler: mux}
	fmt.Println("Tests event server listening on", listener.Addr().String())
	go func() {
		panic(server.Serve(listener))
	}()
	u, _ := url.Parse("http://" + listener.Addr().String() + "/events")
	return *u, eventsManager
}
func testAuthServer(username, password string) (url.URL, *golongpoll.LongpollManager) {
	eventsManager := testEventsManager()
	var hf http.HandlerFunc = func(w http.ResponseWriter, req *http.Request) {
		reqUsername, reqPassword, _ := req.BasicAuth()
		if reqUsername == username && reqPassword == password {
			eventsManager.SubscriptionHandler(w, req)
		} else {
			w.WriteHeader(401)
		}
	}
	listener, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		panic(err)
	}
	fmt.Println("Tests event server listening on", listener.Addr().String())
	go func() {
		panic(http.Serve(listener, hf))
	}()
	u, _ := url.Parse("http://" + listener.Addr().String() + "/events")
	return *u, eventsManager
}
func TestClient_Events(t *testing.T) {
	category := "testing"
	u, manager := testServer()
	opts := ClientOptions{
		SubscribeUrl:         u,
		Category:             category,
		PollTimeoutSeconds:   1,
		ReattemptWaitSeconds: 1,
		LoggingEnabled:       true,
	}
	c, err := NewClient(opts)
	if err != nil {
		t.Fatalf("Error creating client: %v", err)
	}
	events := c.Start(time.Now())
	expectedResults := []string{"test1", "test2", "test3", "test4", "test5", "test6"}
	for num, result := range expectedResults {
		manager.Publish(category, result)
		if num == 3 {
			time.Sleep(time.Duration(2) * time.Second)
		}
	}
	for i := 0; i < len(expectedResults); i++ {
		select {
		case e, ok := <-events:
			if !ok {
				c.Stop()
				t.Fatal("Unexpected channel close.")
			}
			data, ok := e.Data.(string)
			if !ok {
				t.Errorf("Expected data to be a sring, got: %T", e.Data)
			} else if data != expectedResults[i] {
				t.Errorf("Unexpected data value, expected: %v, got: %v", expectedResults[i], data)
			}
		case <-time.After(3 * time.Second):
			t.Error("Should have seen events.")
		}
	}
	c.Stop()
	manager.Publish(category, "testNope")
	select {
	case e, ok := <-events:
		if ok {
			t.Error("Expected channel close")
		}
		if e != nil {
			t.Errorf("Expected nil Event, got: %v", e)
		}
	case <-time.After(3 * time.Second):
		t.Error("Should have seen a channel close.")
	}
}
func TestClient_PastEvents(t *testing.T) {
	category := "testing"
	u, manager := testServer()
	opts := ClientOptions{
		SubscribeUrl:         u,
		Category:             category,
		PollTimeoutSeconds:   1,
		ReattemptWaitSeconds: 1,
		LoggingEnabled:       true,
	}
	expectedResults := []string{"test1", "test2", "test3"}
	for _, result := range expectedResults {
		manager.Publish(category, result)
		time.Sleep(time.Duration(1) * time.Second)
	}
	time.Sleep(time.Duration(1) * time.Second)
	c, err := NewClient(opts)
	if err != nil {
		t.Fatalf("Error creating client: %v", err)
	}
	events := c.Start(time.Now().Add(-5 * time.Second))
	for i := 0; i < len(expectedResults); i++ {
		select {
		case e, ok := <-events:
			if !ok {
				c.Stop()
				t.Fatal("Unexpected channel close.")
			}
			data, ok := e.Data.(string)
			if !ok {
				t.Errorf("Expected data to be a sring, got: %T", e.Data)
			} else if data != expectedResults[i] {
				t.Errorf("Unexpected data value, expected: %v, got: %v", expectedResults[i], data)
			}
		case <-time.After(3 * time.Second):
			t.Error("Should have seen events.")
		}
	}
	c.Stop()
}
func TestClient_HttpBasicAuthentication(t *testing.T) {
	category := "testing"
	testUser := "bob"
	testPassword := "bob"
	u, manager := testAuthServer(testUser, testPassword)
	opts := ClientOptions{
		SubscribeUrl:         u,
		Category:             category,
		PollTimeoutSeconds:   1,
		ReattemptWaitSeconds: 1,
		LoggingEnabled:       true,
		BasicAuthUsername:    testUser,
		BasicAuthPassword:    testPassword,
	}
	c, err := NewClient(opts)
	if err != nil {
		t.Fatalf("Error creating client: %v", err)
	}
	events := c.Start(time.Now())
	manager.Publish(category, "test")
	select {
	case e, ok := <-events:
		if !ok {
			c.Stop()
			t.Fatal("Unexpected channel close.")
		}
		data, ok := e.Data.(string)
		if !ok {
			t.Errorf("Expected data to be a sring, got: %T", e.Data)
		} else if data != "test" {
			t.Errorf("Unexpected data value, expected: %v, got: %v", "test", data)
		}
	case <-time.After(3 * time.Second):
		t.Error("Error selecting from the events channel")
	}
	c.Stop()
	opts = ClientOptions{
		SubscribeUrl:         u,
		Category:             category,
		PollTimeoutSeconds:   1,
		ReattemptWaitSeconds: 1,
		LoggingEnabled:       true,
		BasicAuthUsername:    testUser,
		BasicAuthPassword:    "notThePassword",
	}
	c, err = NewClient(opts)
	if err != nil {
		t.Fatalf("Error creating client: %v", err)
	}
	events = c.Start(time.Now())
	defer c.Stop()
	manager.Publish(category, "test")
	select {
	case e, ok := <-events:
		t.Error("Received something in the events channel although the requests are unauthorized")
		if e != nil {
			t.Errorf("unexpected event: %v", e)
		}
		t.Errorf("Channel ok: %v", ok)
	case <-time.After(3 * time.Second):
	}
}