Merge branch 'danielalves-master' into develop
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Javier Provecho Fernandez · committed by Javier Provecho Fernandez · parents 50d9af7, afbf1cf · Browse files · 2 files·+49 added−44 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+49 added−44 removed
- other:+49 added−44 removed
Provenance
- Commit
ee82083- Tree
f263e45- Author
Javier Provecho Fernandez <javiertitan@gmail.com>- Committer
Javier Provecho Fernandez <javiertitan@gmail.com>- Parent 1
50d9af7- Parent 2
afbf1cf
2 files changed+49 added−44 removed
modified:context_test.go+35 added-44 removed
23 unmodified lines24 // func (c *Context) Negotiate(code int, config Negotiate) {25 // BAD case: func (c *Context) Render(code int, render render.Render, obj ...interface{}) {26 // test that information is not leaked when reusing Contexts (using the Pool)27 28-func createTestContext() (c *Context, w *httptest.ResponseRecorder, r *Engine) {29- w = httptest.NewRecorder()30- r = New()31- c = r.allocateContext()32- c.reset()33- c.writermem.reset(w)34- return35-}36-28 func createMultipartRequest() *http.Request {29 boundary := "--testboundary"30 body := new(bytes.Buffer)31 mw := multipart.NewWriter(body)40 unmodified lines72 assert.Equal(t, c.Writer.(*responseWriter), &c.writermem)73 }74 75 func TestContextHandlers(t *testing.T) {85- c, _, _ := createTestContext()76+ c, _, _ := CreateTestContext()77 assert.Nil(t, c.handlers)78 assert.Nil(t, c.handlers.Last())79 80 c.handlers = HandlersChain{}12 unmodified lines93 94 // TestContextSetGet tests that a parameter is set correctly on the95 // current context and can be retrieved using Get.96 func TestContextSetGet(t *testing.T) {106- c, _, _ := createTestContext()97+ c, _, _ := CreateTestContext()98 c.Set("foo", "bar")99 100 value, err := c.Get("foo")101 assert.Equal(t, value, "bar")7 unmodified lines109 assert.Panics(t, func() { c.MustGet("no_exist") })110 }111 112 func TestContextSetGetValues(t *testing.T) {122- c, _, _ := createTestContext()113+ c, _, _ := CreateTestContext()114 c.Set("string", "this is a string")115 c.Set("int32", int32(-42))116 c.Set("int64", int64(42424242424242))117 c.Set("uint64", uint64(42))12 unmodified lines130 131 }132 133 func TestContextCopy(t *testing.T) {143- c, _, _ := createTestContext()134+ c, _, _ := CreateTestContext()135 c.index = 2136 c.Request, _ = http.NewRequest("POST", "/hola", nil)137 c.handlers = HandlersChain{func(c *Context) {}}138 c.Params = Params{Param{Key: "foo", Value: "bar"}}10 unmodified lines149 assert.Equal(t, cp.Params, c.Params)150 }151 152 func TestContextHandlerName(t *testing.T) {162- c, _, _ := createTestContext()153+ c, _, _ := CreateTestContext()154 c.handlers = HandlersChain{func(c *Context) {}, handlerNameTest}155 156 assert.Equal(t, c.HandlerName(), "github.com/gin-gonic/gin.handlerNameTest")157 }2 unmodified lines160 161 }162 163 func TestContextQuery(t *testing.T) {173- c, _, _ := createTestContext()164+ c, _, _ := CreateTestContext()165 c.Request, _ = http.NewRequest("GET", "http://example.com/?foo=bar&page=10", nil)166 167 assert.Equal(t, c.DefaultQuery("foo", "none"), "bar")168 assert.Equal(t, c.Query("foo"), "bar")8 unmodified lines177 assert.Empty(t, c.PostForm("NoKey"))178 }179 180 func TestContextQueryAndPostForm(t *testing.T) {190- c, _, _ := createTestContext()181+ c, _, _ := CreateTestContext()182 body := bytes.NewBufferString("foo=bar&page=11&both=POST&foo=second")183 c.Request, _ = http.NewRequest("POST", "/?both=GET&id=main&id=omit&array[]=first&array[]=second", body)184 c.Request.Header.Add("Content-Type", MIMEPOSTForm)185 31 unmodified lines217 assert.Equal(t, obj.Array, []string{"first", "second"})218 }219 220 func TestContextPostFormMultipart(t *testing.T) {230- c, _, _ := createTestContext()221+ c, _, _ := CreateTestContext()222 c.Request = createMultipartRequest()223 224 var obj struct {225 Foo string `form:"foo"`14 unmodified lines240 241 // Tests that the response is serialized as JSON242 // and Content-Type is set to application/json243 func TestContextRenderJSON(t *testing.T) {253- c, w, _ := createTestContext()244+ c, w, _ := CreateTestContext()245 c.JSON(201, H{"foo": "bar"})246 247 assert.Equal(t, w.Code, 201)248 assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n")2 unmodified lines251 252 // Tests that the response is serialized as JSON253 // we change the content-type before254 func TestContextRenderAPIJSON(t *testing.T) {264- c, w, _ := createTestContext()255+ c, w, _ := CreateTestContext()256 c.Header("Content-Type", "application/vnd.api+json")257 c.JSON(201, H{"foo": "bar"})258 259 assert.Equal(t, w.Code, 201)3 unmodified lines263 264 // Tests that the response is serialized as JSON265 // and Content-Type is set to application/json266 func TestContextRenderIndentedJSON(t *testing.T) {276- c, w, _ := createTestContext()267+ c, w, _ := CreateTestContext()268 c.IndentedJSON(201, H{"foo": "bar", "bar": "foo", "nested": H{"foo": "bar"}})269 270 assert.Equal(t, w.Code, 201)271 assert.Equal(t, w.Body.String(), "{\n \"bar\": \"foo\",\n \"foo\": \"bar\",\n \"nested\": {\n \"foo\": \"bar\"\n }\n}")2 unmodified lines274 275 // Tests that the response executes the templates276 // and responds with Content-Type set to text/html277 func TestContextRenderHTML(t *testing.T) {287- c, w, router := createTestContext()278+ c, w, router := CreateTestContext()279 templ := template.Must(template.New("t").Parse(`Hello {{.name}}`))280 router.SetHTMLTemplate(templ)281 282 c.HTML(201, "t", H{"name": "alexandernyquist"})5 unmodified lines288 289 // TestContextXML tests that the response is serialized as XML290 // and Content-Type is set to application/xml291 func TestContextRenderXML(t *testing.T) {301- c, w, _ := createTestContext()292+ c, w, _ := CreateTestContext()293 c.XML(201, H{"foo": "bar"})294 295 assert.Equal(t, w.Code, 201)296 assert.Equal(t, w.Body.String(), "<map><foo>bar</foo></map>")2 unmodified lines299 300 // TestContextString tests that the response is returned301 // with Content-Type set to text/plain302 func TestContextRenderString(t *testing.T) {312- c, w, _ := createTestContext()303+ c, w, _ := CreateTestContext()304 c.String(201, "test %s %d", "string", 2)305 306 assert.Equal(t, w.Code, 201)307 assert.Equal(t, w.Body.String(), "test string 2")2 unmodified lines310 311 // TestContextString tests that the response is returned312 // with Content-Type set to text/html313 func TestContextRenderHTMLString(t *testing.T) {323- c, w, _ := createTestContext()314+ c, w, _ := CreateTestContext()315 c.Header("Content-Type", "text/html; charset=utf-8")316 c.String(201, "<html>%s %d</html>", "string", 3)317 318 assert.Equal(t, w.Code, 201)3 unmodified lines322 323 // TestContextData tests that the response can be written from `bytesting`324 // with specified MIME type325 func TestContextRenderData(t *testing.T) {335- c, w, _ := createTestContext()326+ c, w, _ := CreateTestContext()327 c.Data(201, "text/csv", []byte(`foo,bar`))328 329 assert.Equal(t, w.Code, 201)330 assert.Equal(t, w.Body.String(), "foo,bar")331 assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/csv")332 }333 334 func TestContextRenderSSE(t *testing.T) {344- c, w, _ := createTestContext()335+ c, w, _ := CreateTestContext()336 c.SSEvent("float", 1.5)337 c.Render(-1, sse.Event{338 Id: "123",339 Data: "text",6 unmodified lines346 assert.Equal(t, strings.Replace(w.Body.String(), " ", "", -1), strings.Replace("event:float\ndata:1.5\n\nid:123\ndata:text\n\nevent:chat\ndata:{\"bar\":\"foo\",\"foo\":\"bar\"}\n\n", " ", "", -1))347 }348 349 func TestContextRenderFile(t *testing.T) {359- c, w, _ := createTestContext()350+ c, w, _ := CreateTestContext()351 c.Request, _ = http.NewRequest("GET", "/", nil)352 c.File("./gin.go")353 354 assert.Equal(t, w.Code, 200)1 unmodified line356 assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8")357 }358 359 func TestContextHeaders(t *testing.T) {369- c, _, _ := createTestContext()360+ c, _, _ := CreateTestContext()361 c.Header("Content-Type", "text/plain")362 c.Header("X-Custom", "value")363 364 assert.Equal(t, c.Writer.Header().Get("Content-Type"), "text/plain")8 unmodified lines373 }374 375 // TODO376 func TestContextRenderRedirectWithRelativePath(t *testing.T) {386- c, w, _ := createTestContext()377+ c, w, _ := CreateTestContext()378 c.Request, _ = http.NewRequest("POST", "http://example.com", nil)379 assert.Panics(t, func() { c.Redirect(299, "/new_path") })380 assert.Panics(t, func() { c.Redirect(309, "/new_path") })381 3 unmodified lines385 assert.Equal(t, w.Header().Get("Location"), "/path")386 }387 388 func TestContextRenderRedirectWithAbsolutePath(t *testing.T) {398- c, w, _ := createTestContext()389+ c, w, _ := CreateTestContext()390 c.Request, _ = http.NewRequest("POST", "http://example.com", nil)391 c.Redirect(302, "http://google.com")392 c.Writer.WriteHeaderNow()393 1 unmodified line395 assert.Equal(t, w.Header().Get("Location"), "http://google.com")396 }397 398 func TestContextNegotiationFormat(t *testing.T) {408- c, _, _ := createTestContext()399+ c, _, _ := CreateTestContext()400 c.Request, _ = http.NewRequest("POST", "", nil)401 402 assert.Panics(t, func() { c.NegotiateFormat() })403 assert.Equal(t, c.NegotiateFormat(MIMEJSON, MIMEXML), MIMEJSON)404 assert.Equal(t, c.NegotiateFormat(MIMEHTML, MIMEJSON), MIMEHTML)405 }406 407 func TestContextNegotiationFormatWithAccept(t *testing.T) {417- c, _, _ := createTestContext()408+ c, _, _ := CreateTestContext()409 c.Request, _ = http.NewRequest("POST", "/", nil)410 c.Request.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")411 412 assert.Equal(t, c.NegotiateFormat(MIMEJSON, MIMEXML), MIMEXML)1 unmodified line414 assert.Equal(t, c.NegotiateFormat(MIMEJSON), "")415 }416 417 func TestContextNegotiationFormatCustum(t *testing.T) {427- c, _, _ := createTestContext()418+ c, _, _ := CreateTestContext()419 c.Request, _ = http.NewRequest("POST", "/", nil)420 c.Request.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")421 422 c.Accepted = nil4 unmodified lines427 assert.Equal(t, c.NegotiateFormat(MIMEJSON), MIMEJSON)428 }429 430 func TestContextIsAborted(t *testing.T) {440- c, _, _ := createTestContext()431+ c, _, _ := CreateTestContext()432 assert.False(t, c.IsAborted())433 434 c.Abort()435 assert.True(t, c.IsAborted())7 unmodified lines443 444 // TestContextData tests that the response can be written from `bytesting`445 // with specified MIME type446 func TestContextAbortWithStatus(t *testing.T) {456- c, w, _ := createTestContext()447+ c, w, _ := CreateTestContext()448 c.index = 4449 c.AbortWithStatus(401)450 c.Writer.WriteHeaderNow()451 3 unmodified lines455 assert.True(t, c.IsAborted())456 }457 458 func TestContextError(t *testing.T) {468- c, _, _ := createTestContext()459+ c, _, _ := CreateTestContext()460 assert.Empty(t, c.Errors)461 462 c.Error(errors.New("first error"))463 assert.Len(t, c.Errors, 1)17 unmodified lines481 assert.Equal(t, c.Errors.Last(), c.Errors[1])482 }483 484 func TestContextTypedError(t *testing.T) {494- c, _, _ := createTestContext()485+ c, _, _ := CreateTestContext()486 c.Error(errors.New("externo 0")).SetType(ErrorTypePublic)487 c.Error(errors.New("interno 0")).SetType(ErrorTypePrivate)488 489 for _, err := range c.Errors.ByType(ErrorTypePublic) {5 unmodified lines495 assert.Equal(t, c.Errors.Errors(), []string{"externo 0", "interno 0"})496 }497 498 func TestContextAbortWithError(t *testing.T) {508- c, w, _ := createTestContext()499+ c, w, _ := CreateTestContext()500 c.AbortWithError(401, errors.New("bad input")).SetMeta("some input")501 c.Writer.WriteHeaderNow()502 503 assert.Equal(t, w.Code, 401)1 unmodified line505 assert.True(t, c.IsAborted())506 }507 508 func TestContextClientIP(t *testing.T) {518- c, _, _ := createTestContext()509+ c, _, _ := CreateTestContext()510 c.Request, _ = http.NewRequest("POST", "/", nil)511 512 c.Request.Header.Set("X-Real-IP", " 10.10.10.10 ")513 c.Request.Header.Set("X-Forwarded-For", " 20.20.20.20, 30.30.30.30")11 unmodified lines525 assert.Equal(t, c.ClientIP(), "40.40.40.40")526 }527 528 func TestContextContentType(t *testing.T) {538- c, _, _ := createTestContext()529+ c, _, _ := CreateTestContext()530 c.Request, _ = http.NewRequest("POST", "/", nil)531 c.Request.Header.Set("Content-Type", "application/json; charset=utf-8")532 533 assert.Equal(t, c.ContentType(), "application/json")534 }535 536 func TestContextAutoBindJSON(t *testing.T) {546- c, _, _ := createTestContext()537+ c, _, _ := CreateTestContext()538 c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))539 c.Request.Header.Add("Content-Type", MIMEJSON)540 541 var obj struct {6 unmodified lines548 assert.Empty(t, c.Errors)549 }550 551 func TestContextBindWithJSON(t *testing.T) {561- c, w, _ := createTestContext()552+ c, w, _ := CreateTestContext()553 c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))554 c.Request.Header.Add("Content-Type", MIMEXML) // set fake content-type555 556 var obj struct {6 unmodified lines563 assert.Equal(t, w.Body.Len(), 0)564 }565 566 func TestContextBadAutoBind(t *testing.T) {576- c, w, _ := createTestContext()567+ c, w, _ := CreateTestContext()568 c.Request, _ = http.NewRequest("POST", "http://example.com", bytes.NewBufferString("\"foo\":\"bar\", \"bar\":\"foo\"}"))569 c.Request.Header.Add("Content-Type", MIMEJSON)570 var obj struct {571 Foo string `json:"foo"`10 unmodified lines582 assert.True(t, c.IsAborted())583 }584 585 func TestContextGolangContext(t *testing.T) {595- c, _, _ := createTestContext()586+ c, _, _ := CreateTestContext()587 c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))588 assert.NoError(t, c.Err())589 assert.Nil(t, c.Done())590 ti, ok := c.Deadline()9 unmodified linesadded:test_helpers.go+14 added
1+package gin2+3+import (4+ "net/http/httptest"5+)6+7+func CreateTestContext() (c *Context, w *httptest.ResponseRecorder, r *Engine) {8+ w = httptest.NewRecorder()9+ r = New()10+ c = r.allocateContext()11+ c.reset()12+ c.writermem.reset(w)13+ return14+}