Ejemplo n.º 1
0
func TestMuxAPI(t *testing.T) {
	iris.ResetDefault()

	middlewareResponseText := "I assume that you are authenticated\n"
	iris.API("/users", testUserAPI{}, func(ctx *iris.Context) { // optional middleware for .API
		// do your work here, or render a login window if not logged in, get the user and send it to the next middleware, or do  all here
		ctx.Set("user", "username")
		ctx.Next()
	}, func(ctx *iris.Context) {
		if ctx.Get("user") == "username" {
			ctx.Write(middlewareResponseText)
			ctx.Next()
		} else {
			ctx.SetStatusCode(iris.StatusUnauthorized)
		}
	})

	e := httptest.New(iris.Default, t)

	userID := "4077"
	formname := "kataras"

	e.GET("/users").Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Get Users\n")
	e.GET("/users/" + userID).Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Get By " + userID + "\n")
	e.PUT("/users").WithFormField("name", formname).Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Put, name: " + formname + "\n")
	e.POST("/users/"+userID).WithFormField("name", formname).Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Post By " + userID + ", name: " + formname + "\n")
	e.DELETE("/users/" + userID).Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Delete By " + userID + "\n")
}
Ejemplo n.º 2
0
func registerAPI() {
	room := api.Room{}
	room.Init()
	// this is other way to declare routes using the 'API'
	// Api handler
	iris.API("/rooms", room)
}