forked from mirror/gin
Refactores context.go
This commit is contained in:
parent
4103061a4a
commit
3285007fbb
13
context.go
13
context.go
|
@ -8,13 +8,14 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin/binding"
|
|
||||||
"github.com/gin-gonic/gin/render"
|
|
||||||
"github.com/julienschmidt/httprouter"
|
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin/binding"
|
||||||
|
"github.com/gin-gonic/gin/render"
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -187,14 +188,14 @@ func (c *Context) Get(key string) (interface{}, error) {
|
||||||
return value, nil
|
return value, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, errors.New("Key does not exist.")
|
return nil, errors.New("Key %s does not exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustGet returns the value for the given key or panics if the value doesn't exist.
|
// MustGet returns the value for the given key or panics if the value doesn't exist.
|
||||||
func (c *Context) MustGet(key string) interface{} {
|
func (c *Context) MustGet(key string) interface{} {
|
||||||
value, err := c.Get(key)
|
value, err := c.Get(key)
|
||||||
if err != nil || value == nil {
|
if err != nil {
|
||||||
log.Panicf("Key %s doesn't exist", value)
|
log.Panicf(err.Error())
|
||||||
}
|
}
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
|
@ -214,11 +214,11 @@ func TestHandlerFunc(t *testing.T) {
|
||||||
r.ServeHTTP(w, req)
|
r.ServeHTTP(w, req)
|
||||||
|
|
||||||
if w.Code != 404 {
|
if w.Code != 404 {
|
||||||
t.Errorf("Response code should be Not found, was: %s", w.Code)
|
t.Errorf("Response code should be Not found, was: %d", w.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
if stepsPassed != 2 {
|
if stepsPassed != 2 {
|
||||||
t.Errorf("Falied to switch context in handler function: %s", stepsPassed)
|
t.Errorf("Falied to switch context in handler function: %d", stepsPassed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -329,7 +329,7 @@ func TestBindingJSON(t *testing.T) {
|
||||||
r.ServeHTTP(w, req)
|
r.ServeHTTP(w, req)
|
||||||
|
|
||||||
if w.Code != 200 {
|
if w.Code != 200 {
|
||||||
t.Errorf("Response code should be Ok, was: %s", w.Code)
|
t.Errorf("Response code should be Ok, was: %d", w.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
if w.Body.String() != "{\"parsed\":\"bar\"}\n" {
|
if w.Body.String() != "{\"parsed\":\"bar\"}\n" {
|
||||||
|
@ -362,7 +362,7 @@ func TestBindingJSONEncoding(t *testing.T) {
|
||||||
r.ServeHTTP(w, req)
|
r.ServeHTTP(w, req)
|
||||||
|
|
||||||
if w.Code != 200 {
|
if w.Code != 200 {
|
||||||
t.Errorf("Response code should be Ok, was: %s", w.Code)
|
t.Errorf("Response code should be Ok, was: %d", w.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
if w.Body.String() != "{\"parsed\":\"嘉\"}\n" {
|
if w.Body.String() != "{\"parsed\":\"嘉\"}\n" {
|
||||||
|
@ -395,7 +395,7 @@ func TestBindingJSONNoContentType(t *testing.T) {
|
||||||
r.ServeHTTP(w, req)
|
r.ServeHTTP(w, req)
|
||||||
|
|
||||||
if w.Code != 400 {
|
if w.Code != 400 {
|
||||||
t.Errorf("Response code should be Bad request, was: %s", w.Code)
|
t.Errorf("Response code should be Bad request, was: %d", w.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
if w.Body.String() == "{\"parsed\":\"bar\"}\n" {
|
if w.Body.String() == "{\"parsed\":\"bar\"}\n" {
|
||||||
|
@ -430,7 +430,7 @@ func TestBindingJSONMalformed(t *testing.T) {
|
||||||
r.ServeHTTP(w, req)
|
r.ServeHTTP(w, req)
|
||||||
|
|
||||||
if w.Code != 400 {
|
if w.Code != 400 {
|
||||||
t.Errorf("Response code should be Bad request, was: %s", w.Code)
|
t.Errorf("Response code should be Bad request, was: %d", w.Code)
|
||||||
}
|
}
|
||||||
if w.Body.String() == "{\"parsed\":\"bar\"}\n" {
|
if w.Body.String() == "{\"parsed\":\"bar\"}\n" {
|
||||||
t.Errorf("Response should not be {\"parsed\":\"bar\"}, was: %s", w.Body.String())
|
t.Errorf("Response should not be {\"parsed\":\"bar\"}, was: %s", w.Body.String())
|
||||||
|
|
Loading…
Reference in New Issue