Revert "context: inherits context cancelation and deadline from http.Request context for Go>=1.7 (#1690)" (#1736)

This reverts commit f67d7a90c4d2e5bdf310a78d7e6a04e3d9aee851.
This commit is contained in:
thinkerou
2019-01-09 09:32:44 +08:00
committed by GitHub
parent d8fb18c33b
commit 29a145c85d
4 changed files with 28 additions and 118 deletions

View File

@ -7,12 +7,9 @@
package gin
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
@ -28,49 +25,3 @@ func TestContextRenderPureJSON(t *testing.T) {
assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String())
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
}
func TestContextHTTPContext(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
req, _ := http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
c.Request = req.WithContext(ctx)
assert.NoError(t, c.Err())
assert.NotNil(t, c.Done())
select {
case <-c.Done():
assert.Fail(t, "context should not be canceled")
default:
}
ti, ok := c.Deadline()
assert.Equal(t, ti, time.Time{})
assert.False(t, ok)
assert.Equal(t, c.Value(0), c.Request)
cancelFunc()
assert.NotNil(t, c.Done())
select {
case <-c.Done():
default:
assert.Fail(t, "context should be canceled")
}
}
func TestContextHTTPContextWithDeadline(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
req, _ := http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
location, _ := time.LoadLocation("Europe/Paris")
assert.NotNil(t, location)
date := time.Date(2031, 12, 27, 16, 00, 00, 00, location)
ctx, cancelFunc := context.WithDeadline(context.Background(), date)
defer cancelFunc()
c.Request = req.WithContext(ctx)
assert.NoError(t, c.Err())
ti, ok := c.Deadline()
assert.Equal(t, ti, date)
assert.True(t, ok)
}