Skip to content
Myr

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

4 files+28 added−118 removed

modified:context_17_test.go-49 removed
5 unmodified lines
67package gin89import (10 "bytes"11 "context"10 "net/http"11 "net/http/httptest"12 "testing"15 "time"1314 "github.com/stretchr/testify/assert"15)16
7 unmodified lines
24 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}3132func 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)3839 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 }4647 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)5152 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}6061func 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)7071 assert.NoError(t, c.Err())7273 ti, ok := c.Deadline()74 assert.Equal(t, ti, date)75 assert.True(t, ok)76}
modified:context_17.go-30 removed
5 unmodified lines
67package gin89import (10 "time"1110 "github.com/gin-gonic/gin/render"11)1213// 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.15func (c *Context) PureJSON(code int, obj interface{}) {16 c.Render(code, render.PureJSON{Data: obj})17}2021/************************************/22/***** GOLANG.ORG/X/NET/CONTEXT *****/23/************************************/2425// 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.28func (c *Context) Deadline() (time.Time, bool) {29 return c.Request.Context().Deadline()30}3132// 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.35func (c *Context) Done() <-chan struct{} {36 return c.Request.Context().Done()37}3839// 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.45func (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.45// +build !go1.767package gin89import (10 "time"11)1213/************************************/14/***** GOLANG.ORG/X/NET/CONTEXT *****/15/************************************/1617// 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.20func (c *Context) Deadline() (deadline time.Time, ok bool) {21 return22}2324// 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.27func (c *Context) Done() <-chan struct{} {28 return nil29}3031// 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.37func (c *Context) Err() error {38 return nil39}
modified:context.go+28 added
940 unmodified lines
941func (c *Context) SetAccepted(formats ...string) {942 c.Accepted = formats943}944945/************************************/946/***** GOLANG.ORG/X/NET/CONTEXT *****/947/************************************/948949// 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.952func (c *Context) Deadline() (deadline time.Time, ok bool) {953 return954}955956// 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.959func (c *Context) Done() <-chan struct{} {960 return nil961}962963// 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.969func (c *Context) Err() error {970 return nil971}972973// 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.976func (c *Context) Value(key interface{}) interface{} {
9 unmodified lines