コード例 #1
0
func TestUnixSocket(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.RunUnix("/tmp/unix_unit_test")
	}()
	// have to wait for the goroutine to start and run the server
	// otherwise the main thread will complete
	time.Sleep(5 * time.Millisecond)

	c, err := net.Dial("unix", "/tmp/unix_unit_test")
	assert.NoError(t, err)

	fmt.Fprintf(c, "GET /example HTTP/1.0\r\n\r\n")
	scanner := bufio.NewScanner(c)
	var response string
	for scanner.Scan() {
		response += scanner.Text()
	}
	assert.Contains(t, response, "HTTP/1.0 200", "should get a 200")
	assert.Contains(t, response, "it worked", "resp body should match")
}
コード例 #2
0
ファイル: recovery_test.go プロジェクト: ZhuZhengyi/eris-db
// TestPanicInHandler assert that panic has been recovered.
func TestPanicInHandler(t *testing.T) {
	buffer := new(bytes.Buffer)
	router := New()
	router.Use(RecoveryWithWriter(buffer))
	router.GET("/recovery", func(_ *Context) {
		panic("Oupps, Houston, we have a problem")
	})
	// RUN
	w := performRequest(router, "GET", "/recovery")
	// TEST
	assert.Equal(t, w.Code, 500)
	assert.Contains(t, buffer.String(), "Panic recovery -> Oupps, Houston, we have a problem")
	assert.Contains(t, buffer.String(), "TestPanicInHandler")
}
コード例 #3
0
ファイル: githubapi_test.go プロジェクト: ZhuZhengyi/eris-db
func TestGithubAPI(t *testing.T) {
	DefaultWriter = newMockWriter()
	router := Default()
	githubConfigRouter(router)

	for _, route := range githubAPI {
		path, values := exampleFromPath(route.path)
		w := performRequest(router, route.method, path)

		// TEST
		assert.Contains(t, w.Body.String(), "\"status\":\"good\"")
		for _, value := range values {
			str := fmt.Sprintf("\"%s\":\"%s\"", value.Key, value.Value)
			assert.Contains(t, w.Body.String(), str)
		}
	}
}
コード例 #4
0
ファイル: context_test.go プロジェクト: ZhuZhengyi/eris-db
func TestContextRenderFile(t *testing.T) {
	c, w, _ := createTestContext()
	c.Request, _ = http.NewRequest("GET", "/", nil)
	c.File("./gin.go")

	assert.Equal(t, w.Code, 200)
	assert.Contains(t, w.Body.String(), "func New() *Engine {")
	assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8")
}
コード例 #5
0
ファイル: routes_test.go プロジェクト: ZhuZhengyi/eris-db
// TestHandleStaticDir - ensure the root/sub dir handles properly
func TestRouteStaticListingDir(t *testing.T) {
	router := New()
	router.StaticFS("/", Dir("./", true))

	w := performRequest(router, "GET", "/")

	assert.Equal(t, w.Code, 200)
	assert.Contains(t, w.Body.String(), "gin.go")
	assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8")
}
コード例 #6
0
ファイル: gin_test.go プロジェクト: ZhuZhengyi/eris-db
func TestListOfRoutes(t *testing.T) {
	router := New()
	router.GET("/favicon.ico", handler_test1)
	router.GET("/", handler_test1)
	group := router.Group("/users")
	{
		group.GET("/", handler_test2)
		group.GET("/:id", handler_test1)
		group.POST("/:id", handler_test2)
	}
	router.Static("/static", ".")

	list := router.Routes()

	assert.Len(t, list, 7)
	assert.Contains(t, list, RouteInfo{
		Method:  "GET",
		Path:    "/favicon.ico",
		Handler: "github.com/gin-gonic/gin.handler_test1",
	})
	assert.Contains(t, list, RouteInfo{
		Method:  "GET",
		Path:    "/",
		Handler: "github.com/gin-gonic/gin.handler_test1",
	})
	assert.Contains(t, list, RouteInfo{
		Method:  "GET",
		Path:    "/users/",
		Handler: "github.com/gin-gonic/gin.handler_test2",
	})
	assert.Contains(t, list, RouteInfo{
		Method:  "GET",
		Path:    "/users/:id",
		Handler: "github.com/gin-gonic/gin.handler_test1",
	})
	assert.Contains(t, list, RouteInfo{
		Method:  "POST",
		Path:    "/users/:id",
		Handler: "github.com/gin-gonic/gin.handler_test2",
	})
}
コード例 #7
0
ファイル: auth_test.go プロジェクト: ZhuZhengyi/eris-db
func TestBasicAuth(t *testing.T) {
	pairs := processAccounts(Accounts{
		"admin": "password",
		"foo":   "bar",
		"bar":   "foo",
	})

	assert.Len(t, pairs, 3)
	assert.Contains(t, pairs, authPair{
		User:  "******",
		Value: "Basic YmFyOmZvbw==",
	})
	assert.Contains(t, pairs, authPair{
		User:  "******",
		Value: "Basic Zm9vOmJhcg==",
	})
	assert.Contains(t, pairs, authPair{
		User:  "******",
		Value: "Basic YWRtaW46cGFzc3dvcmQ=",
	})
}
コード例 #8
0
ファイル: suite_test.go プロジェクト: alexandrev/eris-db
func TestSuiteLogging(t *testing.T) {
	testT := testing.T{}

	suiteLoggingTester := new(SuiteLoggingTester)

	capture := StdoutCapture{}
	capture.StartCapture()
	Run(&testT, suiteLoggingTester)
	output, err := capture.StopCapture()

	assert.Nil(t, err, "Got an error trying to capture stdout!")

	// Failed tests' output is always printed
	assert.Contains(t, output, "TESTLOGFAIL")

	if testing.Verbose() {
		// In verbose mode, output from successful tests is also printed
		assert.Contains(t, output, "TESTLOGPASS")
	} else {
		assert.NotContains(t, output, "TESTLOGPASS")
	}
}
コード例 #9
0
ファイル: routes_test.go プロジェクト: ZhuZhengyi/eris-db
func TestRouterMiddlewareAndStatic(t *testing.T) {
	router := New()
	static := router.Group("/", func(c *Context) {
		c.Writer.Header().Add("Last-Modified", "Mon, 02 Jan 2006 15:04:05 MST")
		c.Writer.Header().Add("Expires", "Mon, 02 Jan 2006 15:04:05 MST")
		c.Writer.Header().Add("X-GIN", "Gin Framework")
	})
	static.Static("/", "./")

	w := performRequest(router, "GET", "/gin.go")

	assert.Equal(t, w.Code, 200)
	assert.Contains(t, w.Body.String(), "package gin")
	assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8")
	assert.NotEqual(t, w.HeaderMap.Get("Last-Modified"), "Mon, 02 Jan 2006 15:04:05 MST")
	assert.Equal(t, w.HeaderMap.Get("Expires"), "Mon, 02 Jan 2006 15:04:05 MST")
	assert.Equal(t, w.HeaderMap.Get("x-GIN"), "Gin Framework")
}
コード例 #10
0
ファイル: requirements.go プロジェクト: alexandrev/eris-db
// Contains asserts that the specified string contains the specified substring.
//
//    require.Contains(t, "Hello World", "World", "But 'Hello World' does contain 'World'")
func Contains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) {
	if !assert.Contains(t, s, contains, msgAndArgs...) {
		t.FailNow()
	}
}
コード例 #11
0
ファイル: logger_test.go プロジェクト: ZhuZhengyi/eris-db
func TestLogger(t *testing.T) {
	buffer := new(bytes.Buffer)
	router := New()
	router.Use(LoggerWithWriter(buffer))
	router.GET("/example", func(c *Context) {})
	router.POST("/example", func(c *Context) {})
	router.PUT("/example", func(c *Context) {})
	router.DELETE("/example", func(c *Context) {})
	router.PATCH("/example", func(c *Context) {})
	router.HEAD("/example", func(c *Context) {})
	router.OPTIONS("/example", func(c *Context) {})

	performRequest(router, "GET", "/example")
	assert.Contains(t, buffer.String(), "200")
	assert.Contains(t, buffer.String(), "GET")
	assert.Contains(t, buffer.String(), "/example")

	// I wrote these first (extending the above) but then realized they are more
	// like integration tests because they test the whole logging process rather
	// than individual functions.  Im not sure where these should go.

	performRequest(router, "POST", "/example")
	assert.Contains(t, buffer.String(), "200")
	assert.Contains(t, buffer.String(), "POST")
	assert.Contains(t, buffer.String(), "/example")

	performRequest(router, "PUT", "/example")
	assert.Contains(t, buffer.String(), "200")
	assert.Contains(t, buffer.String(), "PUT")
	assert.Contains(t, buffer.String(), "/example")

	performRequest(router, "DELETE", "/example")
	assert.Contains(t, buffer.String(), "200")
	assert.Contains(t, buffer.String(), "DELETE")
	assert.Contains(t, buffer.String(), "/example")

	performRequest(router, "PATCH", "/example")
	assert.Contains(t, buffer.String(), "200")
	assert.Contains(t, buffer.String(), "PATCH")
	assert.Contains(t, buffer.String(), "/example")

	performRequest(router, "HEAD", "/example")
	assert.Contains(t, buffer.String(), "200")
	assert.Contains(t, buffer.String(), "HEAD")
	assert.Contains(t, buffer.String(), "/example")

	performRequest(router, "OPTIONS", "/example")
	assert.Contains(t, buffer.String(), "200")
	assert.Contains(t, buffer.String(), "OPTIONS")
	assert.Contains(t, buffer.String(), "/example")

	performRequest(router, "GET", "/notfound")
	assert.Contains(t, buffer.String(), "404")
	assert.Contains(t, buffer.String(), "GET")
	assert.Contains(t, buffer.String(), "/notfound")

}