Make CreateTestContext public without importing net/http/httptest
This commit is contained in:
parent
f931d1ea80
commit
61ba9db5af
115
context_test.go
115
context_test.go
@ -74,7 +74,7 @@ func TestContextReset(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextHandlers(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
assert.Nil(t, c.handlers)
|
||||
assert.Nil(t, c.handlers.Last())
|
||||
|
||||
@ -95,7 +95,7 @@ func TestContextHandlers(t *testing.T) {
|
||||
// TestContextSetGet tests that a parameter is set correctly on the
|
||||
// current context and can be retrieved using Get.
|
||||
func TestContextSetGet(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Set("foo", "bar")
|
||||
|
||||
value, err := c.Get("foo")
|
||||
@ -111,7 +111,7 @@ func TestContextSetGet(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextSetGetValues(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Set("string", "this is a string")
|
||||
c.Set("int32", int32(-42))
|
||||
c.Set("int64", int64(42424242424242))
|
||||
@ -132,7 +132,7 @@ func TestContextSetGetValues(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextCopy(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.index = 2
|
||||
c.Request, _ = http.NewRequest("POST", "/hola", nil)
|
||||
c.handlers = HandlersChain{func(c *Context) {}}
|
||||
@ -151,7 +151,7 @@ func TestContextCopy(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextHandlerName(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.handlers = HandlersChain{func(c *Context) {}, handlerNameTest}
|
||||
|
||||
assert.Regexp(t, "^(.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest$", c.HandlerName())
|
||||
@ -162,7 +162,7 @@ func handlerNameTest(c *Context) {
|
||||
}
|
||||
|
||||
func TestContextQuery(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("GET", "http://example.com/?foo=bar&page=10&id=", nil)
|
||||
|
||||
value, ok := c.GetQuery("foo")
|
||||
@ -197,7 +197,7 @@ func TestContextQuery(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextQueryAndPostForm(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
body := bytes.NewBufferString("foo=bar&page=11&both=&foo=second")
|
||||
c.Request, _ = http.NewRequest("POST", "/?both=GET&id=main&id=omit&array[]=first&array[]=second", body)
|
||||
c.Request.Header.Add("Content-Type", MIMEPOSTForm)
|
||||
@ -254,7 +254,7 @@ func TestContextQueryAndPostForm(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextPostFormMultipart(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request = createMultipartRequest()
|
||||
|
||||
var obj struct {
|
||||
@ -302,13 +302,13 @@ func TestContextPostFormMultipart(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextSetCookie(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.SetCookie("user", "gin", 1, "/", "localhost", true, true)
|
||||
assert.Equal(t, c.Writer.Header().Get("Set-Cookie"), "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure")
|
||||
}
|
||||
|
||||
func TestContextGetCookie(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("GET", "/get", nil)
|
||||
c.Request.Header.Set("Cookie", "user=gin")
|
||||
cookie, _ := c.Cookie("user")
|
||||
@ -318,7 +318,9 @@ func TestContextGetCookie(t *testing.T) {
|
||||
// Tests that the response is serialized as JSON
|
||||
// and Content-Type is set to application/json
|
||||
func TestContextRenderJSON(t *testing.T) {
|
||||
c, w, _ := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.JSON(201, H{"foo": "bar"})
|
||||
|
||||
assert.Equal(t, w.Code, 201)
|
||||
@ -329,7 +331,9 @@ func TestContextRenderJSON(t *testing.T) {
|
||||
// Tests that the response is serialized as JSON
|
||||
// we change the content-type before
|
||||
func TestContextRenderAPIJSON(t *testing.T) {
|
||||
c, w, _ := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.Header("Content-Type", "application/vnd.api+json")
|
||||
c.JSON(201, H{"foo": "bar"})
|
||||
|
||||
@ -341,7 +345,9 @@ func TestContextRenderAPIJSON(t *testing.T) {
|
||||
// Tests that the response is serialized as JSON
|
||||
// and Content-Type is set to application/json
|
||||
func TestContextRenderIndentedJSON(t *testing.T) {
|
||||
c, w, _ := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.IndentedJSON(201, H{"foo": "bar", "bar": "foo", "nested": H{"foo": "bar"}})
|
||||
|
||||
assert.Equal(t, w.Code, 201)
|
||||
@ -352,7 +358,8 @@ func TestContextRenderIndentedJSON(t *testing.T) {
|
||||
// Tests that the response executes the templates
|
||||
// and responds with Content-Type set to text/html
|
||||
func TestContextRenderHTML(t *testing.T) {
|
||||
c, w, router := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, router := CreateTestContext(w)
|
||||
templ := template.Must(template.New("t").Parse(`Hello {{.name}}`))
|
||||
router.SetHTMLTemplate(templ)
|
||||
|
||||
@ -366,7 +373,9 @@ func TestContextRenderHTML(t *testing.T) {
|
||||
// TestContextXML tests that the response is serialized as XML
|
||||
// and Content-Type is set to application/xml
|
||||
func TestContextRenderXML(t *testing.T) {
|
||||
c, w, _ := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.XML(201, H{"foo": "bar"})
|
||||
|
||||
assert.Equal(t, w.Code, 201)
|
||||
@ -377,7 +386,9 @@ func TestContextRenderXML(t *testing.T) {
|
||||
// TestContextString tests that the response is returned
|
||||
// with Content-Type set to text/plain
|
||||
func TestContextRenderString(t *testing.T) {
|
||||
c, w, _ := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.String(201, "test %s %d", "string", 2)
|
||||
|
||||
assert.Equal(t, w.Code, 201)
|
||||
@ -388,7 +399,9 @@ func TestContextRenderString(t *testing.T) {
|
||||
// TestContextString tests that the response is returned
|
||||
// with Content-Type set to text/html
|
||||
func TestContextRenderHTMLString(t *testing.T) {
|
||||
c, w, _ := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.Header("Content-Type", "text/html; charset=utf-8")
|
||||
c.String(201, "<html>%s %d</html>", "string", 3)
|
||||
|
||||
@ -400,7 +413,9 @@ func TestContextRenderHTMLString(t *testing.T) {
|
||||
// TestContextData tests that the response can be written from `bytesting`
|
||||
// with specified MIME type
|
||||
func TestContextRenderData(t *testing.T) {
|
||||
c, w, _ := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.Data(201, "text/csv", []byte(`foo,bar`))
|
||||
|
||||
assert.Equal(t, w.Code, 201)
|
||||
@ -409,7 +424,9 @@ func TestContextRenderData(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextRenderSSE(t *testing.T) {
|
||||
c, w, _ := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.SSEvent("float", 1.5)
|
||||
c.Render(-1, sse.Event{
|
||||
Id: "123",
|
||||
@ -424,7 +441,9 @@ func TestContextRenderSSE(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextRenderFile(t *testing.T) {
|
||||
c, w, _ := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.Request, _ = http.NewRequest("GET", "/", nil)
|
||||
c.File("./gin.go")
|
||||
|
||||
@ -436,7 +455,9 @@ func TestContextRenderFile(t *testing.T) {
|
||||
// TestContextRenderYAML tests that the response is serialized as YAML
|
||||
// and Content-Type is set to application/x-yaml
|
||||
func TestContextRenderYAML(t *testing.T) {
|
||||
c, w, _ := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.YAML(201, H{"foo": "bar"})
|
||||
|
||||
assert.Equal(t, w.Code, 201)
|
||||
@ -445,7 +466,7 @@ func TestContextRenderYAML(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextHeaders(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Header("Content-Type", "text/plain")
|
||||
c.Header("X-Custom", "value")
|
||||
|
||||
@ -462,7 +483,9 @@ func TestContextHeaders(t *testing.T) {
|
||||
|
||||
// TODO
|
||||
func TestContextRenderRedirectWithRelativePath(t *testing.T) {
|
||||
c, w, _ := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
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") })
|
||||
@ -474,7 +497,9 @@ func TestContextRenderRedirectWithRelativePath(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextRenderRedirectWithAbsolutePath(t *testing.T) {
|
||||
c, w, _ := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.Request, _ = http.NewRequest("POST", "http://example.com", nil)
|
||||
c.Redirect(302, "http://google.com")
|
||||
c.Writer.WriteHeaderNow()
|
||||
@ -484,7 +509,9 @@ func TestContextRenderRedirectWithAbsolutePath(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextRenderRedirectWith201(t *testing.T) {
|
||||
c, w, _ := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.Request, _ = http.NewRequest("POST", "http://example.com", nil)
|
||||
c.Redirect(201, "/resource")
|
||||
c.Writer.WriteHeaderNow()
|
||||
@ -494,7 +521,7 @@ func TestContextRenderRedirectWith201(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextRenderRedirectAll(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("POST", "http://example.com", nil)
|
||||
assert.Panics(t, func() { c.Redirect(200, "/resource") })
|
||||
assert.Panics(t, func() { c.Redirect(202, "/resource") })
|
||||
@ -505,7 +532,7 @@ func TestContextRenderRedirectAll(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextNegotiationFormat(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("POST", "", nil)
|
||||
|
||||
assert.Panics(t, func() { c.NegotiateFormat() })
|
||||
@ -514,7 +541,7 @@ func TestContextNegotiationFormat(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextNegotiationFormatWithAccept(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
c.Request.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
|
||||
|
||||
@ -524,7 +551,7 @@ func TestContextNegotiationFormatWithAccept(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextNegotiationFormatCustum(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
c.Request.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
|
||||
|
||||
@ -537,7 +564,7 @@ func TestContextNegotiationFormatCustum(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextIsAborted(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
assert.False(t, c.IsAborted())
|
||||
|
||||
c.Abort()
|
||||
@ -553,7 +580,9 @@ func TestContextIsAborted(t *testing.T) {
|
||||
// TestContextData tests that the response can be written from `bytesting`
|
||||
// with specified MIME type
|
||||
func TestContextAbortWithStatus(t *testing.T) {
|
||||
c, w, _ := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.index = 4
|
||||
c.AbortWithStatus(401)
|
||||
|
||||
@ -564,7 +593,7 @@ func TestContextAbortWithStatus(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextError(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
assert.Empty(t, c.Errors)
|
||||
|
||||
c.Error(errors.New("first error"))
|
||||
@ -590,7 +619,7 @@ func TestContextError(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextTypedError(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Error(errors.New("externo 0")).SetType(ErrorTypePublic)
|
||||
c.Error(errors.New("interno 0")).SetType(ErrorTypePrivate)
|
||||
|
||||
@ -604,7 +633,9 @@ func TestContextTypedError(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextAbortWithError(t *testing.T) {
|
||||
c, w, _ := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.AbortWithError(401, errors.New("bad input")).SetMeta("some input")
|
||||
|
||||
assert.Equal(t, w.Code, 401)
|
||||
@ -613,7 +644,7 @@ func TestContextAbortWithError(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextClientIP(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
|
||||
c.Request.Header.Set("X-Real-IP", " 10.10.10.10 ")
|
||||
@ -633,7 +664,7 @@ func TestContextClientIP(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextContentType(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
c.Request.Header.Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
||||
@ -641,7 +672,7 @@ func TestContextContentType(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextAutoBindJSON(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
|
||||
c.Request.Header.Add("Content-Type", MIMEJSON)
|
||||
|
||||
@ -656,7 +687,9 @@ func TestContextAutoBindJSON(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextBindWithJSON(t *testing.T) {
|
||||
c, w, _ := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
|
||||
c.Request.Header.Add("Content-Type", MIMEXML) // set fake content-type
|
||||
|
||||
@ -671,7 +704,9 @@ func TestContextBindWithJSON(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextBadAutoBind(t *testing.T) {
|
||||
c, w, _ := CreateTestContext()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.Request, _ = http.NewRequest("POST", "http://example.com", bytes.NewBufferString("\"foo\":\"bar\", \"bar\":\"foo\"}"))
|
||||
c.Request.Header.Add("Content-Type", MIMEJSON)
|
||||
var obj struct {
|
||||
@ -690,7 +725,7 @@ func TestContextBadAutoBind(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextGolangContext(t *testing.T) {
|
||||
c, _, _ := CreateTestContext()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
|
||||
assert.NoError(t, c.Err())
|
||||
assert.Nil(t, c.Done())
|
||||
|
@ -1,14 +0,0 @@
|
||||
package gin
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
)
|
||||
|
||||
func CreateTestContext() (c *Context, w *httptest.ResponseRecorder, r *Engine) {
|
||||
w = httptest.NewRecorder()
|
||||
r = New()
|
||||
c = r.allocateContext()
|
||||
c.reset()
|
||||
c.writermem.reset(w)
|
||||
return
|
||||
}
|
13
test_helpers.go
Normal file
13
test_helpers.go
Normal file
@ -0,0 +1,13 @@
|
||||
package gin
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func CreateTestContext(w http.ResponseWriter) (c *Context, r *Engine) {
|
||||
r = New()
|
||||
c = r.allocateContext()
|
||||
c.reset()
|
||||
c.writermem.reset(w)
|
||||
return
|
||||
}
|
Loading…
Reference in New Issue
Block a user