Beispiel #1
0
func TestWithAccessLog(t *testing.T) {
	var buf bytes.Buffer
	defer buf.Reset()

	ctx := ctxlog.NewContext(context.Background(), log.NewStdLogger(log.SetStdWriter(&buf)))

	finalH := ctxhttp.Chain(
		ctxhttp.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
			w.WriteHeader(http.StatusTeapot)
			_, err := w.Write([]byte{'1', '2', '3'})
			time.Sleep(time.Millisecond)
			return err
		}),
		ctxhttp.WithAccessLog(),
	)

	r, _ := http.NewRequest("GET", "/gopherine", nil)
	r.RemoteAddr = "127.0.0.1"
	r.Header.Set("User-Agent", "Mozilla")
	r.Header.Set("Referer", "http://rustlang.org")

	w := httptest.NewRecorder()
	if err := finalH.ServeHTTPContext(ctx, w, r); err != nil {
		t.Fatal(err)
	}

	assert.Exactly(t, `123`, w.Body.String())
	assert.Exactly(t, http.StatusTeapot, w.Code)

	want1 := `request error: "" method: "GET" uri: "/gopherine" type: "access" status: "error" status_code: 418 duration:`
	want2 := `size: 3 remote_addr: "127.0.0.1" user_agent: "Mozilla" referer: "http://rustlang.org"`
	assert.Contains(t, buf.String(), want1)
	assert.Contains(t, buf.String(), want2)
}
Beispiel #2
0
func TestStdLoggerFormat(t *testing.T) {

	var buf bytes.Buffer
	var bufInfo bytes.Buffer
	sl := log.NewStdLogger(
		log.SetStdLevel(log.StdLevelDebug),
		log.SetStdWriter(&buf),
		log.SetStdInfo(&bufInfo, "TEST-INFO ", std.LstdFlags),
	)

	sl.Debug("my Debug", 3.14152)
	sl.Debug("my Debug2", "", 2.14152)
	sl.Debug("my Debug3", "key3", 3105, 4711, "Hello")
	sl.Info("InfoTEST")
	sl.Info("InfoTEST", "keyI", 117, 2009)

	aTestErr := errors.New("Cannot run PHP code")
	haveErr := sl.Error("ErrorTEST", "myErr", aTestErr)

	logs := buf.String()
	logsInfo := bufInfo.String()

	//	t.Log("", logs)
	//	t.Log("", logsInfo)

	assert.EqualError(t, haveErr, aTestErr.Error())
	assert.Contains(t, logs, "Debug2")
	assert.Contains(t, logs, "BAD_KEY_AT_INDEX_0")
	assert.Contains(t, logs, `key3: 3105 BAD_KEY_AT_INDEX_2: "Hello"`)
	assert.Contains(t, logs, "_: 3.14")

	assert.Contains(t, logsInfo, "InfoTEST")
	assert.Contains(t, logsInfo, `FIX_IMBALANCED_PAIRS: []interface {}{"keyI", 117, 2009}`)
}
Beispiel #3
0
func TestStdLoggerFormat(t *testing.T) {

	var buf bytes.Buffer
	var bufInfo bytes.Buffer
	sl := log.NewStdLogger(
		log.SetStdLevel(log.StdLevelDebug),
		log.SetStdWriter(&buf),
		log.SetStdInfo(&bufInfo, "TEST-INFO ", std.LstdFlags),
	)

	sl.Debug("my Debug", 3.14152)
	sl.Debug("my Debug2", "", 2.14152)
	sl.Debug("my Debug3", "key3", 3105, 4711, "Hello")
	sl.Info("InfoTEST")
	sl.Info("InfoTEST", "keyI", 117, 2009)
	sl.Info("InfoTEST", "Now we have the salad")

	logs := buf.String()
	logsInfo := bufInfo.String()

	assert.Contains(t, logs, "Debug2")
	assert.Contains(t, logs, "BAD_KEY_AT_INDEX_0")
	assert.Contains(t, logs, `key3: 3105 BAD_KEY_AT_INDEX_2: "Hello"`)

	assert.Contains(t, logsInfo, "InfoTEST")
	assert.Contains(t, logsInfo, `_: "Now we have the salad`)
	assert.Contains(t, logsInfo, `FIX_IMBALANCED_PAIRS: []interface {}{"keyI", 117, 2009}`)
}
Beispiel #4
0
func TestStdLoggerFatal(t *testing.T) {

	defer func() {
		if r := recover(); r != nil {
			assert.Contains(t, r.(string), "This is sparta")
		}
	}()

	var buf bytes.Buffer
	sl := log.NewStdLogger(
		log.SetStdWriter(&buf),
	)
	sl.Fatal("This is sparta")
}
Beispiel #5
0
func TestStdLoggerNewPanic(t *testing.T) {

	defer func() {
		if r := recover(); r != nil {
			if msg, ok := r.(string); ok {
				assert.EqualValues(t, "Arguments to New() can only be StdOption types!", msg)
			} else {
				t.Error("Expecting a string")
			}
		}
	}()

	var buf bytes.Buffer
	sl := log.NewStdLogger(
		log.SetStdWriter(&buf),
	)
	sl.New(log.SetStdLevel(log.StdLevelDebug), 1)
}
Beispiel #6
0
func TestStdLoggerGlobals(t *testing.T) {

	var buf bytes.Buffer
	sl := log.NewStdLogger(
		log.SetStdLevel(log.StdLevelDebug),
		log.SetStdWriter(&buf),
		log.SetStdFlag(std.Ldate),
	)
	sl.Debug("my Debug", "float", 3.14152)
	sl.Debug("my Debug2", 2.14152)
	sl.Info("InfoTEST")

	logs := buf.String()

	assert.NotContains(t, logs, "trace2")
	assert.Contains(t, logs, "InfoTEST")
	assert.NotContains(t, logs, "trace1")
	assert.Contains(t, logs, "Debug2")
}