You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.0KB

  1. package versioning_test
  2. import (
  3. "testing"
  4. "github.com/kataras/iris/v12"
  5. "github.com/kataras/iris/v12/httptest"
  6. "github.com/kataras/iris/v12/versioning"
  7. )
  8. func notFoundHandler(ctx iris.Context) {
  9. ctx.NotFound()
  10. }
  11. const (
  12. v10Response = "v1.0 handler"
  13. v2Response = "v2.x handler"
  14. )
  15. func sendHandler(contents string) iris.Handler {
  16. return func(ctx iris.Context) {
  17. ctx.WriteString(contents)
  18. }
  19. }
  20. func TestNewGroup(t *testing.T) {
  21. app := iris.New()
  22. userAPI := app.Party("/api/user")
  23. // [... static serving, middlewares and etc goes here].
  24. userAPIV10 := versioning.NewGroup(userAPI, "1.0.0").Deprecated(versioning.DefaultDeprecationOptions)
  25. userAPIV10.Get("/", sendHandler(v10Response))
  26. userAPIV2 := versioning.NewGroup(userAPI, ">= 2.0.0 < 3.0.0")
  27. userAPIV2.Get("/", sendHandler(v2Response))
  28. userAPIV2.Post("/", sendHandler(v2Response))
  29. userAPIV2.Put("/other", sendHandler(v2Response))
  30. e := httptest.New(t, app)
  31. ex := e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "1.0.0").Expect()
  32. ex.Status(iris.StatusOK).Body().Equal(v10Response)
  33. ex.Header("X-API-Warn").Equal(versioning.DefaultDeprecationOptions.WarnMessage)
  34. e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "2.0.0").Expect().
  35. Status(iris.StatusOK).Body().Equal(v2Response)
  36. e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "2.1.0").Expect().
  37. Status(iris.StatusOK).Body().Equal(v2Response)
  38. e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "2.9.9").Expect().
  39. Status(iris.StatusOK).Body().Equal(v2Response)
  40. e.POST("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "2.0.0").Expect().
  41. Status(iris.StatusOK).Body().Equal(v2Response)
  42. e.PUT("/api/user/other").WithHeader(versioning.AcceptVersionHeaderKey, "2.9.0").Expect().
  43. Status(iris.StatusOK).Body().Equal(v2Response)
  44. e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "3.0").Expect().
  45. Status(iris.StatusNotImplemented).Body().Equal("version not found")
  46. }