Ejemplo n.º 1
0
func TestBadURL(t *testing.T) {
	expected := "parse @#$%?auth_test=true: invalid URL escape \"%?a\""
	_, err := New(APIv2, "@#$%", "application/json", authToken)
	if err == nil || err.Error() != expected {
		log.Fatalf("Expected '%s' Got '%s'", err, expected)
	}

	expected = "Get ?auth_test=true: unsupported protocol scheme \"\""
	_, err = New(APIv2, "", "application/json", authToken)
	if err == nil || err.Error() != expected {
		log.Fatalf("Expected '%s' Got '%s'", err, expected)
	}
}
Ejemplo n.º 2
0
func TestNotAccepted(t *testing.T) {

	server := httptest.NewServer(stdhttp.HandlerFunc(func(w stdhttp.ResponseWriter, r *stdhttp.Request) {
		w.WriteHeader(stdhttp.StatusUnauthorized)
	}))
	defer server.Close()

	expected := "HipChat authorization failed\n "
	_, err := New(APIv2, server.URL, "application/json", authToken)
	if err == nil || err.Error() != expected {
		log.Fatalf("Error Expected '%s' Got '%s'", expected, err.Error())
	}
}
Ejemplo n.º 3
0
func TestBadTemplate(t *testing.T) {

	tests := []string{
		"\"color\":\"green\"",
		"\"notify\":true",
		"\"message\":\"\"",
	}

	var msg string

	server := httptest.NewServer(stdhttp.HandlerFunc(func(w stdhttp.ResponseWriter, r *stdhttp.Request) {
		b, err := ioutil.ReadAll(r.Body)
		if err != nil {
			msg = err.Error()
			return
		}

		msg = string(b)

		if strings.Contains(msg, "badrequest") {
			w.WriteHeader(stdhttp.StatusBadRequest)
			return
		}

		w.WriteHeader(stdhttp.StatusAccepted)
	}))
	defer server.Close()

	hc, err := New(APIv2, server.URL, "application/json", authToken)
	if err != nil {
		log.Fatalf("Error initializing hipchat received '%s'", err)
	}

	hc.SetBuffersAndWorkers(1, 1)
	hc.SetTimestampFormat("MST")
	hc.SetEmailTemplate("{{.NonExistantField}}")
	hc.SetFilenameDisplay(log.Llongfile)
	log.RegisterHandler(hc, log.DebugLevel)

	log.Debug("debug test")

	for i, tt := range tests {
		if !strings.Contains(msg, tt) {
			t.Errorf("Index '%d' Expected '%s' Got '%s'", i, tt, msg)
		}
	}
}
Ejemplo n.º 4
0
func TestHTTPLogger(t *testing.T) {

	tests := getTestHTTPLoggerTests()
	var msg string

	server := httptest.NewServer(stdhttp.HandlerFunc(func(w stdhttp.ResponseWriter, r *stdhttp.Request) {
		b, err := ioutil.ReadAll(r.Body)
		if err != nil {
			msg = err.Error()
			return
		}

		msg = string(b)

		if msg == "UTC  DEBUG badrequest" {
			w.WriteHeader(stdhttp.StatusBadRequest)
			return
		}
	}))
	defer server.Close()

	header := make(stdhttp.Header, 0)
	header.Set("Content-Type", "text/plain")

	hLog, err := New(server.URL, "POST", header)
	if err != nil {
		log.Fatalf("Error initializing HTTP received '%s'", err)
	}
	hLog.SetBuffersAndWorkers(0, 0)
	hLog.SetTimestampFormat("MST")
	log.SetCallerInfoLevels(log.WarnLevel, log.ErrorLevel, log.PanicLevel, log.AlertLevel, log.FatalLevel)
	log.RegisterHandler(hLog, log.DebugLevel, log.TraceLevel, log.InfoLevel, log.NoticeLevel, log.WarnLevel, log.PanicLevel, log.AlertLevel, log.FatalLevel)

	for i, tt := range tests {

		var l log.LeveledLogger

		if tt.flds != nil {
			l = log.WithFields(tt.flds...)
		} else {
			l = log.Logger
		}

		switch tt.lvl {
		case log.DebugLevel:
			l.Debug(tt.msg)
		case log.TraceLevel:
			l.Trace(tt.msg).End()
		case log.InfoLevel:
			l.Info(tt.msg)
		case log.NoticeLevel:
			l.Notice(tt.msg)
		case log.WarnLevel:
			l.Warn(tt.msg)
		case log.ErrorLevel:
			l.Error(tt.msg)
		case log.PanicLevel:
			func() {
				defer func() {
					recover()
				}()

				l.Panic(tt.msg)
			}()
		case log.AlertLevel:
			l.Alert(tt.msg)
		}

		if msg != tt.want {

			if tt.lvl == log.TraceLevel {
				if !strings.HasPrefix(msg, tt.want) {
					t.Errorf("test %d: Expected '%s' Got '%s'", i, tt.want, msg)
				}
				continue
			}

			t.Errorf("test %d: Expected '%s' Got '%s'", i, tt.want, msg)
		}
	}

	log.Debug("badrequest")
}
Ejemplo n.º 5
0
func TestSyslogTLS(t *testing.T) {

	// setup server

	addr, err := net.ResolveTCPAddr("tcp", ":2022")
	if err != nil {
		log.Errorf("Expected '%v' Got '%v'", nil, err)
	}

	cnn, err := net.ListenTCP("tcp", addr)
	if err != nil {
		log.Errorf("Expected '%v' Got '%v'", nil, err)
	}
	defer cnn.Close()

	tlsConfig := &tls.Config{
		Certificates: make([]tls.Certificate, 1),
	}

	tlsConfig.Certificates[0], err = tls.X509KeyPair([]byte(publicKey), []byte(privateKey))
	if err != nil {
		log.Errorf("Expected '%v' Got '%v'", nil, err)
	}

	conn := tls.NewListener(cnn, tlsConfig)

	var msg string
	var m sync.Mutex

	go func() {
		client, err := conn.Accept()
		if err != nil {
			log.Errorf("Expected '%v' Got '%v'", nil, err)
		}

		b := make([]byte, 1024)

		read, err := client.Read(b)
		if err != nil {
			log.Errorf("Expected '%v' Got '%v'", nil, err)
		}

		m.Lock()
		defer m.Unlock()
		msg = string(b[0:read])
	}()

	// setup client log

	pool := x509.NewCertPool()
	pool.AppendCertsFromPEM([]byte(publicKey))

	config := &tls.Config{
		RootCAs: pool,
	}

	sLog, err := New("tcp+tls", "127.0.0.1:2022", "", config)
	if err != nil {
		log.Fatalf("Expected '%v' Got '%v'", nil, err)
	}

	//sLog.SetDisplayColor(true)
	// sLog.SetBuffersAndWorkers(3, 0)
	// sLog.SetTimestampFormat("2006")
	sLog.SetFormatFunc(func(s *Syslog) Formatter {
		return func(e *log.Entry) []byte {
			return []byte(e.Message)
		}
	})

	log.RegisterHandler(sLog, log.AllLevels...)

	log.Debug("debug")
	time.Sleep(500 * time.Millisecond)

	m.Lock()
	defer m.Unlock()

	if msg != "debug" {
		log.Errorf("Expected '%s' Got '%s'", "debug", msg)
	}
}