Skip to content

Commit

Permalink
Refactored variable names
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Apr 24, 2016
1 parent 61fabee commit be825e0
Show file tree
Hide file tree
Showing 23 changed files with 300 additions and 303 deletions.
3 changes: 1 addition & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 LabStack
Copyright (c) 2016 LabStack

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -19,4 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

24 changes: 12 additions & 12 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,19 +452,19 @@ func (c *context) Logger() *log.Logger {
}

func (c *context) ServeContent(content io.ReadSeeker, name string, modtime time.Time) error {
rq := c.Request()
rs := c.Response()
req := c.Request()
res := c.Response()

if t, err := time.Parse(http.TimeFormat, rq.Header().Get(HeaderIfModifiedSince)); err == nil && modtime.Before(t.Add(1*time.Second)) {
rs.Header().Del(HeaderContentType)
rs.Header().Del(HeaderContentLength)
if t, err := time.Parse(http.TimeFormat, req.Header().Get(HeaderIfModifiedSince)); err == nil && modtime.Before(t.Add(1*time.Second)) {
res.Header().Del(HeaderContentType)
res.Header().Del(HeaderContentLength)
return c.NoContent(http.StatusNotModified)
}

rs.Header().Set(HeaderContentType, ContentTypeByExtension(name))
rs.Header().Set(HeaderLastModified, modtime.UTC().Format(http.TimeFormat))
rs.WriteHeader(http.StatusOK)
_, err := io.Copy(rs, content)
res.Header().Set(HeaderContentType, ContentTypeByExtension(name))
res.Header().Set(HeaderLastModified, modtime.UTC().Format(http.TimeFormat))
res.WriteHeader(http.StatusOK)
_, err := io.Copy(res, content)
return err
}

Expand All @@ -478,10 +478,10 @@ func ContentTypeByExtension(name string) (t string) {
return
}

func (c *context) Reset(rq engine.Request, rs engine.Response) {
func (c *context) Reset(req engine.Request, res engine.Response) {
c.netContext = nil
c.request = rq
c.response = rs
c.request = req
c.response = res
c.store = nil
c.handler = notFoundHandler
}
126 changes: 63 additions & 63 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func TestContext(t *testing.T) {
invalidContent := "invalid content"

e := New()
rq := test.NewRequest(POST, "/", strings.NewReader(userJSON))
rc := test.NewResponseRecorder()
c := e.NewContext(rq, rc).(*context)
req := test.NewRequest(POST, "/", strings.NewReader(userJSON))
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec).(*context)

// Request
assert.NotNil(t, c.Request())
Expand Down Expand Up @@ -89,111 +89,111 @@ func TestContext(t *testing.T) {
c.echo.SetRenderer(tpl)
err := c.Render(http.StatusOK, "hello", "Joe")
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rc.Status())
assert.Equal(t, "Hello, Joe!", rc.Body.String())
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, "Hello, Joe!", rec.Body.String())
}

c.echo.renderer = nil
err = c.Render(http.StatusOK, "hello", "Joe")
assert.Error(t, err)

// JSON
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc).(*context)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*context)
err = c.JSON(http.StatusOK, user{"1", "Joe"})
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rc.Status())
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rc.Header().Get(HeaderContentType))
assert.Equal(t, userJSON, rc.Body.String())
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, userJSON, rec.Body.String())
}

// JSON (error)
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc).(*context)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*context)
err = c.JSON(http.StatusOK, make(chan bool))
assert.Error(t, err)

// JSONP
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc).(*context)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*context)
callback := "callback"
err = c.JSONP(http.StatusOK, callback, user{"1", "Joe"})
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rc.Status())
assert.Equal(t, MIMEApplicationJavaScriptCharsetUTF8, rc.Header().Get(HeaderContentType))
assert.Equal(t, callback+"("+userJSON+");", rc.Body.String())
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, MIMEApplicationJavaScriptCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, callback+"("+userJSON+");", rec.Body.String())
}

// XML
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc).(*context)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*context)
err = c.XML(http.StatusOK, user{"1", "Joe"})
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rc.Status())
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rc.Header().Get(HeaderContentType))
assert.Equal(t, xml.Header+userXML, rc.Body.String())
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, xml.Header+userXML, rec.Body.String())
}

// XML (error)
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc).(*context)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*context)
err = c.XML(http.StatusOK, make(chan bool))
assert.Error(t, err)

