Example #1
0
func testServer(t *testing.T) (cancel, step func(), resp <-chan *http.Response) {
	var (
		ctx, cancelfn = context.WithCancel(context.Background())
		stepch        = make(chan bool)
		endpoint      = func(context.Context, interface{}) (interface{}, error) { <-stepch; return struct{}{}, nil }
		response      = make(chan *http.Response)
		handler       = httptransport.Server{
			Context:            ctx,
			Endpoint:           endpoint,
			DecodeRequestFunc:  func(*http.Request) (interface{}, error) { return struct{}{}, nil },
			EncodeResponseFunc: func(http.ResponseWriter, interface{}) error { return nil },
			Before:             []httptransport.RequestFunc{func(ctx context.Context, r *http.Request) context.Context { return ctx }},
			After:              []httptransport.ResponseFunc{func(ctx context.Context, w http.ResponseWriter) { return }},
			Logger:             log.NewNopLogger(),
		}
	)
	go func() {
		server := httptest.NewServer(handler)
		defer server.Close()
		resp, err := http.Get(server.URL)
		if err != nil {
			t.Error(err)
			return
		}
		response <- resp
	}()
	return cancelfn, func() { stepch <- true }, response
}
Example #2
0
func BenchmarkDiscard(b *testing.B) {
	logger := log.NewNopLogger()
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		logger.Log("k", "v")
	}
}
Example #3
0
func TestNopLogger(t *testing.T) {
	logger := log.NewNopLogger()
	if err := logger.Log("abc", 123); err != nil {
		t.Error(err)
	}
	if err := log.NewContext(logger).With("def", "ghi").Log(); err != nil {
		t.Error(err)
	}
}
Example #4
0
func BenchmarkValueBindingTimestamp(b *testing.B) {
	logger := log.NewNopLogger()
	lc := log.NewContext(logger).With("ts", log.DefaultTimestamp)
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		lc.Log("k", "v")
	}
}
Example #5
0
func BenchmarkOneWith(b *testing.B) {
	logger := log.NewNopLogger()
	lc := log.NewContext(logger).With("k", "v")
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		lc.Log("k", "v")
	}
}
Example #6
0
func TestServerBadEncode(t *testing.T) {
	handler := httptransport.Server{
		Context:            context.Background(),
		Endpoint:           func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil },
		DecodeRequestFunc:  func(*http.Request) (interface{}, error) { return struct{}{}, nil },
		EncodeResponseFunc: func(http.ResponseWriter, interface{}) error { return errors.New("dang") },
		Logger:             log.NewNopLogger(),
	}
	server := httptest.NewServer(handler)
	defer server.Close()
	resp, _ := http.Get(server.URL)
	if want, have := http.StatusInternalServerError, resp.StatusCode; want != have {
		t.Errorf("want %d, have %d", want, have)
	}
}
Example #7
0
func TestServerErrorEncoder(t *testing.T) {
	errTeapot := errors.New("teapot")
	code := func(err error) int {
		if err == errTeapot {
			return http.StatusTeapot
		}
		return http.StatusInternalServerError
	}
	handler := httptransport.Server{
		Context:            context.Background(),
		Endpoint:           func(context.Context, interface{}) (interface{}, error) { return struct{}{}, errTeapot },
		DecodeRequestFunc:  func(*http.Request) (interface{}, error) { return struct{}{}, nil },
		EncodeResponseFunc: func(http.ResponseWriter, interface{}) error { return nil },
		ErrorEncoder:       func(w http.ResponseWriter, err error) { w.WriteHeader(code(err)) },
		Logger:             log.NewNopLogger(),
	}
	server := httptest.NewServer(handler)
	defer server.Close()
	resp, _ := http.Get(server.URL)
	if want, have := http.StatusTeapot, resp.StatusCode; want != have {
		t.Errorf("want %d, have %d", want, have)
	}
}