Skip to content

Commit

Permalink
removed embedded context and exposed SetRequest in context
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Oct 11, 2016
1 parent 727b96f commit 15eb5b0
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 261 deletions.
160 changes: 69 additions & 91 deletions context.go

Large diffs are not rendered by default.

94 changes: 0 additions & 94 deletions context/context.go

This file was deleted.

13 changes: 0 additions & 13 deletions context/context_1.7.go

This file was deleted.

13 changes: 0 additions & 13 deletions context/context_pre1.7.go

This file was deleted.

46 changes: 15 additions & 31 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"text/template"
"time"

"golang.org/x/net/context"

"strings"

"net/url"
Expand All @@ -37,7 +35,7 @@ func TestContext(t *testing.T) {
e := New()
req, _ := http.NewRequest(POST, "/", strings.NewReader(userJSON))
rec := httptest.NewRecorder()
c := e.NewContext(req, rec).(*echoContext)
c := e.NewContext(req, rec).(*context)

// Echo
assert.Equal(t, e, c.Echo())
Expand Down Expand Up @@ -68,7 +66,7 @@ func TestContext(t *testing.T) {

// JSON
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*echoContext)
c = e.NewContext(req, rec).(*context)
err = c.JSON(http.StatusOK, user{1, "Jon Snow"})
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
Expand All @@ -78,13 +76,13 @@ func TestContext(t *testing.T) {

// JSON (error)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*echoContext)
c = e.NewContext(req, rec).(*context)
err = c.JSON(http.StatusOK, make(chan bool))
assert.Error(t, err)

// JSONP
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*echoContext)
c = e.NewContext(req, rec).(*context)
callback := "callback"
err = c.JSONP(http.StatusOK, callback, user{1, "Jon Snow"})
if assert.NoError(t, err) {
Expand All @@ -95,7 +93,7 @@ func TestContext(t *testing.T) {

// XML
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*echoContext)
c = e.NewContext(req, rec).(*context)
err = c.XML(http.StatusOK, user{1, "Jon Snow"})
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
Expand All @@ -105,13 +103,13 @@ func TestContext(t *testing.T) {

// XML (error)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*echoContext)
c = e.NewContext(req, rec).(*context)
err = c.XML(http.StatusOK, make(chan bool))
assert.Error(t, err)

// String
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*echoContext)
c = e.NewContext(req, rec).(*context)
err = c.String(http.StatusOK, "Hello, World!")
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
Expand All @@ -121,7 +119,7 @@ func TestContext(t *testing.T) {

// HTML
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*echoContext)
c = e.NewContext(req, rec).(*context)
err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
Expand All @@ -131,7 +129,7 @@ func TestContext(t *testing.T) {

// Stream
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*echoContext)
c = e.NewContext(req, rec).(*context)
r := strings.NewReader("response from a stream")
err = c.Stream(http.StatusOK, "application/octet-stream", r)
if assert.NoError(t, err) {
Expand All @@ -142,7 +140,7 @@ func TestContext(t *testing.T) {

// Attachment
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*echoContext)
c = e.NewContext(req, rec).(*context)
file, err := os.Open("_fixture/images/walle.png")
if assert.NoError(t, err) {
err = c.Attachment(file, "walle.png")
Expand All @@ -155,7 +153,7 @@ func TestContext(t *testing.T) {

// Inline
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*echoContext)
c = e.NewContext(req, rec).(*context)
file, err = os.Open("_fixture/images/walle.png")
if assert.NoError(t, err) {
err = c.Inline(file, "walle.png")
Expand All @@ -168,13 +166,13 @@ func TestContext(t *testing.T) {

// NoContent
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*echoContext)
c = e.NewContext(req, rec).(*context)
c.NoContent(http.StatusOK)
assert.Equal(t, http.StatusOK, rec.Code)

// Error
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*echoContext)
c = e.NewContext(req, rec).(*context)
c.Error(errors.New("error"))
assert.Equal(t, http.StatusInternalServerError, rec.Code)

Expand All @@ -190,7 +188,7 @@ func TestContextCookie(t *testing.T) {
req.Header.Add(HeaderCookie, theme)
req.Header.Add(HeaderCookie, user)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec).(*echoContext)
c := e.NewContext(req, rec).(*context)

// Read single
cookie, err := c.Cookie("theme")
Expand Down Expand Up @@ -351,23 +349,9 @@ func TestContextRedirect(t *testing.T) {
assert.Error(t, c.Redirect(310, "http://labstack.github.io/echo"))
}

func TestContextEmbedded(t *testing.T) {
var c Context
c = new(echoContext)
c.SetStdContext(context.WithValue(c, "key", "val"))
assert.Equal(t, "val", c.Value("key"))
now := time.Now()
ctx, _ := context.WithDeadline(context.Background(), now)
c.SetStdContext(ctx)
n, _ := ctx.Deadline()
assert.Equal(t, now, n)
assert.Equal(t, context.DeadlineExceeded, c.Err())
assert.NotNil(t, c.Done())
}

func TestContextStore(t *testing.T) {
var c Context
c = new(echoContext)
c = new(context)
c.Set("name", "Jon Snow")
assert.Equal(t, "Jon Snow", c.Get("name"))
}
Expand Down
7 changes: 3 additions & 4 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import (

"github.com/rsc/letsencrypt"

"golang.org/x/net/context"
"golang.org/x/net/websocket"

"github.com/labstack/gommon/color"
Expand Down Expand Up @@ -263,10 +262,10 @@ func New() (e *Echo) {

// NewContext returns a Context instance.
func (e *Echo) NewContext(r *http.Request, w http.ResponseWriter) Context {
return &echoContext{
context: context.Background(),
return &context{
request: r,
response: NewResponse(w, e),
store: make(store),
echo: e,
pvalues: make([]string, *e.maxParam),
handler: NotFoundHandler,
Expand Down Expand Up @@ -486,7 +485,7 @@ func (e *Echo) ReleaseContext(c Context) {

// ServeHTTP implements `http.Handler` interface, which serves HTTP requests.
func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c := e.pool.Get().(*echoContext)
c := e.pool.Get().(*context)
c.Reset(r, w)

// Middleware
Expand Down
2 changes: 1 addition & 1 deletion echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func TestEchoHTTPError(t *testing.T) {
func TestEchoContext(t *testing.T) {
e := New()
c := e.AcquireContext()
assert.IsType(t, new(echoContext), c)
assert.IsType(t, new(context), c)
e.ReleaseContext(c)
}

Expand Down
Loading

0 comments on commit 15eb5b0

Please sign in to comment.