Пример #1
0
func ExampleNewContext() {

	handler := func(w http.ResponseWriter, r *http.Request) {
		ctx, cancelFunc := httpctx.NewContext(context.Background(), w, r)
		defer cancelFunc()
		doSomethingWith(ctx, w, r)
	}

	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}
Пример #2
0
func TestNewContext_CancelFunc(t *testing.T) {
	assert := assert.New(t)
	fw := newFakeWriter()
	fr := &http.Request{}
	ctx := context.Background()

	ctx, cancelFunc := httpctx.NewContext(ctx, fw, fr)

	var wg sync.WaitGroup
	finished := false
	wg.Add(1)
	go func(ctx context.Context) {
		<-ctx.Done()
		finished = true
		wg.Done()
	}(ctx)

	cancelFunc()
	wg.Wait()
	assert.True(finished)
}
Пример #3
0
func TestNewContext(t *testing.T) {
	httpctx.NewContext(nil, nil, nil)
}