ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
Iris 为 [httpexpect](https://github.com/gavv/httpexpect) 提供了令人难以置信的支持,它是一个Web应用程序测试框架。  `iris/httptest` 子包为 Iris + httpexpect 体统了辅助方法。 如果你喜欢 Go 的标准 [net/http/httptest](https://golang.org/pkg/net/http/httptest/) 包,你仍然可以使用它。Iris 作为一个 (原文: much every) http web 框架,它与任何外部测试工具兼容, (原文:at the end it's HTTP)。 ## [基本身份验证](https://github.com/kataras/iris/wiki/Testing#basic-authentication) 在我们的第一个示例中,我们将使用 `iris/httptest` 来测试基本身份验证。 **1.**  `main.go` 源文件如下所示: ```source-go package main import ( "github.com/kataras/iris/v12" "github.com/kataras/iris/v12/middleware/basicauth" ) func newApp() *iris.Application { app := iris.New() authConfig := basicauth.Config{ Users: map[string]string{"myusername": "mypassword"}, } authentication := basicauth.New(authConfig) app.Get("/", func(ctx iris.Context) { ctx.Redirect("/admin") }) needAuth := app.Party("/admin", authentication) { //http://localhost:8080/admin needAuth.Get("/", h) // http://localhost:8080/admin/profile needAuth.Get("/profile", h) // http://localhost:8080/admin/settings needAuth.Get("/settings", h) } return app } func h(ctx iris.Context) { username, password, _ := ctx.Request().BasicAuth() // 第三个参数 ^ 始终为 true,因为中间件 // 确保执行此操作,否则将不执行此处理程序。 ctx.Writef("%s %s:%s", ctx.Path(), username, password) } func main() { app := newApp() app.Run(iris.Addr(":8080")) } ``` **2.** 现在, 创建一个 `main_test.go` 文件并复制并粘贴以下内容。 ```source-go package main import ( "testing" "github.com/kataras/iris/v12/httptest" ) func TestNewApp(t *testing.T) { app := newApp() e := httptest.New(t, app) // 无需基本身份验证即可重定向到 /admin e.GET("/").Expect().Status(httptest.StatusUnauthorized) // 没有基本身份验证 e.GET("/admin").Expect().Status(httptest.StatusUnauthorized) // 具有有效的基本身份验证 e.GET("/admin").WithBasicAuth("myusername", "mypassword").Expect(). Status(httptest.StatusOK).Body().Equal("/admin myusername:mypassword") e.GET("/admin/profile").WithBasicAuth("myusername", "mypassword").Expect(). Status(httptest.StatusOK).Body().Equal("/admin/profile myusername:mypassword") e.GET("/admin/settings").WithBasicAuth("myusername", "mypassword").Expect(). Status(httptest.StatusOK).Body().Equal("/admin/settings myusername:mypassword") // 具有无效的基本身份验证 e.GET("/admin/settings").WithBasicAuth("invalidusername", "invalidpassword"). Expect().Status(httptest.StatusUnauthorized) } ``` **3.** 打开你的命令行并执行: ```source-shell $ go test -v ``` ## [其他示例: cookies](https://github.com/kataras/iris/wiki/Testing#other-example-cookies) ```source-go package main import ( "fmt" "testing" "github.com/kataras/iris/v12/httptest" ) func TestCookiesBasic(t *testing.T) { app := newApp() e := httptest.New(t, app, httptest.URL("http://example.com")) cookieName, cookieValue := "my_cookie_name", "my_cookie_value" // 测试设置 Cookie。 t1 := e.GET(fmt.Sprintf("/cookies/%s/%s", cookieName, cookieValue)). Expect().Status(httptest.StatusOK) // 验证 cookie 的存在,它现在应该可用。 t1.Cookie(cookieName).Value().Equal(cookieValue) t1.Body().Contains(cookieValue) path := fmt.Sprintf("/cookies/%s", cookieName) // 测试检索 Cookie. t2 := e.GET(path).Expect().Status(httptest.StatusOK) t2.Body().Equal(cookieValue) // 测试删除 Cookie. t3 := e.DELETE(path).Expect().Status(httptest.StatusOK) t3.Body().Contains(cookieName) t4 := e.GET(path).Expect().Status(httptest.StatusOK) t4.Cookies().Empty() t4.Body().Empty() } ``` ```source-shell $ go test -v -run=TestCookiesBasic$ ``` Iris Web框架本身使用此程序包进行自我测试。在 [_examples repository directory](https://github.com/kataras/iris/tree/master/_examples) y中,你还可以找到一些有用的测试。有关更多信息,请查看并阅读 [httpexpect 的文档](https://github.com/gavv/httpexpect).