func TestIdFull(t *testing.T) { idPool := NewIdPool(10) for i := 0; i < 10; i++ { idPool.GetId() } _, err := idPool.GetId() assert.Error(t, err) }
func testBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) { assert.Equal(t, b.Name(), name) obj := FooStruct{} req := requestWithBody("POST", path, body) err := b.Bind(req, &obj) assert.NoError(t, err) assert.Equal(t, obj.Foo, "bar") obj = FooStruct{} req = requestWithBody("POST", badPath, badBody) err = JSON.Bind(req, &obj) assert.Error(t, err) }
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"` Bar string `json:"bar"` } assert.False(t, c.IsAborted()) assert.Error(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()) }
func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) { b := Form assert.Equal(t, b.Name(), "form") obj := FooBarStruct{} req := requestWithBody(method, path, body) if method == "POST" { req.Header.Add("Content-Type", MIMEPOSTForm) } err := b.Bind(req, &obj) assert.NoError(t, err) assert.Equal(t, obj.Foo, "bar") assert.Equal(t, obj.Bar, "foo") obj = FooBarStruct{} req = requestWithBody(method, badPath, badBody) err = JSON.Bind(req, &obj) assert.Error(t, err) }
func TestRun(t *testing.T) { buffer := new(bytes.Buffer) router := New() go func() { router.Use(LoggerWithWriter(buffer)) router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) router.Run(":5150") }() // have to wait for the goroutine to start and run the server // otherwise the main thread will complete time.Sleep(5 * time.Millisecond) assert.Error(t, router.Run(":5150")) resp, err := http.Get("http://localhost:5150/example") defer resp.Body.Close() assert.NoError(t, err) body, ioerr := ioutil.ReadAll(resp.Body) assert.NoError(t, ioerr) assert.Equal(t, "it worked", string(body[:]), "resp body should match") assert.Equal(t, "200 OK", resp.Status, "should get a 200") }
// Error asserts that a function returned an error (i.e. not `nil`). // // actualObj, err := SomeFunction() // require.Error(t, err, "An error was expected") // require.Equal(t, err, expectedError) // } func Error(t TestingT, err error, msgAndArgs ...interface{}) { if !assert.Error(t, err, msgAndArgs...) { t.FailNow() } }
// Test no colon separated filter and proper filters mixed. func TestQueryNoColonSeparatorMulti(t *testing.T) { _, err := _parseSearchQuery("test test1:24 test2") assert.Error(t, err, "Should detect missing colon.") }
// Test a range query with no lower bounds term. func TestRangeQueryBotchedMin(t *testing.T) { _, err := _parseSearchQuery("test:..5") assert.Error(t, err, "Malformed range-query passed") }
func TestValidationFails(t *testing.T) { var obj FooStruct req := requestWithBody("POST", "/", `{"bar": "foo"}`) err := JSON.Bind(req, &obj) assert.Error(t, err) }
func TestBadUnixSocket(t *testing.T) { router := New() assert.Error(t, router.RunUnix("#/tmp/unix_unit_test")) }
// Test no colon separated filter. func TestQueryNoColonSeparator(t *testing.T) { _, err := _parseQuery("test") assert.Error(t, err, "Should detect missing colon.") }
// Test a range query with no upper bounds term. func TestRangeQueryBotchedMax(t *testing.T) { _, err := _parseQuery("test:5..") assert.Error(t, err, "Malformed range-query passed") }