Skip to content
Myr

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

2 files+49 added−44 removed

modified:context_test.go+35 added-44 removed
23 unmodified lines
24// 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)2728func 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}3628func createMultipartRequest() *http.Request {29 boundary := "--testboundary"30 body := new(bytes.Buffer)31 mw := multipart.NewWriter(body)
40 unmodified lines
72 assert.Equal(t, c.Writer.(*responseWriter), &c.writermem)73}7475func TestContextHandlers(t *testing.T) {85 c, _, _ := createTestContext()76 c, _, _ := CreateTestContext()77 assert.Nil(t, c.handlers)78 assert.Nil(t, c.handlers.Last())7980 c.handlers = HandlersChain{}
12 unmodified lines
9394// TestContextSetGet tests that a parameter is set correctly on the95// current context and can be retrieved using Get.96func TestContextSetGet(t *testing.T) {106 c, _, _ := createTestContext()97 c, _, _ := CreateTestContext()98 c.Set("foo", "bar")99100 value, err := c.Get("foo")101 assert.Equal(t, value, "bar")
7 unmodified lines
109 assert.Panics(t, func() { c.MustGet("no_exist") })110}111112func 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 lines
130131}132133func 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 lines
149 assert.Equal(t, cp.Params, c.Params)150}151152func TestContextHandlerName(t *testing.T) {162 c, _, _ := createTestContext()153 c, _, _ := CreateTestContext()154 c.handlers = HandlersChain{func(c *Context) {}, handlerNameTest}155156 assert.Equal(t, c.HandlerName(), "github.com/gin-gonic/gin.handlerNameTest")157}
2 unmodified lines
160161}162163func TestContextQuery(t *testing.T) {173 c, _, _ := createTestContext()164 c, _, _ := CreateTestContext()165 c.Request, _ = http.NewRequest("GET", "http://example.com/?foo=bar&page=10", nil)166167 assert.Equal(t, c.DefaultQuery("foo", "none"), "bar")168 assert.Equal(t, c.Query("foo"), "bar")
8 unmodified lines
177 assert.Empty(t, c.PostForm("NoKey"))178}179180func 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 lines
217 assert.Equal(t, obj.Array, []string{"first", "second"})218}219220func TestContextPostFormMultipart(t *testing.T) {230 c, _, _ := createTestContext()221 c, _, _ := CreateTestContext()222 c.Request = createMultipartRequest()223224 var obj struct {225 Foo string `form:"foo"`
14 unmodified lines
240241// Tests that the response is serialized as JSON242// and Content-Type is set to application/json243func TestContextRenderJSON(t *testing.T) {253 c, w, _ := createTestContext()244 c, w, _ := CreateTestContext()245 c.JSON(201, H{"foo": "bar"})246247 assert.Equal(t, w.Code, 201)248 assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n")
2 unmodified lines
251252// Tests that the response is serialized as JSON253// we change the content-type before254func 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"})258259 assert.Equal(t, w.Code, 201)
3 unmodified lines
263264// Tests that the response is serialized as JSON265// and Content-Type is set to application/json266func 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"}})269270 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 lines
274275// Tests that the response executes the templates276// and responds with Content-Type set to text/html277func 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)281282 c.HTML(201, "t", H{"name": "alexandernyquist"})
5 unmodified lines
288289// TestContextXML tests that the response is serialized as XML290// and Content-Type is set to application/xml291func TestContextRenderXML(t *testing.T) {301 c, w, _ := createTestContext()292 c, w, _ := CreateTestContext()293 c.XML(201, H{"foo": "bar"})294295 assert.Equal(t, w.Code, 201)296 assert.Equal(t, w.Body.String(), "<map><foo>bar</foo></map>")
2 unmodified lines
299300// TestContextString tests that the response is returned301// with Content-Type set to text/plain302func TestContextRenderString(t *testing.T) {312 c, w, _ := createTestContext()303 c, w, _ := CreateTestContext()304 c.String(201, "test %s %d", "string", 2)305306 assert.Equal(t, w.Code, 201)307 assert.Equal(t, w.Body.String(), "test string 2")
2 unmodified lines
310311// TestContextString tests that the response is returned312// with Content-Type set to text/html313func 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)317318 assert.Equal(t, w.Code, 201)
3 unmodified lines
322323// TestContextData tests that the response can be written from `bytesting`324// with specified MIME type325func TestContextRenderData(t *testing.T) {335 c, w, _ := createTestContext()326 c, w, _ := CreateTestContext()327 c.Data(201, "text/csv", []byte(`foo,bar`))328329 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}333334func 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 lines
346 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}348349func TestContextRenderFile(t *testing.T) {359 c, w, _ := createTestContext()350 c, w, _ := CreateTestContext()351 c.Request, _ = http.NewRequest("GET", "/", nil)352 c.File("./gin.go")353354 assert.Equal(t, w.Code, 200)
1 unmodified line
356 assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8")357}358359func TestContextHeaders(t *testing.T) {369 c, _, _ := createTestContext()360 c, _, _ := CreateTestContext()361 c.Header("Content-Type", "text/plain")362 c.Header("X-Custom", "value")363364 assert.Equal(t, c.Writer.Header().Get("Content-Type"), "text/plain")
8 unmodified lines
373}374375// TODO376func 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 lines
385 assert.Equal(t, w.Header().Get("Location"), "/path")386}387388func 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 line
395 assert.Equal(t, w.Header().Get("Location"), "http://google.com")396}397398func TestContextNegotiationFormat(t *testing.T) {408 c, _, _ := createTestContext()399 c, _, _ := CreateTestContext()400 c.Request, _ = http.NewRequest("POST", "", nil)401402 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}406407func 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")411412 assert.Equal(t, c.NegotiateFormat(MIMEJSON, MIMEXML), MIMEXML)
1 unmodified line
414 assert.Equal(t, c.NegotiateFormat(MIMEJSON), "")415}416417func 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")421422 c.Accepted = nil
4 unmodified lines
427 assert.Equal(t, c.NegotiateFormat(MIMEJSON), MIMEJSON)428}429430func TestContextIsAborted(t *testing.T) {440 c, _, _ := createTestContext()431 c, _, _ := CreateTestContext()432 assert.False(t, c.IsAborted())433434 c.Abort()435 assert.True(t, c.IsAborted())
7 unmodified lines
443444// TestContextData tests that the response can be written from `bytesting`445// with specified MIME type446func 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 lines
455 assert.True(t, c.IsAborted())456}457458func TestContextError(t *testing.T) {468 c, _, _ := createTestContext()459 c, _, _ := CreateTestContext()460 assert.Empty(t, c.Errors)461462 c.Error(errors.New("first error"))463 assert.Len(t, c.Errors, 1)
17 unmodified lines
481 assert.Equal(t, c.Errors.Last(), c.Errors[1])482}483484func 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)488489 for _, err := range c.Errors.ByType(ErrorTypePublic) {
5 unmodified lines
495 assert.Equal(t, c.Errors.Errors(), []string{"externo 0", "interno 0"})496}497498func 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()502503 assert.Equal(t, w.Code, 401)
1 unmodified line
505 assert.True(t, c.IsAborted())506}507508func TestContextClientIP(t *testing.T) {518 c, _, _ := createTestContext()509 c, _, _ := CreateTestContext()510 c.Request, _ = http.NewRequest("POST", "/", nil)511512 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 lines
525 assert.Equal(t, c.ClientIP(), "40.40.40.40")526}527528func 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")532533 assert.Equal(t, c.ContentType(), "application/json")534}535536func 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)540541 var obj struct {
6 unmodified lines
548 assert.Empty(t, c.Errors)549}550551func 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-type555556 var obj struct {
6 unmodified lines
563 assert.Equal(t, w.Body.Len(), 0)564}565566func 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 lines
582 assert.True(t, c.IsAborted())583}584585func 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 lines
added:test_helpers.go+14 added
1package gin23import (4 "net/http/httptest"5)67func 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}