httptest.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package httptest
  2. import (
  3. "crypto/tls"
  4. "net/http"
  5. "testing"
  6. "github.com/iris-contrib/httpexpect"
  7. "github.com/kataras/iris"
  8. )
  9. type (
  10. // OptionSetter sets a configuration field to the configuration
  11. OptionSetter interface {
  12. // Set receives a pointer to the Configuration type and does the job of filling it
  13. Set(c *Configuration)
  14. }
  15. // OptionSet implements the OptionSetter
  16. OptionSet func(c *Configuration)
  17. )
  18. // Set is the func which makes the OptionSet an OptionSetter, this is used mostly
  19. func (o OptionSet) Set(c *Configuration) {
  20. o(c)
  21. }
  22. // Configuration httptest configuration
  23. type Configuration struct {
  24. // URL the base url.
  25. // Defaults to empty string "".
  26. URL string
  27. // Debug if true then debug messages from the httpexpect will be shown when a test runs
  28. // Defaults to false.
  29. Debug bool
  30. // LogLevel sets the application's log level.
  31. // Defaults to "disable" when testing.
  32. LogLevel string
  33. }
  34. // Set implements the OptionSetter for the Configuration itself
  35. func (c Configuration) Set(main *Configuration) {
  36. main.URL = c.URL
  37. main.Debug = c.Debug
  38. if c.LogLevel != "" {
  39. main.LogLevel = c.LogLevel
  40. }
  41. }
  42. var (
  43. // URL if setted then it sets the httptest's BaseURL.
  44. // Defaults to empty string "".
  45. URL = func(schemeAndHost string) OptionSet {
  46. return func(c *Configuration) {
  47. c.URL = schemeAndHost
  48. }
  49. }
  50. // Debug if true then debug messages from the httpexpect will be shown when a test runs
  51. // Defaults to false.
  52. Debug = func(val bool) OptionSet {
  53. return func(c *Configuration) {
  54. c.Debug = val
  55. }
  56. }
  57. // LogLevel sets the application's log level "val".
  58. // Defaults to disabled when testing.
  59. LogLevel = func(val string) OptionSet {
  60. return func(c *Configuration) {
  61. c.LogLevel = val
  62. }
  63. }
  64. )
  65. // DefaultConfiguration returns the default configuration for the httptest.
  66. func DefaultConfiguration() *Configuration {
  67. return &Configuration{URL: "", Debug: false, LogLevel: "disable"}
  68. }
  69. // New Prepares and returns a new test framework based on the "app".
  70. // You can find example on the https://github.com/kataras/iris/tree/master/_examples/testing/httptest
  71. func New(t *testing.T, app *iris.Application, setters ...OptionSetter) *httpexpect.Expect {
  72. conf := DefaultConfiguration()
  73. for _, setter := range setters {
  74. setter.Set(conf)
  75. }
  76. // set the logger or disable it (default) and disable the updater (for any case).
  77. app.Configure(iris.WithoutVersionChecker)
  78. app.Logger().SetLevel(conf.LogLevel)
  79. if err := app.Build(); err != nil {
  80. if conf.Debug && (conf.LogLevel == "disable" || conf.LogLevel == "disabled") {
  81. app.Logger().Println(err.Error())
  82. return nil
  83. }
  84. }
  85. testConfiguration := httpexpect.Config{
  86. BaseURL: conf.URL,
  87. Client: &http.Client{
  88. Transport: httpexpect.NewBinder(app),
  89. Jar: httpexpect.NewJar(),
  90. },
  91. Reporter: httpexpect.NewAssertReporter(t),
  92. }
  93. if conf.Debug {
  94. testConfiguration.Printers = []httpexpect.Printer{
  95. httpexpect.NewDebugPrinter(t, true),
  96. }
  97. }
  98. return httpexpect.WithConfig(testConfiguration)
  99. }
  100. // NewInsecure same as New but receives a single host instead of the whole framework.
  101. // Useful for testing running TLS servers.
  102. func NewInsecure(t *testing.T, setters ...OptionSetter) *httpexpect.Expect {
  103. conf := DefaultConfiguration()
  104. for _, setter := range setters {
  105. setter.Set(conf)
  106. }
  107. transport := &http.Transport{
  108. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  109. }
  110. testConfiguration := httpexpect.Config{
  111. BaseURL: conf.URL,
  112. Client: &http.Client{
  113. Transport: transport,
  114. Jar: httpexpect.NewJar(),
  115. },
  116. Reporter: httpexpect.NewAssertReporter(t),
  117. }
  118. if conf.Debug {
  119. testConfiguration.Printers = []httpexpect.Printer{
  120. httpexpect.NewDebugPrinter(t, true),
  121. }
  122. }
  123. return httpexpect.WithConfig(testConfiguration)
  124. }