// String
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc).(*context)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*context)
err = c.String(http.StatusOK, "Hello, World!")
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rc.Status())
assert.Equal(t, MIMETextPlainCharsetUTF8, rc.Header().Get(HeaderContentType))
assert.Equal(t, "Hello, World!", rc.Body.String())
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, MIMETextPlainCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, "Hello, World!", rec.Body.String())
}

// HTML
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc).(*context)
rec = test.NewResponseRecorder()
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, rc.Status())
assert.Equal(t, MIMETextHTMLCharsetUTF8, rc.Header().Get(HeaderContentType))
assert.Equal(t, "Hello, <strong>World!</strong>", rc.Body.String())
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, MIMETextHTMLCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
}

// Attachment
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc).(*context)
rec = test.NewResponseRecorder()
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")
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rc.Status())
assert.Equal(t, "attachment; filename=walle.png", rc.Header().Get(HeaderContentDisposition))
assert.Equal(t, 219885, rc.Body.Len())
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, "attachment; filename=walle.png", rec.Header().Get(HeaderContentDisposition))
assert.Equal(t, 219885, rec.Body.Len())
}
}

// NoContent
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc).(*context)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*context)
c.NoContent(http.StatusOK)
assert.Equal(t, http.StatusOK, rc.Status())
assert.Equal(t, http.StatusOK, rec.Status())

// Redirect
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc).(*context)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*context)
assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo"))
assert.Equal(t, http.StatusMovedPermanently, rc.Status())
assert.Equal(t, "http://labstack.github.io/echo", rc.Header().Get(HeaderLocation))
assert.Equal(t, http.StatusMovedPermanently, rec.Status())
assert.Equal(t, "http://labstack.github.io/echo", rec.Header().Get(HeaderLocation))

// Error
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc).(*context)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*context)
c.Error(errors.New("error"))
assert.Equal(t, http.StatusInternalServerError, rc.Status())
assert.Equal(t, http.StatusInternalServerError, rec.Status())

// Reset
c.Reset(rq, test.NewResponseRecorder())
c.Reset(req, test.NewResponseRecorder())
}

