Iris 为 httpexpect (一个web测试框架) 提供了丰富的支持。iris/httptest 子包为 iris + httpexpect 提供了帮助。

如果你更喜欢 golang 的 net/http/httptest 标准库,你也可以使用它。Iris 尽可能与其它外部测试工具兼容。


基本认证(Basic Authentication)

在第一个示例中,我们使用 iris/httptest 来测试权限验证。

  1. main.go 文件像这样:
  1. package main
  2. import (
  3. "github.com/kataras/iris/v12"
  4. "github.com/kataras/iris/v12/middleware/basicauth"
  5. )
  6. func newApp() *iris.Application {
  7. app := iris.New()
  8. authConfig := basicauth.Config{
  9. Users: map[string]string{"myusername": "mypassword"},
  10. }
  11. authentication := basicauth.New(authConfig)
  12. app.Get("/", func(ctx iris.Context) { ctx.Redirect("/admin") })
  13. needAuth := app.Party("/admin", authentication)
  14. {
  15. //http://localhost:8080/admin
  16. needAuth.Get("/", h)
  17. // http://localhost:8080/admin/profile
  18. needAuth.Get("/profile", h)
  19. // http://localhost:8080/admin/settings
  20. needAuth.Get("/settings", h)
  21. }
  22. return app
  23. }
  24. func h(ctx iris.Context) {
  25. username, password, _ := ctx.Request().BasicAuth()
  26. // third parameter ^ will be always true because the middleware
  27. // makes sure for that, otherwise this handler will not be executed.
  28. ctx.Writef("%s %s:%s", ctx.Path(), username, password)
  29. }
  30. func main() {
  31. app := newApp()
  32. app.Run(iris.Addr(":8080"))
  33. }
  1. 现在,创建一个 main_test.go 文件,然后复制下面的代码:
  1. package main
  2. import (
  3. "testing"
  4. "github.com/kataras/iris/v12/httptest"
  5. )
  6. func TestNewApp(t *testing.T) {
  7. app := newApp()
  8. e := httptest.New(t, app)
  9. // redirects to /admin without basic auth
  10. e.GET("/").Expect().Status(httptest.StatusUnauthorized)
  11. // without basic auth
  12. e.GET("/admin").Expect().Status(httptest.StatusUnauthorized)
  13. // with valid basic auth
  14. e.GET("/admin").WithBasicAuth("myusername", "mypassword").Expect().
  15. Status(httptest.StatusOK).Body().Equal("/admin myusername:mypassword")
  16. e.GET("/admin/profile").WithBasicAuth("myusername", "mypassword").Expect().
  17. Status(httptest.StatusOK).Body().Equal("/admin/profile myusername:mypassword")
  18. e.GET("/admin/settings").WithBasicAuth("myusername", "mypassword").Expect().
  19. Status(httptest.StatusOK).Body().Equal("/admin/settings myusername:mypassword")
  20. // with invalid basic auth
  21. e.GET("/admin/settings").WithBasicAuth("invalidusername", "invalidpassword").
  22. Expect().Status(httptest.StatusUnauthorized)
  23. }
  1. 打开你的命令行然后执行:
  1. $ go test -v

其他示例

  1. package main
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/kataras/iris/v12/httptest"
  6. )
  7. func TestCookiesBasic(t *testing.T) {
  8. app := newApp()
  9. e := httptest.New(t, app, httptest.URL("http://example.com"))
  10. cookieName, cookieValue := "my_cookie_name", "my_cookie_value"
  11. // Test Set A Cookie.
  12. t1 := e.GET(fmt.Sprintf("/cookies/%s/%s", cookieName, cookieValue)).
  13. Expect().Status(httptest.StatusOK)
  14. // Validate cookie's existence, it should be available now.
  15. t1.Cookie(cookieName).Value().Equal(cookieValue)
  16. t1.Body().Contains(cookieValue)
  17. path := fmt.Sprintf("/cookies/%s", cookieName)
  18. // Test Retrieve A Cookie.
  19. t2 := e.GET(path).Expect().Status(httptest.StatusOK)
  20. t2.Body().Equal(cookieValue)
  21. // Test Remove A Cookie.
  22. t3 := e.DELETE(path).Expect().Status(httptest.StatusOK)
  23. t3.Body().Contains(cookieName)
  24. t4 := e.GET(path).Expect().Status(httptest.StatusOK)
  25. t4.Cookies().Empty()
  26. t4.Body().Empty()
  27. }
  1. $ go test -v -run=TestCookiesBasic$