Revert "context: inherits context cancelation and deadline from http.Request context for Go>=1.7 (#1690)" (#1736)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This reverts commit f67d7a90c4d2e5bdf310a78d7e6a04e3d9aee851.
authored by thinkerou · committed by GitHub · parent d8fb18c · Browse files · 4 files·+28 added−118 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+28 added−118 removed
- other:+28 added−118 removed
Provenance
- Commit
29a145c- Tree
f78653a- Author
thinkerou <thinkerou@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
d8fb18c
4 files changed+28 added−118 removed
modified:context_17_test.go-49 removed
5 unmodified lines6 7 package gin8 9 import (10- "bytes"11- "context"10 "net/http"11 "net/http/httptest"12 "testing"15- "time"13 14 "github.com/stretchr/testify/assert"15 )16 7 unmodified lines24 assert.Equal(t, http.StatusCreated, w.Code)25 assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String())26 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))27 }31-32-func TestContextHTTPContext(t *testing.T) {33- c, _ := CreateTestContext(httptest.NewRecorder())34- req, _ := http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))35- ctx, cancelFunc := context.WithCancel(context.Background())36- defer cancelFunc()37- c.Request = req.WithContext(ctx)38-39- assert.NoError(t, c.Err())40- assert.NotNil(t, c.Done())41- select {42- case <-c.Done():43- assert.Fail(t, "context should not be canceled")44- default:45- }46-47- ti, ok := c.Deadline()48- assert.Equal(t, ti, time.Time{})49- assert.False(t, ok)50- assert.Equal(t, c.Value(0), c.Request)51-52- cancelFunc()53- assert.NotNil(t, c.Done())54- select {55- case <-c.Done():56- default:57- assert.Fail(t, "context should be canceled")58- }59-}60-61-func TestContextHTTPContextWithDeadline(t *testing.T) {62- c, _ := CreateTestContext(httptest.NewRecorder())63- req, _ := http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))64- location, _ := time.LoadLocation("Europe/Paris")65- assert.NotNil(t, location)66- date := time.Date(2031, 12, 27, 16, 00, 00, 00, location)67- ctx, cancelFunc := context.WithDeadline(context.Background(), date)68- defer cancelFunc()69- c.Request = req.WithContext(ctx)70-71- assert.NoError(t, c.Err())72-73- ti, ok := c.Deadline()74- assert.Equal(t, ti, date)75- assert.True(t, ok)76-}modified:context_17.go-30 removed
5 unmodified lines6 7 package gin8 9 import (10- "time"11-10 "github.com/gin-gonic/gin/render"11 )12 13 // PureJSON serializes the given struct as JSON into the response body.14 // PureJSON, unlike JSON, does not replace special html characters with their unicode entities.15 func (c *Context) PureJSON(code int, obj interface{}) {16 c.Render(code, render.PureJSON{Data: obj})17 }20-21-/************************************/22-/***** GOLANG.ORG/X/NET/CONTEXT *****/23-/************************************/24-25-// Deadline returns the time when work done on behalf of this context26-// should be canceled. Deadline returns ok==false when no deadline is27-// set. Successive calls to Deadline return the same results.28-func (c *Context) Deadline() (time.Time, bool) {29- return c.Request.Context().Deadline()30-}31-32-// Done returns a channel that's closed when work done on behalf of this33-// context should be canceled. Done may return nil if this context can34-// never be canceled. Successive calls to Done return the same value.35-func (c *Context) Done() <-chan struct{} {36- return c.Request.Context().Done()37-}38-39-// Err returns a non-nil error value after Done is closed,40-// successive calls to Err return the same error.41-// If Done is not yet closed, Err returns nil.42-// If Done is closed, Err returns a non-nil error explaining why:43-// Canceled if the context was canceled44-// or DeadlineExceeded if the context's deadline passed.45-func (c *Context) Err() error {46- return c.Request.Context().Err()47-}deleted:context_pre17.go-39 removed
1-// Copyright 2018 Gin Core Team. All rights reserved.2-// Use of this source code is governed by a MIT style3-// license that can be found in the LICENSE file.4-5-// +build !go1.76-7-package gin8-9-import (10- "time"11-)12-13-/************************************/14-/***** GOLANG.ORG/X/NET/CONTEXT *****/15-/************************************/16-17-// Deadline returns the time when work done on behalf of this context18-// should be canceled. Deadline returns ok==false when no deadline is19-// set. Successive calls to Deadline return the same results.20-func (c *Context) Deadline() (deadline time.Time, ok bool) {21- return22-}23-24-// Done returns a channel that's closed when work done on behalf of this25-// context should be canceled. Done may return nil if this context can26-// never be canceled. Successive calls to Done return the same value.27-func (c *Context) Done() <-chan struct{} {28- return nil29-}30-31-// Err returns a non-nil error value after Done is closed,32-// successive calls to Err return the same error.33-// If Done is not yet closed, Err returns nil.34-// If Done is closed, Err returns a non-nil error explaining why:35-// Canceled if the context was canceled36-// or DeadlineExceeded if the context's deadline passed.37-func (c *Context) Err() error {38- return nil39-}modified:context.go+28 added
940 unmodified lines941 func (c *Context) SetAccepted(formats ...string) {942 c.Accepted = formats943 }944 945+/************************************/946+/***** GOLANG.ORG/X/NET/CONTEXT *****/947+/************************************/948+949+// Deadline returns the time when work done on behalf of this context950+// should be canceled. Deadline returns ok==false when no deadline is951+// set. Successive calls to Deadline return the same results.952+func (c *Context) Deadline() (deadline time.Time, ok bool) {953+ return954+}955+956+// Done returns a channel that's closed when work done on behalf of this957+// context should be canceled. Done may return nil if this context can958+// never be canceled. Successive calls to Done return the same value.959+func (c *Context) Done() <-chan struct{} {960+ return nil961+}962+963+// Err returns a non-nil error value after Done is closed,964+// successive calls to Err return the same error.965+// If Done is not yet closed, Err returns nil.966+// If Done is closed, Err returns a non-nil error explaining why:967+// Canceled if the context was canceled968+// or DeadlineExceeded if the context's deadline passed.969+func (c *Context) Err() error {970+ return nil971+}972+973 // Value returns the value associated with this context for key, or nil974 // if no value is associated with key. Successive calls to Value with975 // the same key returns the same result.976 func (c *Context) Value(key interface{}) interface{} {9 unmodified lines