Exemple #1
0
func TestMuxFireMethodNotAllowed(t *testing.T) {
	iris.ResetDefault()
	iris.Default.Config.FireMethodNotAllowed = true
	h := func(ctx *iris.Context) {
		ctx.Write("%s", ctx.MethodString())
	}

	iris.Default.OnError(iris.StatusMethodNotAllowed, func(ctx *iris.Context) {
		ctx.Write("Hello from my custom 405 page")
	})

	iris.Get("/mypath", h)
	iris.Put("/mypath", h)

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

	e.GET("/mypath").Expect().Status(iris.StatusOK).Body().Equal("GET")
	e.PUT("/mypath").Expect().Status(iris.StatusOK).Body().Equal("PUT")
	// this should fail with 405 and catch by the custom http error

	e.POST("/mypath").Expect().Status(iris.StatusMethodNotAllowed).Body().Equal("Hello from my custom 405 page")
	iris.Close()
}
Exemple #2
0
func TestContextFlashMessages(t *testing.T) {
	iris.ResetDefault()
	firstKey := "name"
	lastKey := "package"

	values := pathParameters{pathParameter{Key: firstKey, Value: "kataras"}, pathParameter{Key: lastKey, Value: "iris"}}
	jsonExpected := map[string]string{firstKey: "kataras", lastKey: "iris"}
	// set the flashes, the cookies are filled
	iris.Put("/set", func(ctx *iris.Context) {
		for _, v := range values {
			ctx.SetFlash(v.Key, v.Value)
		}
	})

	// get the first flash, the next should be available to the next requess
	iris.Get("/get_first_flash", func(ctx *iris.Context) {
		for _, v := range values {
			val, err := ctx.GetFlash(v.Key)
			if err == nil {
				ctx.JSON(iris.StatusOK, map[string]string{v.Key: val})
			} else {
				ctx.JSON(iris.StatusOK, nil) // return nil
			}

			break
		}

	})

	// just an empty handler to test if the flashes should remeain to the next if GetFlash/GetFlashes used
	iris.Get("/get_no_getflash", func(ctx *iris.Context) {
	})

	// get the last flash, the next should be available to the next requess
	iris.Get("/get_last_flash", func(ctx *iris.Context) {
		for i, v := range values {
			if i == len(values)-1 {
				val, err := ctx.GetFlash(v.Key)
				if err == nil {
					ctx.JSON(iris.StatusOK, map[string]string{v.Key: val})
				} else {
					ctx.JSON(iris.StatusOK, nil) // return nil
				}

			}
		}
	})

	iris.Get("/get_zero_flashes", func(ctx *iris.Context) {
		ctx.JSON(iris.StatusOK, ctx.GetFlashes()) // should return nil
	})

	// we use the GetFlash to get the flash messages, the messages and the cookies should be empty after that
	iris.Get("/get_flash", func(ctx *iris.Context) {
		kv := make(map[string]string)
		for _, v := range values {
			val, err := ctx.GetFlash(v.Key)
			if err == nil {
				kv[v.Key] = val
			}
		}
		ctx.JSON(iris.StatusOK, kv)
	}, func(ctx *iris.Context) {
		// at the same request, flashes should be available
		if len(ctx.GetFlashes()) == 0 {
			t.Fatalf("Flashes should be remeain to the whole request lifetime")
		}
	})

	iris.Get("/get_flashes", func(ctx *iris.Context) {
		// one time one handler, using GetFlashes
		kv := make(map[string]string)
		flashes := ctx.GetFlashes()
		//second time on the same handler, using the GetFlash
		for k := range flashes {
			kv[k], _ = ctx.GetFlash(k)
		}
		if len(flashes) != len(kv) {
			ctx.SetStatusCode(iris.StatusNoContent)
			return
		}
		ctx.Next()

	}, func(ctx *iris.Context) {
		// third time on a next handler
		// test the if next handler has access to them(must) because flash are request lifetime now.
		// print them to the client for test the response also
		ctx.JSON(iris.StatusOK, ctx.GetFlashes())
	})

	e := httptest.New(iris.Default, t)
	e.PUT("/set").Expect().Status(iris.StatusOK).Cookies().NotEmpty()
	e.GET("/get_first_flash").Expect().Status(iris.StatusOK).JSON().Object().ContainsKey(firstKey).NotContainsKey(lastKey)
	// just a request which does not use the flash message, so flash messages should be available on the next request
	e.GET("/get_no_getflash").Expect().Status(iris.StatusOK)
	e.GET("/get_last_flash").Expect().Status(iris.StatusOK).JSON().Object().ContainsKey(lastKey).NotContainsKey(firstKey)
	g := e.GET("/get_zero_flashes").Expect().Status(iris.StatusOK)
	g.JSON().Null()
	g.Cookies().Empty()
	// set the magain
	e.PUT("/set").Expect().Status(iris.StatusOK).Cookies().NotEmpty()
	// get them again using GetFlash
	e.GET("/get_flash").Expect().Status(iris.StatusOK).JSON().Object().Equal(jsonExpected)
	// this should be empty again
	g = e.GET("/get_zero_flashes").Expect().Status(iris.StatusOK)
	g.JSON().Null()
	g.Cookies().Empty()
	//set them again
	e.PUT("/set").Expect().Status(iris.StatusOK).Cookies().NotEmpty()
	// get them again using GetFlashes
	e.GET("/get_flashes").Expect().Status(iris.StatusOK).JSON().Object().Equal(jsonExpected)
	// this should be empty again
	g = e.GET("/get_zero_flashes").Expect().Status(iris.StatusOK)
	g.JSON().Null()
	g.Cookies().Empty()

	// test Get, and get again should return nothing
	e.PUT("/set").Expect().Status(iris.StatusOK).Cookies().NotEmpty()
	e.GET("/get_first_flash").Expect().Status(iris.StatusOK).JSON().Object().ContainsKey(firstKey).NotContainsKey(lastKey)
	g = e.GET("/get_first_flash").Expect().Status(iris.StatusOK)
	g.JSON().Null()
	g.Cookies().Empty()
}