Improves unit tests
This commit is contained in:
		
							
								
								
									
										5
									
								
								auth.go
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								auth.go
									
									
									
									
									
								
							| @ -9,7 +9,6 @@ import ( | ||||
| 	"encoding/base64" | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"log" | ||||
| 	"sort" | ||||
| ) | ||||
|  | ||||
| @ -61,12 +60,12 @@ func BasicAuth(accounts Accounts) HandlerFunc { | ||||
|  | ||||
| func processAccounts(accounts Accounts) authPairs { | ||||
| 	if len(accounts) == 0 { | ||||
| 		log.Panic("Empty list of authorized credentials") | ||||
| 		panic("Empty list of authorized credentials") | ||||
| 	} | ||||
| 	pairs := make(authPairs, 0, len(accounts)) | ||||
| 	for user, password := range accounts { | ||||
| 		if len(user) == 0 { | ||||
| 			log.Panic("User can not be empty") | ||||
| 			panic("User can not be empty") | ||||
| 		} | ||||
| 		base := user + ":" + password | ||||
| 		value := "Basic " + base64.StdEncoding.EncodeToString([]byte(base)) | ||||
|  | ||||
| @ -6,7 +6,6 @@ package binding | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"log" | ||||
| 	"reflect" | ||||
| 	"strconv" | ||||
| ) | ||||
| @ -136,6 +135,6 @@ func setFloatField(val string, bitSize int, field reflect.Value) error { | ||||
| // https://github.com/codegangsta/martini-contrib/pull/34#issuecomment-29683659 | ||||
| func ensureNotPointer(obj interface{}) { | ||||
| 	if reflect.TypeOf(obj).Kind() == reflect.Ptr { | ||||
| 		log.Panic("Pointers are not accepted as binding models") | ||||
| 		panic("Pointers are not accepted as binding models") | ||||
| 	} | ||||
| } | ||||
|  | ||||
							
								
								
									
										19
									
								
								context.go
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								context.go
									
									
									
									
									
								
							| @ -6,7 +6,7 @@ package gin | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"log" | ||||
| 	"fmt" | ||||
| 	"math" | ||||
| 	"net/http" | ||||
| 	"strings" | ||||
| @ -81,6 +81,10 @@ func (c *Context) AbortWithStatus(code int) { | ||||
| 	c.Abort() | ||||
| } | ||||
|  | ||||
| func (c *Context) IsAborted() bool { | ||||
| 	return c.index == AbortIndex | ||||
| } | ||||
|  | ||||
| /************************************/ | ||||
| /********* ERROR MANAGEMENT *********/ | ||||
| /************************************/ | ||||
| @ -96,7 +100,7 @@ func (c *Context) Fail(code int, err error) { | ||||
| 	c.AbortWithStatus(code) | ||||
| } | ||||
|  | ||||
| func (c *Context) ErrorTyped(err error, typ uint32, meta interface{}) { | ||||
| func (c *Context) ErrorTyped(err error, typ int, meta interface{}) { | ||||
| 	c.Errors = append(c.Errors, errorMsg{ | ||||
| 		Err:  err.Error(), | ||||
| 		Type: typ, | ||||
| @ -146,9 +150,8 @@ func (c *Context) MustGet(key string) interface{} { | ||||
| 	if value, exists := c.Get(key); exists { | ||||
| 		return value | ||||
| 	} else { | ||||
| 		log.Panicf("Key %s does not exist", key) | ||||
| 		panic("Key " + key + " does not exist") | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| /************************************/ | ||||
| @ -163,7 +166,7 @@ func (c *Context) ClientIP() string { | ||||
| 	clientIP = c.Request.Header.Get("X-Forwarded-For") | ||||
| 	clientIP = strings.Split(clientIP, ",")[0] | ||||
| 	if len(clientIP) > 0 { | ||||
| 		return clientIP | ||||
| 		return strings.TrimSpace(clientIP) | ||||
| 	} | ||||
| 	return c.Request.RemoteAddr | ||||
| } | ||||
| @ -236,7 +239,7 @@ func (c *Context) Redirect(code int, location string) { | ||||
| 	if code >= 300 && code <= 308 { | ||||
| 		c.Render(code, render.Redirect, c.Request, location) | ||||
| 	} else { | ||||
| 		log.Panicf("Cannot redirect with status code %d", code) | ||||
| 		panic(fmt.Sprintf("Cannot redirect with status code %d", code)) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -275,7 +278,7 @@ func (c *Context) Negotiate(code int, config Negotiate) { | ||||
|  | ||||
| 	case binding.MIMEHTML: | ||||
| 		if len(config.HTMLPath) == 0 { | ||||
| 			log.Panic("negotiate config is wrong. html path is needed") | ||||
| 			panic("negotiate config is wrong. html path is needed") | ||||
| 		} | ||||
| 		data := chooseData(config.HTMLData, config.Data) | ||||
| 		c.HTML(code, config.HTMLPath, data) | ||||
| @ -291,7 +294,7 @@ func (c *Context) Negotiate(code int, config Negotiate) { | ||||
|  | ||||
| func (c *Context) NegotiateFormat(offered ...string) string { | ||||
| 	if len(offered) == 0 { | ||||
| 		log.Panic("you must provide at least one offer") | ||||
| 		panic("you must provide at least one offer") | ||||
| 	} | ||||
| 	if c.Accepted == nil { | ||||
| 		c.Accepted = parseAccept(c.Request.Header.Get("Accept")) | ||||
|  | ||||
							
								
								
									
										643
									
								
								context_test.go
									
									
									
									
									
								
							
							
						
						
									
										643
									
								
								context_test.go
									
									
									
									
									
								
							| @ -11,454 +11,311 @@ import ( | ||||
| 	"net/http" | ||||
| 	"net/http/httptest" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/gin-gonic/gin/binding" | ||||
| 	"github.com/julienschmidt/httprouter" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| // TestContextParamsGet tests that a parameter can be parsed from the URL. | ||||
| func TestContextParamsByName(t *testing.T) { | ||||
| 	req, _ := http.NewRequest("GET", "/test/alexandernyquist", nil) | ||||
| 	w := httptest.NewRecorder() | ||||
| 	name := "" | ||||
|  | ||||
| 	r := New() | ||||
| 	r.GET("/test/:name", func(c *Context) { | ||||
| 		name = c.Params.ByName("name") | ||||
| 	}) | ||||
|  | ||||
| 	r.ServeHTTP(w, req) | ||||
|  | ||||
| 	if name != "alexandernyquist" { | ||||
| 		t.Errorf("Url parameter was not correctly parsed. Should be alexandernyquist, was %s.", name) | ||||
| func createTestContext() (c *Context, w *httptest.ResponseRecorder, r *Engine) { | ||||
| 	w = httptest.NewRecorder() | ||||
| 	r = New() | ||||
| 	c = r.allocateContext() | ||||
| 	c.reset() | ||||
| 	c.writermem.reset(w) | ||||
| 	return | ||||
| } | ||||
|  | ||||
| func TestContextReset(t *testing.T) { | ||||
| 	router := New() | ||||
| 	c := router.allocateContext() | ||||
| 	assert.Equal(t, c.Engine, router) | ||||
|  | ||||
| 	c.index = 2 | ||||
| 	c.Writer = &responseWriter{ResponseWriter: httptest.NewRecorder()} | ||||
| 	c.Params = httprouter.Params{httprouter.Param{}} | ||||
| 	c.Error(errors.New("test"), nil) | ||||
| 	c.Set("foo", "bar") | ||||
| 	c.reset() | ||||
|  | ||||
| 	assert.False(t, c.IsAborted()) | ||||
| 	assert.Nil(t, c.Keys) | ||||
| 	assert.Nil(t, c.Accepted) | ||||
| 	assert.Len(t, c.Errors, 0) | ||||
| 	assert.Len(t, c.Params, 0) | ||||
| 	assert.Equal(t, c.index, -1) | ||||
| 	assert.Equal(t, c.Writer.(*responseWriter), &c.writermem) | ||||
| } | ||||
|  | ||||
| // TestContextSetGet tests that a parameter is set correctly on the | ||||
| // current context and can be retrieved using Get. | ||||
| func TestContextSetGet(t *testing.T) { | ||||
| 	req, _ := http.NewRequest("GET", "/test", nil) | ||||
| 	w := httptest.NewRecorder() | ||||
|  | ||||
| 	r := New() | ||||
| 	r.GET("/test", func(c *Context) { | ||||
| 		// Key should be lazily created | ||||
| 		if c.Keys != nil { | ||||
| 			t.Error("Keys should be nil") | ||||
| 		} | ||||
|  | ||||
| 		// Set | ||||
| 	c, _, _ := createTestContext() | ||||
| 	c.Set("foo", "bar") | ||||
|  | ||||
| 		v, ok := c.Get("foo") | ||||
| 		if !ok { | ||||
| 			t.Errorf("Error on exist key") | ||||
| 		} | ||||
| 		if v != "bar" { | ||||
| 			t.Errorf("Value should be bar, was %s", v) | ||||
| 		} | ||||
| 	}) | ||||
| 	value, err := c.Get("foo") | ||||
| 	assert.Equal(t, value, "bar") | ||||
| 	assert.True(t, err) | ||||
|  | ||||
| 	r.ServeHTTP(w, req) | ||||
| 	value, err = c.Get("foo2") | ||||
| 	assert.Nil(t, value) | ||||
| 	assert.False(t, err) | ||||
|  | ||||
| 	assert.Equal(t, c.MustGet("foo"), "bar") | ||||
| 	assert.Panics(t, func() { c.MustGet("no_exist") }) | ||||
| } | ||||
|  | ||||
| // TestContextJSON tests that the response is serialized as JSON | ||||
| // Tests that the response is serialized as JSON | ||||
| // and Content-Type is set to application/json | ||||
| func TestContextJSON(t *testing.T) { | ||||
| 	req, _ := http.NewRequest("GET", "/test", nil) | ||||
| 	w := httptest.NewRecorder() | ||||
| func TestContextRenderJSON(t *testing.T) { | ||||
| 	c, w, _ := createTestContext() | ||||
| 	c.JSON(201, H{"foo": "bar"}) | ||||
|  | ||||
| 	r := New() | ||||
| 	r.GET("/test", func(c *Context) { | ||||
| 		c.JSON(200, H{"foo": "bar"}) | ||||
| 	}) | ||||
|  | ||||
| 	r.ServeHTTP(w, req) | ||||
|  | ||||
| 	if w.Body.String() != "{\"foo\":\"bar\"}\n" { | ||||
| 		t.Errorf("Response should be {\"foo\":\"bar\"}, was: %s", w.Body.String()) | ||||
| 	assert.Equal(t, w.Code, 201) | ||||
| 	assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n") | ||||
| 	assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8") | ||||
| } | ||||
|  | ||||
| 	if w.HeaderMap.Get("Content-Type") != "application/json; charset=utf-8" { | ||||
| 		t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type")) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // TestContextHTML tests that the response executes the templates | ||||
| // Tests that the response executes the templates | ||||
| // and responds with Content-Type set to text/html | ||||
| func TestContextHTML(t *testing.T) { | ||||
| 	req, _ := http.NewRequest("GET", "/test", nil) | ||||
| 	w := httptest.NewRecorder() | ||||
| func TestContextRenderHTML(t *testing.T) { | ||||
| 	c, w, router := createTestContext() | ||||
| 	templ, _ := template.New("t").Parse(`Hello {{.name}}`) | ||||
| 	router.SetHTMLTemplate(templ) | ||||
|  | ||||
| 	r := New() | ||||
| 	templ, _ := template.New("t").Parse(`Hello {{.Name}}`) | ||||
| 	r.SetHTMLTemplate(templ) | ||||
| 	c.HTML(201, "t", H{"name": "alexandernyquist"}) | ||||
|  | ||||
| 	type TestData struct{ Name string } | ||||
|  | ||||
| 	r.GET("/test", func(c *Context) { | ||||
| 		c.HTML(200, "t", TestData{"alexandernyquist"}) | ||||
| 	}) | ||||
|  | ||||
| 	r.ServeHTTP(w, req) | ||||
|  | ||||
| 	if w.Body.String() != "Hello alexandernyquist" { | ||||
| 		t.Errorf("Response should be Hello alexandernyquist, was: %s", w.Body.String()) | ||||
| 	} | ||||
|  | ||||
| 	if w.HeaderMap.Get("Content-Type") != "text/html; charset=utf-8" { | ||||
| 		t.Errorf("Content-Type should be text/html, was %s", w.HeaderMap.Get("Content-Type")) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // TestContextString tests that the response is returned | ||||
| // with Content-Type set to text/plain | ||||
| func TestContextString(t *testing.T) { | ||||
| 	req, _ := http.NewRequest("GET", "/test", nil) | ||||
| 	w := httptest.NewRecorder() | ||||
|  | ||||
| 	r := New() | ||||
| 	r.GET("/test", func(c *Context) { | ||||
| 		c.String(200, "test") | ||||
| 	}) | ||||
|  | ||||
| 	r.ServeHTTP(w, req) | ||||
|  | ||||
| 	if w.Body.String() != "test" { | ||||
| 		t.Errorf("Response should be test, was: %s", w.Body.String()) | ||||
| 	} | ||||
|  | ||||
| 	if w.HeaderMap.Get("Content-Type") != "text/plain; charset=utf-8" { | ||||
| 		t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type")) | ||||
| 	} | ||||
| 	assert.Equal(t, w.Code, 201) | ||||
| 	assert.Equal(t, w.Body.String(), "Hello alexandernyquist") | ||||
| 	assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8") | ||||
| } | ||||
|  | ||||
| // TestContextXML tests that the response is serialized as XML | ||||
| // and Content-Type is set to application/xml | ||||
| func TestContextXML(t *testing.T) { | ||||
| 	req, _ := http.NewRequest("GET", "/test", nil) | ||||
| 	w := httptest.NewRecorder() | ||||
| func TestContextRenderXML(t *testing.T) { | ||||
| 	c, w, _ := createTestContext() | ||||
| 	c.XML(201, H{"foo": "bar"}) | ||||
|  | ||||
| 	r := New() | ||||
| 	r.GET("/test", func(c *Context) { | ||||
| 		c.XML(200, H{"foo": "bar"}) | ||||
| 	}) | ||||
|  | ||||
| 	r.ServeHTTP(w, req) | ||||
|  | ||||
| 	if w.Body.String() != "<map><foo>bar</foo></map>" { | ||||
| 		t.Errorf("Response should be <map><foo>bar</foo></map>, was: %s", w.Body.String()) | ||||
| 	assert.Equal(t, w.Code, 201) | ||||
| 	assert.Equal(t, w.Body.String(), "<map><foo>bar</foo></map>") | ||||
| 	assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/xml; charset=utf-8") | ||||
| } | ||||
|  | ||||
| 	if w.HeaderMap.Get("Content-Type") != "application/xml; charset=utf-8" { | ||||
| 		t.Errorf("Content-Type should be application/xml, was %s", w.HeaderMap.Get("Content-Type")) | ||||
| // TestContextString tests that the response is returned | ||||
| // with Content-Type set to text/plain | ||||
| func TestContextRenderString(t *testing.T) { | ||||
| 	c, w, _ := createTestContext() | ||||
| 	c.String(201, "test %s %d", "string", 2) | ||||
|  | ||||
| 	assert.Equal(t, w.Code, 201) | ||||
| 	assert.Equal(t, w.Body.String(), "test string 2") | ||||
| 	assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8") | ||||
| } | ||||
|  | ||||
| // TestContextString tests that the response is returned | ||||
| // with Content-Type set to text/html | ||||
| func TestContextRenderHTMLString(t *testing.T) { | ||||
| 	c, w, _ := createTestContext() | ||||
| 	c.HTMLString(201, "<html>%s %d</html>", "string", 3) | ||||
|  | ||||
| 	assert.Equal(t, w.Code, 201) | ||||
| 	assert.Equal(t, w.Body.String(), "<html>string 3</html>") | ||||
| 	assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8") | ||||
| } | ||||
|  | ||||
| // TestContextData tests that the response can be written from `bytesting` | ||||
| // with specified MIME type | ||||
| func TestContextData(t *testing.T) { | ||||
| 	req, _ := http.NewRequest("GET", "/test/csv", nil) | ||||
| 	w := httptest.NewRecorder() | ||||
| func TestContextRenderData(t *testing.T) { | ||||
| 	c, w, _ := createTestContext() | ||||
| 	c.Data(201, "text/csv", []byte(`foo,bar`)) | ||||
|  | ||||
| 	r := New() | ||||
| 	r.GET("/test/csv", func(c *Context) { | ||||
| 		c.Data(200, "text/csv", []byte(`foo,bar`)) | ||||
| 	}) | ||||
|  | ||||
| 	r.ServeHTTP(w, req) | ||||
|  | ||||
| 	if w.Body.String() != "foo,bar" { | ||||
| 		t.Errorf("Response should be foo&bar, was: %s", w.Body.String()) | ||||
| 	assert.Equal(t, w.Code, 201) | ||||
| 	assert.Equal(t, w.Body.String(), "foo,bar") | ||||
| 	assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/csv") | ||||
| } | ||||
|  | ||||
| 	if w.HeaderMap.Get("Content-Type") != "text/csv" { | ||||
| 		t.Errorf("Content-Type should be text/csv, was %s", w.HeaderMap.Get("Content-Type")) | ||||
| // TODO | ||||
| func TestContextRenderRedirectWithRelativePath(t *testing.T) { | ||||
| 	c, w, _ := createTestContext() | ||||
| 	c.Request, _ = http.NewRequest("POST", "http://example.com", nil) | ||||
| 	assert.Panics(t, func() { c.Redirect(299, "/new_path") }) | ||||
| 	assert.Panics(t, func() { c.Redirect(309, "/new_path") }) | ||||
|  | ||||
| 	c.Redirect(302, "/path") | ||||
| 	c.Writer.WriteHeaderNow() | ||||
| 	assert.Equal(t, w.Code, 302) | ||||
| 	assert.Equal(t, w.Header().Get("Location"), "/path") | ||||
| } | ||||
|  | ||||
| func TestContextRenderRedirectWithAbsolutePath(t *testing.T) { | ||||
| 	c, w, _ := createTestContext() | ||||
| 	c.Request, _ = http.NewRequest("POST", "http://example.com", nil) | ||||
| 	c.Redirect(302, "http://google.com") | ||||
| 	c.Writer.WriteHeaderNow() | ||||
|  | ||||
| 	assert.Equal(t, w.Code, 302) | ||||
| 	assert.Equal(t, w.Header().Get("Location"), "http://google.com") | ||||
| } | ||||
|  | ||||
| func TestContextNegotiationFormat(t *testing.T) { | ||||
| 	c, _, _ := createTestContext() | ||||
| 	c.Request, _ = http.NewRequest("POST", "", nil) | ||||
|  | ||||
| 	assert.Equal(t, c.NegotiateFormat(MIMEJSON, MIMEXML), MIMEJSON) | ||||
| 	assert.Equal(t, c.NegotiateFormat(MIMEHTML, MIMEJSON), MIMEHTML) | ||||
| } | ||||
|  | ||||
| func TestContextNegotiationFormatWithAccept(t *testing.T) { | ||||
| 	c, _, _ := createTestContext() | ||||
| 	c.Request, _ = http.NewRequest("POST", "", nil) | ||||
| 	c.Request.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") | ||||
|  | ||||
| 	assert.Equal(t, c.NegotiateFormat(MIMEJSON, MIMEXML), MIMEXML) | ||||
| 	assert.Equal(t, c.NegotiateFormat(MIMEXML, MIMEHTML), MIMEHTML) | ||||
| 	assert.Equal(t, c.NegotiateFormat(MIMEJSON), "") | ||||
| } | ||||
|  | ||||
| func TestContextNegotiationFormatCustum(t *testing.T) { | ||||
| 	c, _, _ := createTestContext() | ||||
| 	c.Request, _ = http.NewRequest("POST", "", nil) | ||||
| 	c.Request.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") | ||||
|  | ||||
| 	c.Accepted = nil | ||||
| 	c.SetAccepted(MIMEJSON, MIMEXML) | ||||
|  | ||||
| 	assert.Equal(t, c.NegotiateFormat(MIMEJSON, MIMEXML), MIMEJSON) | ||||
| 	assert.Equal(t, c.NegotiateFormat(MIMEXML, MIMEHTML), MIMEXML) | ||||
| 	assert.Equal(t, c.NegotiateFormat(MIMEJSON), MIMEJSON) | ||||
| } | ||||
|  | ||||
| // TestContextData tests that the response can be written from `bytesting` | ||||
| // with specified MIME type | ||||
| func TestContextAbortWithStatus(t *testing.T) { | ||||
| 	c, w, _ := createTestContext() | ||||
| 	c.index = 4 | ||||
| 	c.AbortWithStatus(401) | ||||
| 	c.Writer.WriteHeaderNow() | ||||
|  | ||||
| 	assert.Equal(t, c.index, AbortIndex) | ||||
| 	assert.Equal(t, c.Writer.Status(), 401) | ||||
| 	assert.Equal(t, w.Code, 401) | ||||
| 	assert.True(t, c.IsAborted()) | ||||
| } | ||||
|  | ||||
| func TestContextError(t *testing.T) { | ||||
| 	c, _, _ := createTestContext() | ||||
| 	c.Error(errors.New("first error"), "some data") | ||||
| 	assert.Equal(t, c.LastError().Error(), "first error") | ||||
| 	assert.Len(t, c.Errors, 1) | ||||
|  | ||||
| 	c.Error(errors.New("second error"), "some data 2") | ||||
| 	assert.Equal(t, c.LastError().Error(), "second error") | ||||
| 	assert.Len(t, c.Errors, 2) | ||||
|  | ||||
| 	assert.Equal(t, c.Errors[0].Err, "first error") | ||||
| 	assert.Equal(t, c.Errors[0].Meta, "some data") | ||||
| 	assert.Equal(t, c.Errors[0].Type, ErrorTypeExternal) | ||||
|  | ||||
| 	assert.Equal(t, c.Errors[1].Err, "second error") | ||||
| 	assert.Equal(t, c.Errors[1].Meta, "some data 2") | ||||
| 	assert.Equal(t, c.Errors[1].Type, ErrorTypeExternal) | ||||
| } | ||||
|  | ||||
| func TestContextTypedError(t *testing.T) { | ||||
| 	c, _, _ := createTestContext() | ||||
| 	c.ErrorTyped(errors.New("externo 0"), ErrorTypeExternal, nil) | ||||
| 	c.ErrorTyped(errors.New("externo 1"), ErrorTypeExternal, nil) | ||||
| 	c.ErrorTyped(errors.New("interno 0"), ErrorTypeInternal, nil) | ||||
| 	c.ErrorTyped(errors.New("externo 2"), ErrorTypeExternal, nil) | ||||
| 	c.ErrorTyped(errors.New("interno 1"), ErrorTypeInternal, nil) | ||||
| 	c.ErrorTyped(errors.New("interno 2"), ErrorTypeInternal, nil) | ||||
|  | ||||
| 	for _, err := range c.Errors.ByType(ErrorTypeExternal) { | ||||
| 		assert.Equal(t, err.Type, ErrorTypeExternal) | ||||
| 	} | ||||
|  | ||||
| 	for _, err := range c.Errors.ByType(ErrorTypeInternal) { | ||||
| 		assert.Equal(t, err.Type, ErrorTypeInternal) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestContextFile(t *testing.T) { | ||||
| 	req, _ := http.NewRequest("GET", "/test/file", nil) | ||||
| 	w := httptest.NewRecorder() | ||||
| func TestContextFail(t *testing.T) { | ||||
| 	c, w, _ := createTestContext() | ||||
| 	c.Fail(401, errors.New("bad input")) | ||||
| 	c.Writer.WriteHeaderNow() | ||||
|  | ||||
| 	r := New() | ||||
| 	r.GET("/test/file", func(c *Context) { | ||||
| 		c.File("./gin.go") | ||||
| 	}) | ||||
|  | ||||
| 	r.ServeHTTP(w, req) | ||||
|  | ||||
| 	bodyAsString := w.Body.String() | ||||
|  | ||||
| 	if len(bodyAsString) == 0 { | ||||
| 		t.Errorf("Got empty body instead of file data") | ||||
| 	assert.Equal(t, w.Code, 401) | ||||
| 	assert.Equal(t, c.LastError().Error(), "bad input") | ||||
| 	assert.Equal(t, c.index, AbortIndex) | ||||
| 	assert.True(t, c.IsAborted()) | ||||
| } | ||||
|  | ||||
| 	if w.HeaderMap.Get("Content-Type") != "text/plain; charset=utf-8" { | ||||
| 		t.Errorf("Content-Type should be text/plain; charset=utf-8, was %s", w.HeaderMap.Get("Content-Type")) | ||||
| 	} | ||||
| func TestContextClientIP(t *testing.T) { | ||||
| 	c, _, _ := createTestContext() | ||||
| 	c.Request, _ = http.NewRequest("POST", "", nil) | ||||
|  | ||||
| 	c.Request.Header.Set("X-Real-IP", "10.10.10.10") | ||||
| 	c.Request.Header.Set("X-Forwarded-For", "20.20.20.20 , 30.30.30.30") | ||||
| 	c.Request.RemoteAddr = "40.40.40.40" | ||||
|  | ||||
| 	assert.Equal(t, c.ClientIP(), "10.10.10.10") | ||||
| 	c.Request.Header.Del("X-Real-IP") | ||||
| 	assert.Equal(t, c.ClientIP(), "20.20.20.20") | ||||
| 	c.Request.Header.Del("X-Forwarded-For") | ||||
| 	assert.Equal(t, c.ClientIP(), "40.40.40.40") | ||||
| } | ||||
|  | ||||
| // TestHandlerFunc - ensure that custom middleware works properly | ||||
| func TestHandlerFunc(t *testing.T) { | ||||
| func TestContextContentType(t *testing.T) { | ||||
| 	c, _, _ := createTestContext() | ||||
| 	c.Request, _ = http.NewRequest("POST", "", nil) | ||||
| 	c.Request.Header.Set("Content-Type", "application/json; charset=utf-8") | ||||
|  | ||||
| 	req, _ := http.NewRequest("GET", "/", nil) | ||||
| 	w := httptest.NewRecorder() | ||||
|  | ||||
| 	r := New() | ||||
| 	var stepsPassed int = 0 | ||||
|  | ||||
| 	r.Use(func(context *Context) { | ||||
| 		stepsPassed += 1 | ||||
| 		context.Next() | ||||
| 		stepsPassed += 1 | ||||
| 	}) | ||||
|  | ||||
| 	r.ServeHTTP(w, req) | ||||
|  | ||||
| 	if w.Code != 404 { | ||||
| 		t.Errorf("Response code should be Not found, was: %d", w.Code) | ||||
| 	assert.Equal(t, c.ContentType(), "application/json") | ||||
| } | ||||
|  | ||||
| 	if stepsPassed != 2 { | ||||
| 		t.Errorf("Falied to switch context in handler function: %d", stepsPassed) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // TestBadAbortHandlersChain - ensure that Abort after switch context will not interrupt pending handlers | ||||
| func TestBadAbortHandlersChain(t *testing.T) { | ||||
| 	// SETUP | ||||
| 	var stepsPassed int = 0 | ||||
| 	r := New() | ||||
| 	r.Use(func(c *Context) { | ||||
| 		stepsPassed += 1 | ||||
| 		c.Next() | ||||
| 		stepsPassed += 1 | ||||
| 		// after check and abort | ||||
| 		c.AbortWithStatus(409) | ||||
| 	}) | ||||
| 	r.Use(func(c *Context) { | ||||
| 		stepsPassed += 1 | ||||
| 		c.Next() | ||||
| 		stepsPassed += 1 | ||||
| 		c.AbortWithStatus(403) | ||||
| 	}) | ||||
|  | ||||
| 	// RUN | ||||
| 	w := PerformRequest(r, "GET", "/") | ||||
|  | ||||
| 	// TEST | ||||
| 	if w.Code != 409 { | ||||
| 		t.Errorf("Response code should be Forbiden, was: %d", w.Code) | ||||
| 	} | ||||
| 	if stepsPassed != 4 { | ||||
| 		t.Errorf("Falied to switch context in handler function: %d", stepsPassed) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // TestAbortHandlersChain - ensure that Abort interrupt used middlewares in fifo order | ||||
| func TestAbortHandlersChain(t *testing.T) { | ||||
| 	// SETUP | ||||
| 	var stepsPassed int = 0 | ||||
| 	r := New() | ||||
| 	r.Use(func(context *Context) { | ||||
| 		stepsPassed += 1 | ||||
| 		context.AbortWithStatus(409) | ||||
| 	}) | ||||
| 	r.Use(func(context *Context) { | ||||
| 		stepsPassed += 1 | ||||
| 		context.Next() | ||||
| 		stepsPassed += 1 | ||||
| 	}) | ||||
|  | ||||
| 	// RUN | ||||
| 	w := PerformRequest(r, "GET", "/") | ||||
|  | ||||
| 	// TEST | ||||
| 	if w.Code != 409 { | ||||
| 		t.Errorf("Response code should be Conflict, was: %d", w.Code) | ||||
| 	} | ||||
| 	if stepsPassed != 1 { | ||||
| 		t.Errorf("Falied to switch context in handler function: %d", stepsPassed) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // TestFailHandlersChain - ensure that Fail interrupt used middlewares in fifo order as | ||||
| // as well as Abort | ||||
| func TestFailHandlersChain(t *testing.T) { | ||||
| 	// SETUP | ||||
| 	var stepsPassed int = 0 | ||||
| 	r := New() | ||||
| 	r.Use(func(context *Context) { | ||||
| 		stepsPassed += 1 | ||||
| 		context.Fail(500, errors.New("foo")) | ||||
| 	}) | ||||
| 	r.Use(func(context *Context) { | ||||
| 		stepsPassed += 1 | ||||
| 		context.Next() | ||||
| 		stepsPassed += 1 | ||||
| 	}) | ||||
|  | ||||
| 	// RUN | ||||
| 	w := PerformRequest(r, "GET", "/") | ||||
|  | ||||
| 	// TEST | ||||
| 	if w.Code != 500 { | ||||
| 		t.Errorf("Response code should be Server error, was: %d", w.Code) | ||||
| 	} | ||||
| 	if stepsPassed != 1 { | ||||
| 		t.Errorf("Falied to switch context in handler function: %d", stepsPassed) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestBindingJSON(t *testing.T) { | ||||
|  | ||||
| 	body := bytes.NewBuffer([]byte("{\"foo\":\"bar\"}")) | ||||
|  | ||||
| 	r := New() | ||||
| 	r.POST("/binding/json", func(c *Context) { | ||||
| 		var body struct { | ||||
| func TestContextAutoBind(t *testing.T) { | ||||
| 	c, w, _ := createTestContext() | ||||
| 	c.Request, _ = http.NewRequest("POST", "http://example.com", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}")) | ||||
| 	c.Request.Header.Add("Content-Type", MIMEJSON) | ||||
| 	var obj struct { | ||||
| 		Foo string `json:"foo"` | ||||
| 		Bar string `json:"bar"` | ||||
| 	} | ||||
| 		if c.Bind(&body) { | ||||
| 			c.JSON(200, H{"parsed": body.Foo}) | ||||
| 		} | ||||
| 	}) | ||||
|  | ||||
| 	req, _ := http.NewRequest("POST", "/binding/json", body) | ||||
| 	req.Header.Set("Content-Type", "application/json") | ||||
| 	w := httptest.NewRecorder() | ||||
|  | ||||
| 	r.ServeHTTP(w, req) | ||||
|  | ||||
| 	if w.Code != 200 { | ||||
| 		t.Errorf("Response code should be Ok, was: %d", w.Code) | ||||
| 	assert.True(t, c.Bind(&obj)) | ||||
| 	assert.Equal(t, obj.Bar, "foo") | ||||
| 	assert.Equal(t, obj.Foo, "bar") | ||||
| 	assert.Equal(t, w.Body.Len(), 0) | ||||
| } | ||||
|  | ||||
| 	if w.Body.String() != "{\"parsed\":\"bar\"}\n" { | ||||
| 		t.Errorf("Response should be {\"parsed\":\"bar\"}, was: %s", w.Body.String()) | ||||
| 	} | ||||
|  | ||||
| 	if w.HeaderMap.Get("Content-Type") != "application/json; charset=utf-8" { | ||||
| 		t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type")) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestBindingJSONEncoding(t *testing.T) { | ||||
|  | ||||
| 	body := bytes.NewBuffer([]byte("{\"foo\":\"嘉\"}")) | ||||
|  | ||||
| 	r := New() | ||||
| 	r.POST("/binding/json", func(c *Context) { | ||||
| 		var body struct { | ||||
| func TestContextBadAutoBind(t *testing.T) { | ||||
| 	c, w, _ := createTestContext() | ||||
| 	c.Request, _ = http.NewRequest("POST", "http://example.com", bytes.NewBufferString("\"foo\":\"bar\", \"bar\":\"foo\"}")) | ||||
| 	c.Request.Header.Add("Content-Type", MIMEJSON) | ||||
| 	var obj struct { | ||||
| 		Foo string `json:"foo"` | ||||
| 		} | ||||
| 		if c.Bind(&body) { | ||||
| 			c.JSON(200, H{"parsed": body.Foo}) | ||||
| 		} | ||||
| 	}) | ||||
|  | ||||
| 	req, _ := http.NewRequest("POST", "/binding/json", body) | ||||
| 	req.Header.Set("Content-Type", "application/json; charset=utf-8") | ||||
| 	w := httptest.NewRecorder() | ||||
|  | ||||
| 	r.ServeHTTP(w, req) | ||||
|  | ||||
| 	if w.Code != 200 { | ||||
| 		t.Errorf("Response code should be Ok, was: %d", w.Code) | ||||
| 		Bar string `json:"bar"` | ||||
| 	} | ||||
|  | ||||
| 	if w.Body.String() != "{\"parsed\":\"嘉\"}\n" { | ||||
| 		t.Errorf("Response should be {\"parsed\":\"嘉\"}, was: %s", w.Body.String()) | ||||
| 	assert.False(t, c.IsAborted()) | ||||
| 	assert.False(t, c.Bind(&obj)) | ||||
| 	c.Writer.WriteHeaderNow() | ||||
|  | ||||
| 	assert.Empty(t, obj.Bar) | ||||
| 	assert.Empty(t, obj.Foo) | ||||
| 	assert.Equal(t, w.Code, 400) | ||||
| 	assert.True(t, c.IsAborted()) | ||||
| } | ||||
|  | ||||
| 	if w.HeaderMap.Get("Content-Type") != "application/json; charset=utf-8" { | ||||
| 		t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type")) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestBindingJSONMalformed(t *testing.T) { | ||||
|  | ||||
| 	body := bytes.NewBuffer([]byte("\"foo\":\"bar\"\n")) | ||||
|  | ||||
| 	r := New() | ||||
| 	r.POST("/binding/json", func(c *Context) { | ||||
| 		var body struct { | ||||
| func TestContextBindWith(t *testing.T) { | ||||
| 	c, w, _ := createTestContext() | ||||
| 	c.Request, _ = http.NewRequest("POST", "http://example.com", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}")) | ||||
| 	c.Request.Header.Add("Content-Type", MIMEXML) | ||||
| 	var obj struct { | ||||
| 		Foo string `json:"foo"` | ||||
| 		Bar string `json:"bar"` | ||||
| 	} | ||||
| 		if c.Bind(&body) { | ||||
| 			c.JSON(200, H{"parsed": body.Foo}) | ||||
| 		} | ||||
|  | ||||
| 	}) | ||||
|  | ||||
| 	req, _ := http.NewRequest("POST", "/binding/json", body) | ||||
| 	req.Header.Set("Content-Type", "application/json") | ||||
|  | ||||
| 	w := httptest.NewRecorder() | ||||
|  | ||||
| 	r.ServeHTTP(w, req) | ||||
|  | ||||
| 	if w.Code != 400 { | ||||
| 		t.Errorf("Response code should be Bad request, was: %d", w.Code) | ||||
| 	} | ||||
| 	if w.Body.String() == "{\"parsed\":\"bar\"}\n" { | ||||
| 		t.Errorf("Response should not be {\"parsed\":\"bar\"}, was: %s", w.Body.String()) | ||||
| 	} | ||||
|  | ||||
| 	if w.HeaderMap.Get("Content-Type") == "application/json" { | ||||
| 		t.Errorf("Content-Type should not be application/json, was %s", w.HeaderMap.Get("Content-Type")) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestBindingForm(t *testing.T) { | ||||
|  | ||||
| 	body := bytes.NewBuffer([]byte("foo=bar&num=123&unum=1234567890")) | ||||
|  | ||||
| 	r := New() | ||||
| 	r.POST("/binding/form", func(c *Context) { | ||||
| 		var body struct { | ||||
| 			Foo  string `form:"foo"` | ||||
| 			Num  int    `form:"num"` | ||||
| 			Unum uint   `form:"unum"` | ||||
| 		} | ||||
| 		if c.Bind(&body) { | ||||
| 			c.JSON(200, H{"foo": body.Foo, "num": body.Num, "unum": body.Unum}) | ||||
| 		} | ||||
| 	}) | ||||
|  | ||||
| 	req, _ := http.NewRequest("POST", "/binding/form", body) | ||||
| 	req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||||
| 	w := httptest.NewRecorder() | ||||
|  | ||||
| 	r.ServeHTTP(w, req) | ||||
|  | ||||
| 	if w.Code != 200 { | ||||
| 		t.Errorf("Response code should be Ok, was: %d", w.Code) | ||||
| 	} | ||||
|  | ||||
| 	expected := "{\"foo\":\"bar\",\"num\":123,\"unum\":1234567890}\n" | ||||
| 	if w.Body.String() != expected { | ||||
| 		t.Errorf("Response should be %s, was %s", expected, w.Body.String()) | ||||
| 	} | ||||
|  | ||||
| 	if w.HeaderMap.Get("Content-Type") != "application/json; charset=utf-8" { | ||||
| 		t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type")) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestClientIP(t *testing.T) { | ||||
| 	r := New() | ||||
|  | ||||
| 	var clientIP string = "" | ||||
| 	r.GET("/", func(c *Context) { | ||||
| 		clientIP = c.ClientIP() | ||||
| 	}) | ||||
|  | ||||
| 	body := bytes.NewBuffer([]byte("")) | ||||
| 	req, _ := http.NewRequest("GET", "/", body) | ||||
| 	req.RemoteAddr = "clientip:1234" | ||||
| 	w := httptest.NewRecorder() | ||||
| 	r.ServeHTTP(w, req) | ||||
|  | ||||
| 	if clientIP != "clientip:1234" { | ||||
| 		t.Errorf("ClientIP should not be %s, but clientip:1234", clientIP) | ||||
| 	} | ||||
| 	assert.True(t, c.BindWith(&obj, binding.JSON)) | ||||
| 	assert.Equal(t, obj.Bar, "foo") | ||||
| 	assert.Equal(t, obj.Foo, "bar") | ||||
| 	assert.Equal(t, w.Body.Len(), 0) | ||||
| } | ||||
|  | ||||
							
								
								
									
										9
									
								
								debug.go
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								debug.go
									
									
									
									
									
								
							| @ -4,7 +4,12 @@ | ||||
|  | ||||
| package gin | ||||
|  | ||||
| import "log" | ||||
| import ( | ||||
| 	"log" | ||||
| 	"os" | ||||
| ) | ||||
|  | ||||
| var debugLogger = log.New(os.Stdout, "[GIN-debug] ", 0) | ||||
|  | ||||
| func IsDebugging() bool { | ||||
| 	return ginMode == debugCode | ||||
| @ -20,6 +25,6 @@ func debugRoute(httpMethod, absolutePath string, handlers []HandlerFunc) { | ||||
|  | ||||
| func debugPrint(format string, values ...interface{}) { | ||||
| 	if IsDebugging() { | ||||
| 		log.Printf("[GIN-debug] "+format, values...) | ||||
| 		debugLogger.Printf(format, values...) | ||||
| 	} | ||||
| } | ||||
|  | ||||
							
								
								
									
										38
									
								
								debug_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								debug_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,38 @@ | ||||
| // Copyright 2014 Manu Martinez-Almeida.  All rights reserved. | ||||
| // Use of this source code is governed by a MIT style | ||||
| // license that can be found in the LICENSE file. | ||||
|  | ||||
| package gin | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func TestIsDebugging(t *testing.T) { | ||||
| 	SetMode(DebugMode) | ||||
| 	assert.True(t, IsDebugging()) | ||||
| 	SetMode(ReleaseMode) | ||||
| 	assert.False(t, IsDebugging()) | ||||
| 	SetMode(TestMode) | ||||
| 	assert.False(t, IsDebugging()) | ||||
| } | ||||
|  | ||||
| // TODO | ||||
| // func TestDebugPrint(t *testing.T) { | ||||
| // 	buffer := bytes.NewBufferString("") | ||||
| // 	debugLogger. | ||||
| // 	log.SetOutput(buffer) | ||||
|  | ||||
| // 	SetMode(ReleaseMode) | ||||
| // 	debugPrint("This is a example") | ||||
| // 	assert.Equal(t, buffer.Len(), 0) | ||||
|  | ||||
| // 	SetMode(DebugMode) | ||||
| // 	debugPrint("This is %s", "a example") | ||||
| // 	assert.Equal(t, buffer.String(), "[GIN-debug] This is a example") | ||||
|  | ||||
| // 	SetMode(TestMode) | ||||
| // 	log.SetOutput(os.Stdout) | ||||
| // } | ||||
| @ -18,13 +18,13 @@ const ( | ||||
| // Used internally to collect errors that occurred during an http request. | ||||
| type errorMsg struct { | ||||
| 	Err  string      `json:"error"` | ||||
| 	Type uint32      `json:"-"` | ||||
| 	Type int         `json:"-"` | ||||
| 	Meta interface{} `json:"meta"` | ||||
| } | ||||
|  | ||||
| type errorMsgs []errorMsg | ||||
|  | ||||
| func (a errorMsgs) ByType(typ uint32) errorMsgs { | ||||
| func (a errorMsgs) ByType(typ int) errorMsgs { | ||||
| 	if len(a) == 0 { | ||||
| 		return a | ||||
| 	} | ||||
|  | ||||
| @ -1,11 +1,26 @@ | ||||
| package main | ||||
|  | ||||
| import ( | ||||
| 	"net/http" | ||||
|  | ||||
| 	"github.com/flosch/pongo2" | ||||
| 	"github.com/gin-gonic/gin" | ||||
| 	"net/http" | ||||
| 	"github.com/gin-gonic/gin/render" | ||||
| ) | ||||
|  | ||||
| func main() { | ||||
| 	router := gin.Default() | ||||
| 	router.HTMLRender = newPongoRender() | ||||
|  | ||||
| 	router.GET("/index", func(c *gin.Context) { | ||||
| 		c.HTML(200, "index.html", gin.H{ | ||||
| 			"title": "Gin meets pongo2 !", | ||||
| 			"name":  c.Input.Get("name"), | ||||
| 		}) | ||||
| 	}) | ||||
| 	router.Run(":8080") | ||||
| } | ||||
|  | ||||
| type pongoRender struct { | ||||
| 	cache map[string]*pongo2.Template | ||||
| } | ||||
| @ -14,13 +29,6 @@ func newPongoRender() *pongoRender { | ||||
| 	return &pongoRender{map[string]*pongo2.Template{}} | ||||
| } | ||||
|  | ||||
| func writeHeader(w http.ResponseWriter, code int, contentType string) { | ||||
| 	if code >= 0 { | ||||
| 		w.Header().Set("Content-Type", contentType) | ||||
| 		w.WriteHeader(code) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (p *pongoRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { | ||||
| 	file := data[0].(string) | ||||
| 	ctx := data[1].(pongo2.Context) | ||||
| @ -36,23 +44,6 @@ func (p *pongoRender) Render(w http.ResponseWriter, code int, data ...interface{ | ||||
| 		p.cache[file] = tmpl | ||||
| 		t = tmpl | ||||
| 	} | ||||
| 	writeHeader(w, code, "text/html") | ||||
| 	render.WriteHeader(w, code, "text/html") | ||||
| 	return t.ExecuteWriter(ctx, w) | ||||
| } | ||||
|  | ||||
| func main() { | ||||
| 	r := gin.Default() | ||||
| 	r.HTMLRender = newPongoRender() | ||||
|  | ||||
| 	r.GET("/index", func(c *gin.Context) { | ||||
| 		name := c.Request.FormValue("name") | ||||
| 		ctx := pongo2.Context{ | ||||
| 			"title": "Gin meets pongo2 !", | ||||
| 			"name":  name, | ||||
| 		} | ||||
| 		c.HTML(200, "index.html", ctx) | ||||
| 	}) | ||||
|  | ||||
| 	// Listen and server on 0.0.0.0:8080 | ||||
| 	r.Run(":8080") | ||||
| } | ||||
|  | ||||
							
								
								
									
										275
									
								
								gin_test.go
									
									
									
									
									
								
							
							
						
						
									
										275
									
								
								gin_test.go
									
									
									
									
									
								
							| @ -5,202 +5,137 @@ | ||||
| package gin | ||||
|  | ||||
| import ( | ||||
| 	"io/ioutil" | ||||
| 	"net/http" | ||||
| 	"net/http/httptest" | ||||
| 	"os" | ||||
| 	"path" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func init() { | ||||
| 	SetMode(TestMode) | ||||
| } | ||||
|  | ||||
| func PerformRequest(r http.Handler, method, path string) *httptest.ResponseRecorder { | ||||
| 	req, _ := http.NewRequest(method, path, nil) | ||||
| 	w := httptest.NewRecorder() | ||||
| 	r.ServeHTTP(w, req) | ||||
| 	return w | ||||
| func TestCreateEngine(t *testing.T) { | ||||
| 	router := New() | ||||
| 	assert.Equal(t, "/", router.absolutePath) | ||||
| 	assert.Equal(t, router.engine, router) | ||||
| 	assert.Empty(t, router.Handlers) | ||||
|  | ||||
| 	// TODO | ||||
| 	// assert.Equal(t, router.router.NotFound, router.handle404) | ||||
| 	// assert.Equal(t, router.router.MethodNotAllowed, router.handle405) | ||||
| } | ||||
|  | ||||
| // TestSingleRouteOK tests that POST route is correctly invoked. | ||||
| func testRouteOK(method string, t *testing.T) { | ||||
| 	// SETUP | ||||
| 	passed := false | ||||
| 	r := New() | ||||
| 	r.Handle(method, "/test", []HandlerFunc{func(c *Context) { | ||||
| 		passed = true | ||||
| 	}}) | ||||
|  | ||||
| 	// RUN | ||||
| 	w := PerformRequest(r, method, "/test") | ||||
|  | ||||
| 	// TEST | ||||
| 	if passed == false { | ||||
| 		t.Errorf(method + " route handler was not invoked.") | ||||
| 	} | ||||
| 	if w.Code != http.StatusOK { | ||||
| 		t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code) | ||||
| 	} | ||||
| } | ||||
| func TestRouterGroupRouteOK(t *testing.T) { | ||||
| 	testRouteOK("POST", t) | ||||
| 	testRouteOK("DELETE", t) | ||||
| 	testRouteOK("PATCH", t) | ||||
| 	testRouteOK("PUT", t) | ||||
| 	testRouteOK("OPTIONS", t) | ||||
| 	testRouteOK("HEAD", t) | ||||
| func TestCreateDefaultRouter(t *testing.T) { | ||||
| 	router := Default() | ||||
| 	assert.Len(t, router.Handlers, 2) | ||||
| } | ||||
|  | ||||
| // TestSingleRouteOK tests that POST route is correctly invoked. | ||||
| func testRouteNotOK(method string, t *testing.T) { | ||||
| 	// SETUP | ||||
| 	passed := false | ||||
| 	r := New() | ||||
| 	r.Handle(method, "/test_2", []HandlerFunc{func(c *Context) { | ||||
| 		passed = true | ||||
| 	}}) | ||||
| func TestNoRouteWithoutGlobalHandlers(t *testing.T) { | ||||
| 	middleware0 := func(c *Context) {} | ||||
| 	middleware1 := func(c *Context) {} | ||||
|  | ||||
| 	// RUN | ||||
| 	w := PerformRequest(r, method, "/test") | ||||
| 	router := New() | ||||
|  | ||||
| 	// TEST | ||||
| 	if passed == true { | ||||
| 		t.Errorf(method + " route handler was invoked, when it should not") | ||||
| 	} | ||||
| 	if w.Code != http.StatusNotFound { | ||||
| 		// If this fails, it's because httprouter needs to be updated to at least f78f58a0db | ||||
| 		t.Errorf("Status code should be %v, was %d. Location: %s", http.StatusNotFound, w.Code, w.HeaderMap.Get("Location")) | ||||
| 	} | ||||
| 	router.NoRoute(middleware0) | ||||
| 	assert.Nil(t, router.Handlers) | ||||
| 	assert.Len(t, router.noRoute, 1) | ||||
| 	assert.Len(t, router.allNoRoute, 1) | ||||
| 	assert.Equal(t, router.noRoute[0], middleware0) | ||||
| 	assert.Equal(t, router.allNoRoute[0], middleware0) | ||||
|  | ||||
| 	router.NoRoute(middleware1, middleware0) | ||||
| 	assert.Len(t, router.noRoute, 2) | ||||
| 	assert.Len(t, router.allNoRoute, 2) | ||||
| 	assert.Equal(t, router.noRoute[0], middleware1) | ||||
| 	assert.Equal(t, router.allNoRoute[0], middleware1) | ||||
| 	assert.Equal(t, router.noRoute[1], middleware0) | ||||
| 	assert.Equal(t, router.allNoRoute[1], middleware0) | ||||
| } | ||||
|  | ||||
| // TestSingleRouteOK tests that POST route is correctly invoked. | ||||
| func TestRouteNotOK(t *testing.T) { | ||||
| 	testRouteNotOK("POST", t) | ||||
| 	testRouteNotOK("DELETE", t) | ||||
| 	testRouteNotOK("PATCH", t) | ||||
| 	testRouteNotOK("PUT", t) | ||||
| 	testRouteNotOK("OPTIONS", t) | ||||
| 	testRouteNotOK("HEAD", t) | ||||
| func TestNoRouteWithGlobalHandlers(t *testing.T) { | ||||
| 	middleware0 := func(c *Context) {} | ||||
| 	middleware1 := func(c *Context) {} | ||||
| 	middleware2 := func(c *Context) {} | ||||
|  | ||||
| 	router := New() | ||||
| 	router.Use(middleware2) | ||||
|  | ||||
| 	router.NoRoute(middleware0) | ||||
| 	assert.Len(t, router.allNoRoute, 2) | ||||
| 	assert.Len(t, router.Handlers, 1) | ||||
| 	assert.Len(t, router.noRoute, 1) | ||||
|  | ||||
| 	assert.Equal(t, router.Handlers[0], middleware2) | ||||
| 	assert.Equal(t, router.noRoute[0], middleware0) | ||||
| 	assert.Equal(t, router.allNoRoute[0], middleware2) | ||||
| 	assert.Equal(t, router.allNoRoute[1], middleware0) | ||||
|  | ||||
| 	router.Use(middleware1) | ||||
| 	assert.Len(t, router.allNoRoute, 3) | ||||
| 	assert.Len(t, router.Handlers, 2) | ||||
| 	assert.Len(t, router.noRoute, 1) | ||||
|  | ||||
| 	assert.Equal(t, router.Handlers[0], middleware2) | ||||
| 	assert.Equal(t, router.Handlers[1], middleware1) | ||||
| 	assert.Equal(t, router.noRoute[0], middleware0) | ||||
| 	assert.Equal(t, router.allNoRoute[0], middleware2) | ||||
| 	assert.Equal(t, router.allNoRoute[1], middleware1) | ||||
| 	assert.Equal(t, router.allNoRoute[2], middleware0) | ||||
| } | ||||
|  | ||||
| // TestSingleRouteOK tests that POST route is correctly invoked. | ||||
| func testRouteNotOK2(method string, t *testing.T) { | ||||
| 	// SETUP | ||||
| 	passed := false | ||||
| 	r := New() | ||||
| 	var methodRoute string | ||||
| 	if method == "POST" { | ||||
| 		methodRoute = "GET" | ||||
| 	} else { | ||||
| 		methodRoute = "POST" | ||||
| 	} | ||||
| 	r.Handle(methodRoute, "/test", []HandlerFunc{func(c *Context) { | ||||
| 		passed = true | ||||
| 	}}) | ||||
| func TestNoMethodWithoutGlobalHandlers(t *testing.T) { | ||||
| 	middleware0 := func(c *Context) {} | ||||
| 	middleware1 := func(c *Context) {} | ||||
|  | ||||
| 	// RUN | ||||
| 	w := PerformRequest(r, method, "/test") | ||||
| 	router := New() | ||||
|  | ||||
| 	// TEST | ||||
| 	if passed == true { | ||||
| 		t.Errorf(method + " route handler was invoked, when it should not") | ||||
| 	} | ||||
| 	if w.Code != http.StatusMethodNotAllowed { | ||||
| 		t.Errorf("Status code should be %v, was %d. Location: %s", http.StatusMethodNotAllowed, w.Code, w.HeaderMap.Get("Location")) | ||||
| 	} | ||||
| 	router.NoMethod(middleware0) | ||||
| 	assert.Empty(t, router.Handlers) | ||||
| 	assert.Len(t, router.noMethod, 1) | ||||
| 	assert.Len(t, router.allNoMethod, 1) | ||||
| 	assert.Equal(t, router.noMethod[0], middleware0) | ||||
| 	assert.Equal(t, router.allNoMethod[0], middleware0) | ||||
|  | ||||
| 	router.NoMethod(middleware1, middleware0) | ||||
| 	assert.Len(t, router.noMethod, 2) | ||||
| 	assert.Len(t, router.allNoMethod, 2) | ||||
| 	assert.Equal(t, router.noMethod[0], middleware1) | ||||
| 	assert.Equal(t, router.allNoMethod[0], middleware1) | ||||
| 	assert.Equal(t, router.noMethod[1], middleware0) | ||||
| 	assert.Equal(t, router.allNoMethod[1], middleware0) | ||||
| } | ||||
|  | ||||
| // TestSingleRouteOK tests that POST route is correctly invoked. | ||||
| func TestRouteNotOK2(t *testing.T) { | ||||
| 	testRouteNotOK2("POST", t) | ||||
| 	testRouteNotOK2("DELETE", t) | ||||
| 	testRouteNotOK2("PATCH", t) | ||||
| 	testRouteNotOK2("PUT", t) | ||||
| 	testRouteNotOK2("OPTIONS", t) | ||||
| 	testRouteNotOK2("HEAD", t) | ||||
| func TestRebuild404Handlers(t *testing.T) { | ||||
|  | ||||
| } | ||||
|  | ||||
| // TestHandleStaticFile - ensure the static file handles properly | ||||
| func TestHandleStaticFile(t *testing.T) { | ||||
| 	// SETUP file | ||||
| 	testRoot, _ := os.Getwd() | ||||
| 	f, err := ioutil.TempFile(testRoot, "") | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 	defer os.Remove(f.Name()) | ||||
| 	filePath := path.Join("/", path.Base(f.Name())) | ||||
| 	f.WriteString("Gin Web Framework") | ||||
| 	f.Close() | ||||
| func TestNoMethodWithGlobalHandlers(t *testing.T) { | ||||
| 	middleware0 := func(c *Context) {} | ||||
| 	middleware1 := func(c *Context) {} | ||||
| 	middleware2 := func(c *Context) {} | ||||
|  | ||||
| 	// SETUP gin | ||||
| 	r := New() | ||||
| 	r.Static("./", testRoot) | ||||
| 	router := New() | ||||
| 	router.Use(middleware2) | ||||
|  | ||||
| 	// RUN | ||||
| 	w := PerformRequest(r, "GET", filePath) | ||||
| 	router.NoMethod(middleware0) | ||||
| 	assert.Len(t, router.allNoMethod, 2) | ||||
| 	assert.Len(t, router.Handlers, 1) | ||||
| 	assert.Len(t, router.noMethod, 1) | ||||
|  | ||||
| 	// TEST | ||||
| 	if w.Code != 200 { | ||||
| 		t.Errorf("Response code should be 200, was: %d", w.Code) | ||||
| 	} | ||||
| 	if w.Body.String() != "Gin Web Framework" { | ||||
| 		t.Errorf("Response should be test, was: %s", w.Body.String()) | ||||
| 	} | ||||
| 	if w.HeaderMap.Get("Content-Type") != "text/plain; charset=utf-8" { | ||||
| 		t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type")) | ||||
| 	} | ||||
| } | ||||
| 	assert.Equal(t, router.Handlers[0], middleware2) | ||||
| 	assert.Equal(t, router.noMethod[0], middleware0) | ||||
| 	assert.Equal(t, router.allNoMethod[0], middleware2) | ||||
| 	assert.Equal(t, router.allNoMethod[1], middleware0) | ||||
|  | ||||
| // TestHandleStaticDir - ensure the root/sub dir handles properly | ||||
| func TestHandleStaticDir(t *testing.T) { | ||||
| 	// SETUP | ||||
| 	r := New() | ||||
| 	r.Static("/", "./") | ||||
| 	router.Use(middleware1) | ||||
| 	assert.Len(t, router.allNoMethod, 3) | ||||
| 	assert.Len(t, router.Handlers, 2) | ||||
| 	assert.Len(t, router.noMethod, 1) | ||||
|  | ||||
| 	// RUN | ||||
| 	w := PerformRequest(r, "GET", "/") | ||||
|  | ||||
| 	// TEST | ||||
| 	bodyAsString := w.Body.String() | ||||
| 	if w.Code != 200 { | ||||
| 		t.Errorf("Response code should be 200, was: %d", w.Code) | ||||
| 	} | ||||
| 	if len(bodyAsString) == 0 { | ||||
| 		t.Errorf("Got empty body instead of file tree") | ||||
| 	} | ||||
| 	if !strings.Contains(bodyAsString, "gin.go") { | ||||
| 		t.Errorf("Can't find:`gin.go` in file tree: %s", bodyAsString) | ||||
| 	} | ||||
| 	if w.HeaderMap.Get("Content-Type") != "text/html; charset=utf-8" { | ||||
| 		t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type")) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // TestHandleHeadToDir - ensure the root/sub dir handles properly | ||||
| func TestHandleHeadToDir(t *testing.T) { | ||||
| 	// SETUP | ||||
| 	r := New() | ||||
| 	r.Static("/", "./") | ||||
|  | ||||
| 	// RUN | ||||
| 	w := PerformRequest(r, "HEAD", "/") | ||||
|  | ||||
| 	// TEST | ||||
| 	bodyAsString := w.Body.String() | ||||
| 	if w.Code != 200 { | ||||
| 		t.Errorf("Response code should be Ok, was: %d", w.Code) | ||||
| 	} | ||||
| 	if len(bodyAsString) == 0 { | ||||
| 		t.Errorf("Got empty body instead of file tree") | ||||
| 	} | ||||
| 	if !strings.Contains(bodyAsString, "gin.go") { | ||||
| 		t.Errorf("Can't find:`gin.go` in file tree: %s", bodyAsString) | ||||
| 	} | ||||
| 	if w.HeaderMap.Get("Content-Type") != "text/html; charset=utf-8" { | ||||
| 		t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type")) | ||||
| 	} | ||||
| 	assert.Equal(t, router.Handlers[0], middleware2) | ||||
| 	assert.Equal(t, router.Handlers[1], middleware1) | ||||
| 	assert.Equal(t, router.noMethod[0], middleware0) | ||||
| 	assert.Equal(t, router.allNoMethod[0], middleware2) | ||||
| 	assert.Equal(t, router.allNoMethod[1], middleware1) | ||||
| 	assert.Equal(t, router.allNoMethod[2], middleware0) | ||||
| } | ||||
|  | ||||
| @ -25,14 +25,13 @@ func ErrorLogger() HandlerFunc { | ||||
| 	return ErrorLoggerT(ErrorTypeAll) | ||||
| } | ||||
|  | ||||
| func ErrorLoggerT(typ uint32) HandlerFunc { | ||||
| func ErrorLoggerT(typ int) HandlerFunc { | ||||
| 	return func(c *Context) { | ||||
| 		c.Next() | ||||
|  | ||||
| 		if !c.Writer.Written() { | ||||
| 			errs := c.Errors.ByType(typ) | ||||
| 			if len(errs) > 0 { | ||||
| 				c.JSON(-1, c.Errors) | ||||
| 			if errs := c.Errors.ByType(typ); len(errs) > 0 { | ||||
| 				c.JSON(-1, errs) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
							
								
								
									
										3
									
								
								mode.go
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								mode.go
									
									
									
									
									
								
							| @ -5,7 +5,6 @@ | ||||
| package gin | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
| 	"os" | ||||
|  | ||||
| 	"github.com/mattn/go-colorable" | ||||
| @ -46,7 +45,7 @@ func SetMode(value string) { | ||||
| 	case TestMode: | ||||
| 		ginMode = testCode | ||||
| 	default: | ||||
| 		log.Panic("gin mode unknown: " + value) | ||||
| 		panic("gin mode unknown: " + value) | ||||
| 	} | ||||
| 	modeName = value | ||||
| } | ||||
|  | ||||
| @ -18,11 +18,11 @@ func TestPanicInHandler(t *testing.T) { | ||||
| 	r := New() | ||||
| 	r.Use(Recovery()) | ||||
| 	r.GET("/recovery", func(_ *Context) { | ||||
| 		log.Panic("Oupps, Houston, we have a problem") | ||||
| 		panic("Oupps, Houston, we have a problem") | ||||
| 	}) | ||||
|  | ||||
| 	// RUN | ||||
| 	w := PerformRequest(r, "GET", "/recovery") | ||||
| 	w := performRequest(r, "GET", "/recovery") | ||||
|  | ||||
| 	// restore logging | ||||
| 	log.SetOutput(os.Stderr) | ||||
| @ -40,11 +40,11 @@ func TestPanicWithAbort(t *testing.T) { | ||||
| 	r.Use(Recovery()) | ||||
| 	r.GET("/recovery", func(c *Context) { | ||||
| 		c.AbortWithStatus(400) | ||||
| 		log.Panic("Oupps, Houston, we have a problem") | ||||
| 		panic("Oupps, Houston, we have a problem") | ||||
| 	}) | ||||
|  | ||||
| 	// RUN | ||||
| 	w := PerformRequest(r, "GET", "/recovery") | ||||
| 	w := performRequest(r, "GET", "/recovery") | ||||
|  | ||||
| 	// restore logging | ||||
| 	log.SetOutput(os.Stderr) | ||||
|  | ||||
							
								
								
									
										332
									
								
								routes_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										332
									
								
								routes_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,332 @@ | ||||
| // Copyright 2014 Manu Martinez-Almeida.  All rights reserved. | ||||
| // Use of this source code is governed by a MIT style | ||||
| // license that can be found in the LICENSE file. | ||||
|  | ||||
| package gin | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"io/ioutil" | ||||
| 	"net/http" | ||||
| 	"net/http/httptest" | ||||
| 	"os" | ||||
| 	"path" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder { | ||||
| 	req, _ := http.NewRequest(method, path, nil) | ||||
| 	w := httptest.NewRecorder() | ||||
| 	r.ServeHTTP(w, req) | ||||
| 	return w | ||||
| } | ||||
|  | ||||
| func testRouteOK(method string, t *testing.T) { | ||||
| 	// SETUP | ||||
| 	passed := false | ||||
| 	r := New() | ||||
| 	r.Handle(method, "/test", []HandlerFunc{func(c *Context) { | ||||
| 		passed = true | ||||
| 	}}) | ||||
| 	// RUN | ||||
| 	w := performRequest(r, method, "/test") | ||||
|  | ||||
| 	// TEST | ||||
| 	assert.True(t, passed) | ||||
| 	assert.Equal(t, w.Code, http.StatusOK) | ||||
| } | ||||
|  | ||||
| // TestSingleRouteOK tests that POST route is correctly invoked. | ||||
| func testRouteNotOK(method string, t *testing.T) { | ||||
| 	// SETUP | ||||
| 	passed := false | ||||
| 	router := New() | ||||
| 	router.Handle(method, "/test_2", []HandlerFunc{func(c *Context) { | ||||
| 		passed = true | ||||
| 	}}) | ||||
|  | ||||
| 	// RUN | ||||
| 	w := performRequest(router, method, "/test") | ||||
|  | ||||
| 	// TEST | ||||
| 	assert.False(t, passed) | ||||
| 	assert.Equal(t, w.Code, http.StatusNotFound) | ||||
| } | ||||
|  | ||||
| // TestSingleRouteOK tests that POST route is correctly invoked. | ||||
| func testRouteNotOK2(method string, t *testing.T) { | ||||
| 	// SETUP | ||||
| 	passed := false | ||||
| 	router := New() | ||||
| 	var methodRoute string | ||||
| 	if method == "POST" { | ||||
| 		methodRoute = "GET" | ||||
| 	} else { | ||||
| 		methodRoute = "POST" | ||||
| 	} | ||||
| 	router.Handle(methodRoute, "/test", []HandlerFunc{func(c *Context) { | ||||
| 		passed = true | ||||
| 	}}) | ||||
|  | ||||
| 	// RUN | ||||
| 	w := performRequest(router, method, "/test") | ||||
|  | ||||
| 	// TEST | ||||
| 	assert.False(t, passed) | ||||
| 	assert.Equal(t, w.Code, http.StatusMethodNotAllowed) | ||||
| } | ||||
|  | ||||
| func TestRouterGroupRouteOK(t *testing.T) { | ||||
| 	testRouteOK("POST", t) | ||||
| 	testRouteOK("DELETE", t) | ||||
| 	testRouteOK("PATCH", t) | ||||
| 	testRouteOK("PUT", t) | ||||
| 	testRouteOK("OPTIONS", t) | ||||
| 	testRouteOK("HEAD", t) | ||||
| } | ||||
|  | ||||
| // TestSingleRouteOK tests that POST route is correctly invoked. | ||||
| func TestRouteNotOK(t *testing.T) { | ||||
| 	testRouteNotOK("POST", t) | ||||
| 	testRouteNotOK("DELETE", t) | ||||
| 	testRouteNotOK("PATCH", t) | ||||
| 	testRouteNotOK("PUT", t) | ||||
| 	testRouteNotOK("OPTIONS", t) | ||||
| 	testRouteNotOK("HEAD", t) | ||||
| } | ||||
|  | ||||
| // TestSingleRouteOK tests that POST route is correctly invoked. | ||||
| func TestRouteNotOK2(t *testing.T) { | ||||
| 	testRouteNotOK2("POST", t) | ||||
| 	testRouteNotOK2("DELETE", t) | ||||
| 	testRouteNotOK2("PATCH", t) | ||||
| 	testRouteNotOK2("PUT", t) | ||||
| 	testRouteNotOK2("OPTIONS", t) | ||||
| 	testRouteNotOK2("HEAD", t) | ||||
| } | ||||
|  | ||||
| // TestHandleStaticFile - ensure the static file handles properly | ||||
| func TestHandleStaticFile(t *testing.T) { | ||||
| 	// SETUP file | ||||
| 	testRoot, _ := os.Getwd() | ||||
| 	f, err := ioutil.TempFile(testRoot, "") | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 	defer os.Remove(f.Name()) | ||||
| 	filePath := path.Join("/", path.Base(f.Name())) | ||||
| 	f.WriteString("Gin Web Framework") | ||||
| 	f.Close() | ||||
|  | ||||
| 	// SETUP gin | ||||
| 	r := New() | ||||
| 	r.Static("./", testRoot) | ||||
|  | ||||
| 	// RUN | ||||
| 	w := performRequest(r, "GET", filePath) | ||||
|  | ||||
| 	// TEST | ||||
| 	if w.Code != 200 { | ||||
| 		t.Errorf("Response code should be 200, was: %d", w.Code) | ||||
| 	} | ||||
| 	if w.Body.String() != "Gin Web Framework" { | ||||
| 		t.Errorf("Response should be test, was: %s", w.Body.String()) | ||||
| 	} | ||||
| 	if w.HeaderMap.Get("Content-Type") != "text/plain; charset=utf-8" { | ||||
| 		t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type")) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // TestHandleStaticDir - ensure the root/sub dir handles properly | ||||
| func TestHandleStaticDir(t *testing.T) { | ||||
| 	// SETUP | ||||
| 	r := New() | ||||
| 	r.Static("/", "./") | ||||
|  | ||||
| 	// RUN | ||||
| 	w := performRequest(r, "GET", "/") | ||||
|  | ||||
| 	// TEST | ||||
| 	bodyAsString := w.Body.String() | ||||
| 	if w.Code != 200 { | ||||
| 		t.Errorf("Response code should be 200, was: %d", w.Code) | ||||
| 	} | ||||
| 	if len(bodyAsString) == 0 { | ||||
| 		t.Errorf("Got empty body instead of file tree") | ||||
| 	} | ||||
| 	if !strings.Contains(bodyAsString, "gin.go") { | ||||
| 		t.Errorf("Can't find:`gin.go` in file tree: %s", bodyAsString) | ||||
| 	} | ||||
| 	if w.HeaderMap.Get("Content-Type") != "text/html; charset=utf-8" { | ||||
| 		t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type")) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // TestHandleHeadToDir - ensure the root/sub dir handles properly | ||||
| func TestHandleHeadToDir(t *testing.T) { | ||||
| 	// SETUP | ||||
| 	router := New() | ||||
| 	router.Static("/", "./") | ||||
|  | ||||
| 	// RUN | ||||
| 	w := performRequest(router, "HEAD", "/") | ||||
|  | ||||
| 	// TEST | ||||
| 	bodyAsString := w.Body.String() | ||||
| 	assert.Equal(t, w.Code, 200) | ||||
| 	assert.NotEmpty(t, bodyAsString) | ||||
| 	assert.Contains(t, bodyAsString, "gin.go") | ||||
| 	assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8") | ||||
| } | ||||
|  | ||||
| func TestContextGeneralCase(t *testing.T) { | ||||
| 	signature := "" | ||||
| 	router := New() | ||||
| 	router.Use(func(c *Context) { | ||||
| 		signature += "A" | ||||
| 		c.Next() | ||||
| 		signature += "B" | ||||
| 	}) | ||||
| 	router.Use(func(c *Context) { | ||||
| 		signature += "C" | ||||
| 	}) | ||||
| 	router.GET("/", func(c *Context) { | ||||
| 		signature += "D" | ||||
| 	}) | ||||
| 	router.NoRoute(func(c *Context) { | ||||
| 		signature += "X" | ||||
| 	}) | ||||
| 	router.NoMethod(func(c *Context) { | ||||
| 		signature += "X" | ||||
| 	}) | ||||
| 	// RUN | ||||
| 	w := performRequest(router, "GET", "/") | ||||
|  | ||||
| 	// TEST | ||||
| 	assert.Equal(t, w.Code, 200) | ||||
| 	assert.Equal(t, signature, "ACDB") | ||||
| } | ||||
|  | ||||
| // TestBadAbortHandlersChain - ensure that Abort after switch context will not interrupt pending handlers | ||||
| func TestContextNextOrder(t *testing.T) { | ||||
| 	signature := "" | ||||
| 	router := New() | ||||
| 	router.Use(func(c *Context) { | ||||
| 		signature += "A" | ||||
| 		c.Next() | ||||
| 		signature += "B" | ||||
| 	}) | ||||
| 	router.Use(func(c *Context) { | ||||
| 		signature += "C" | ||||
| 		c.Next() | ||||
| 		signature += "D" | ||||
| 	}) | ||||
| 	router.NoRoute(func(c *Context) { | ||||
| 		signature += "E" | ||||
| 		c.Next() | ||||
| 		signature += "F" | ||||
| 	}, func(c *Context) { | ||||
| 		signature += "G" | ||||
| 		c.Next() | ||||
| 		signature += "H" | ||||
| 	}) | ||||
| 	// RUN | ||||
| 	w := performRequest(router, "GET", "/") | ||||
|  | ||||
| 	// TEST | ||||
| 	assert.Equal(t, w.Code, 404) | ||||
| 	assert.Equal(t, signature, "ACEGHFDB") | ||||
| } | ||||
|  | ||||
| // TestAbortHandlersChain - ensure that Abort interrupt used middlewares in fifo order | ||||
| func TestAbortHandlersChain(t *testing.T) { | ||||
| 	signature := "" | ||||
| 	router := New() | ||||
| 	router.Use(func(c *Context) { | ||||
| 		signature += "A" | ||||
| 	}) | ||||
| 	router.Use(func(c *Context) { | ||||
| 		signature += "C" | ||||
| 		c.AbortWithStatus(409) | ||||
| 		c.Next() | ||||
| 		signature += "D" | ||||
| 	}) | ||||
| 	router.GET("/", func(c *Context) { | ||||
| 		signature += "D" | ||||
| 		c.Next() | ||||
| 		signature += "E" | ||||
| 	}) | ||||
|  | ||||
| 	// RUN | ||||
| 	w := performRequest(router, "GET", "/") | ||||
|  | ||||
| 	// TEST | ||||
| 	assert.Equal(t, signature, "ACD") | ||||
| 	assert.Equal(t, w.Code, 409) | ||||
| } | ||||
|  | ||||
| func TestAbortHandlersChainAndNext(t *testing.T) { | ||||
| 	signature := "" | ||||
| 	router := New() | ||||
| 	router.Use(func(c *Context) { | ||||
| 		signature += "A" | ||||
| 		c.AbortWithStatus(410) | ||||
| 		c.Next() | ||||
| 		signature += "B" | ||||
|  | ||||
| 	}) | ||||
| 	router.GET("/", func(c *Context) { | ||||
| 		signature += "C" | ||||
| 		c.Next() | ||||
| 	}) | ||||
| 	// RUN | ||||
| 	w := performRequest(router, "GET", "/") | ||||
|  | ||||
| 	// TEST | ||||
| 	assert.Equal(t, signature, "AB") | ||||
| 	assert.Equal(t, w.Code, 410) | ||||
| } | ||||
|  | ||||
| // TestContextParamsGet tests that a parameter can be parsed from the URL. | ||||
| func TestContextParamsByName(t *testing.T) { | ||||
| 	name := "" | ||||
| 	lastName := "" | ||||
| 	router := New() | ||||
| 	router.GET("/test/:name/:last_name", func(c *Context) { | ||||
| 		name = c.Params.ByName("name") | ||||
| 		lastName = c.Params.ByName("last_name") | ||||
| 	}) | ||||
| 	// RUN | ||||
| 	w := performRequest(router, "GET", "/test/john/smith") | ||||
|  | ||||
| 	// TEST | ||||
| 	assert.Equal(t, w.Code, 200) | ||||
| 	assert.Equal(t, name, "john") | ||||
| 	assert.Equal(t, lastName, "smith") | ||||
| } | ||||
|  | ||||
| // TestFailHandlersChain - ensure that Fail interrupt used middlewares in fifo order as | ||||
| // as well as Abort | ||||
| func TestFailHandlersChain(t *testing.T) { | ||||
| 	// SETUP | ||||
| 	var stepsPassed int = 0 | ||||
| 	r := New() | ||||
| 	r.Use(func(context *Context) { | ||||
| 		stepsPassed += 1 | ||||
| 		context.Fail(500, errors.New("foo")) | ||||
| 	}) | ||||
| 	r.Use(func(context *Context) { | ||||
| 		stepsPassed += 1 | ||||
| 		context.Next() | ||||
| 		stepsPassed += 1 | ||||
| 	}) | ||||
| 	// RUN | ||||
| 	w := performRequest(r, "GET", "/") | ||||
|  | ||||
| 	// TEST | ||||
| 	assert.Equal(t, w.Code, 500, "Response code should be Server error, was: %d", w.Code) | ||||
| 	assert.Equal(t, stepsPassed, 1, "Falied to switch context in handler function: %d", stepsPassed) | ||||
| } | ||||
							
								
								
									
										19
									
								
								utils.go
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								utils.go
									
									
									
									
									
								
							| @ -6,7 +6,6 @@ package gin | ||||
|  | ||||
| import ( | ||||
| 	"encoding/xml" | ||||
| 	"log" | ||||
| 	"reflect" | ||||
| 	"runtime" | ||||
| 	"strings" | ||||
| @ -50,29 +49,33 @@ func filterFlags(content string) string { | ||||
| func chooseData(custom, wildcard interface{}) interface{} { | ||||
| 	if custom == nil { | ||||
| 		if wildcard == nil { | ||||
| 			log.Panic("negotiation config is invalid") | ||||
| 			panic("negotiation config is invalid") | ||||
| 		} | ||||
| 		return wildcard | ||||
| 	} | ||||
| 	return custom | ||||
| } | ||||
|  | ||||
| func parseAccept(acceptHeader string) (parts []string) { | ||||
| 	parts = strings.Split(acceptHeader, ",") | ||||
| 	for i, part := range parts { | ||||
| func parseAccept(acceptHeader string) []string { | ||||
| 	parts := strings.Split(acceptHeader, ",") | ||||
| 	out := make([]string, 0, len(parts)) | ||||
| 	for _, part := range parts { | ||||
| 		index := strings.IndexByte(part, ';') | ||||
| 		if index >= 0 { | ||||
| 			part = part[0:index] | ||||
| 		} | ||||
| 		parts[i] = strings.TrimSpace(part) | ||||
| 		part = strings.TrimSpace(part) | ||||
| 		if len(part) > 0 { | ||||
| 			out = append(out, part) | ||||
| 		} | ||||
| 	return | ||||
| 	} | ||||
| 	return out | ||||
| } | ||||
|  | ||||
| func lastChar(str string) uint8 { | ||||
| 	size := len(str) | ||||
| 	if size == 0 { | ||||
| 		log.Panic("The length of the string can't be 0") | ||||
| 		panic("The length of the string can't be 0") | ||||
| 	} | ||||
| 	return str[size-1] | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user