func TestContextPath(t *testing.T) {
Expand All @@ -215,9 +215,9 @@ func TestContextQueryParam(t *testing.T) {
q := make(url.Values)
q.Set("name", "joe")
q.Set("email", "[email protected]")
rq := test.NewRequest(GET, "/?"+q.Encode(), nil)
req := test.NewRequest(GET, "/?"+q.Encode(), nil)
e := New()
c := e.NewContext(rq, nil)
c := e.NewContext(req, nil)
assert.Equal(t, "joe", c.QueryParam("name"))
assert.Equal(t, "[email protected]", c.QueryParam("email"))
}
Expand All @@ -228,10 +228,10 @@ func TestContextFormValue(t *testing.T) {
f.Set("email", "[email protected]")

e := New()
rq := test.NewRequest(POST, "/", strings.NewReader(f.Encode()))
rq.Header().Add(HeaderContentType, MIMEApplicationForm)
req := test.NewRequest(POST, "/", strings.NewReader(f.Encode()))
req.Header().Add(HeaderContentType, MIMEApplicationForm)

c := e.NewContext(rq, nil)
c := e.NewContext(req, nil)
assert.Equal(t, "joe", c.FormValue("name"))
assert.Equal(t, "[email protected]", c.FormValue("email"))
}
Expand All @@ -244,9 +244,9 @@ func TestContextNetContext(t *testing.T) {

func TestContextServeContent(t *testing.T) {
e := New()
rq := test.NewRequest(GET, "/", nil)
rc := test.NewResponseRecorder()
c := e.NewContext(rq, rc)
req := test.NewRequest(GET, "/", nil)
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)

fs := http.Dir("_fixture/images")
f, err := fs.Open("walle.png")
Expand All @@ -255,15 +255,15 @@ func TestContextServeContent(t *testing.T) {
if assert.NoError(t, err) {
// Not cached
if assert.NoError(t, c.ServeContent(f, fi.Name(), fi.ModTime())) {
assert.Equal(t, http.StatusOK, rc.Status())
assert.Equal(t, http.StatusOK, rec.Status())
}

// Cached
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc)
rq.Header().Set(HeaderIfModifiedSince, fi.ModTime().UTC().Format(http.TimeFormat))
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
req.Header().Set(HeaderIfModifiedSince, fi.ModTime().UTC().Format(http.TimeFormat))
if assert.NoError(t, c.ServeContent(f, fi.Name(), fi.ModTime())) {
assert.Equal(t, http.StatusNotModified, rc.Status())
assert.Equal(t, http.StatusNotModified, rec.Status())
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ func New() (e *Echo) {
}

// NewContext returns a Context instance.
func (e *Echo) NewContext(rq engine.Request, rs engine.Response) Context {
func (e *Echo) NewContext(req engine.Request, res engine.Response) Context {
return &context{
request: rq,
response: rs,
request: req,
response: res,
echo: e,
pvalues: make([]string, *e.maxParam),
store: make(store),
Expand Down Expand Up @@ -520,14 +520,14 @@ func (e *Echo) PutContext(c Context) {
e.pool.Put(c)
}

func (e *Echo) ServeHTTP(rq engine.Request, rs engine.Response) {
func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) {
c := e.pool.Get().(*context)
c.Reset(rq, rs)
c.Reset(req, res)

// Middleware
h := func(Context) error {
method := rq.Method()
path := rq.URL().Path()
method := req.Method()
path := req.URL().Path()
e.router.Find(method, path, c)
h := c.handler
for i := len(e.middleware) - 1; i >= 0; i-- {
Expand Down Expand Up @@ -575,15 +575,15 @@ func (e *HTTPError) Error() string {
}

func (b *binder) Bind(i interface{}, c Context) (err error) {
rq := c.Request()
ct := rq.Header().Get(HeaderContentType)
req := c.Request()
ct := req.Header().Get(HeaderContentType)
err = ErrUnsupportedMediaType
if strings.HasPrefix(ct, MIMEApplicationJSON) {
if err = json.NewDecoder(rq.Body()).Decode(i); err != nil {
if err = json.NewDecoder(req.Body()).Decode(i); err != nil {
err = NewHTTPError(http.StatusBadRequest, err.Error())
}
} else if strings.HasPrefix(ct, MIMEApplicationXML) {
if err = xml.NewDecoder(rq.Body()).Decode(i); err != nil {
if err = xml.NewDecoder(req.Body()).Decode(i); err != nil {
err = NewHTTPError(http.StatusBadRequest, err.Error())
}
}
Expand Down
20 changes: 10 additions & 10 deletions echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ type (

func TestEcho(t *testing.T) {
e := New()
rq := test.NewRequest(GET, "/", nil)
rc := test.NewResponseRecorder()
c := e.NewContext(rq, rc)
req := test.NewRequest(GET, "/", nil)
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)

// Router
assert.NotNil(t, e.Router())
Expand All @@ -37,7 +37,7 @@ func TestEcho(t *testing.T) {

// DefaultHTTPErrorHandler
e.DefaultHTTPErrorHandler(errors.New("error"), c)
assert.Equal(t, http.StatusInternalServerError, rc.Status())
assert.Equal(t, http.StatusInternalServerError, rec.Status())
}

func TestEchoStatic(t *testing.T) {
Expand Down Expand Up @@ -289,9 +289,9 @@ func TestEchoGroup(t *testing.T) {

func TestEchoNotFound(t *testing.T) {
e := New()
rq := test.NewRequest(GET, "/files", nil)
req := test.NewRequest(GET, "/files", nil)
rec := test.NewResponseRecorder()
e.ServeHTTP(rq, rec)
e.ServeHTTP(req, rec)
assert.Equal(t, http.StatusNotFound, rec.Status())
}

Expand All @@ -300,9 +300,9 @@ func TestEchoMethodNotAllowed(t *testing.T) {
e.GET("/", func(c Context) error {
return c.String(http.StatusOK, "Echo!")
})
rq := test.NewRequest(POST, "/", nil)
req := test.NewRequest(POST, "/", nil)
rec := test.NewResponseRecorder()
e.ServeHTTP(rq, rec)
e.ServeHTTP(req, rec)
assert.Equal(t, http.StatusMethodNotAllowed, rec.Status())
}

Expand All @@ -328,8 +328,8 @@ func testMethod(t *testing.T, method, path string, e *Echo) {
}

func request(method, path string, e *Echo) (int, string) {
rq := test.NewRequest(method, path, nil)
req := test.NewRequest(method, path, nil)
rec := test.NewResponseRecorder()
e.ServeHTTP(rq, rec)
e.ServeHTTP(req, rec)
return rec.Status(), rec.Body.String()
}
Loading

0 comments on commit be825e0

Please sign in to comment.