Testing
Iris offers an incredible support for the httpexpect, a Testing Framework for web applications. The iris/httptest subpackage provides helpers for Iris + httpexpect.
if you prefer the Go's standard net/http/httptest package, you can still use it. Iris as much every http web framework is compatible with any external tool for testing, at the end it's HTTP.

Basic Authentication

In our first example we will use the iris/httptest to test Basic Authentication.
1. The main.go source file looks like that:
1
package main
2
3
import (
4
"github.com/kataras/iris/v12"
5
"github.com/kataras/iris/v12/middleware/basicauth"
6
)
7
8
func newApp() *iris.Application {
9
app := iris.New()
10
11
opts := basicauth.Options{
12
Allow: basicauth.AllowUsers(map[string]string{"myusername": "mypassword"}),
13
}
14
15
authentication := basicauth.New(opts) // or just: basicauth.Default(map...)
16
17
app.Get("/", func(ctx iris.Context) { ctx.Redirect("/admin") })
18
19
// to party
20
21
needAuth := app.Party("/admin", authentication)
22
{
23
//http://localhost:8080/admin
24
needAuth.Get("/", h)
25
// http://localhost:8080/admin/profile
26
needAuth.Get("/profile", h)
27
28
// http://localhost:8080/admin/settings
29
needAuth.Get("/settings", h)
30
}
31
32
return app
33
}
34
35
func h(ctx iris.Context) {
36
username, password, _ := ctx.Request().BasicAuth()
37
// third parameter it will be always true because the middleware
38
// makes sure for that, otherwise this handler will not be executed.
39
// OR:
40
// ctx.User().GetUsername()
41
// ctx.User().GetPassword()
42
ctx.Writef("%s %s:%s", ctx.Path(), username, password)
43
}
44
45
func main() {
46
app := newApp()
47
app.Listen(":8080")
48
}
Copied!
2. Now, create a main_test.go file and copy-paste the following.
1
package main
2
3
import (
4
"testing"
5
6
"github.com/kataras/iris/v12"
7
"github.com/kataras/iris/v12/httptest"
8
)
9
10
func TestNewApp(t *testing.T) {
11
app := newApp()
12
e := httptest.New(t, app)
13
14
// redirects to /admin without basic auth
15
e.GET("/").Expect().Status(httptest.StatusUnauthorized)
16
// without basic auth
17
e.GET("/admin").Expect().Status(httptest.StatusUnauthorized)
18
19
// with valid basic auth
20
e.GET("/admin").WithBasicAuth("myusername", "mypassword").Expect().
21
Status(httptest.StatusOK).Body().Equal("/admin myusername:mypassword")
22
e.GET("/admin/profile").WithBasicAuth("myusername", "mypassword").Expect().
23
Status(httptest.StatusOK).Body().Equal("/admin/profile myusername:mypassword")
24
e.GET("/admin/settings").WithBasicAuth("myusername", "mypassword").Expect().
25
Status(httptest.StatusOK).Body().Equal("/admin/settings myusername:mypassword")
26
27
// with invalid basic auth
28
e.GET("/admin/settings").WithBasicAuth("invalidusername", "invalidpassword").
29
Expect().Status(httptest.StatusUnauthorized)
30
}
31
32
func TestHandlerUsingNetHTTP(t *testing.T) {
33
handler := func(ctx iris.Context) {
34
ctx.WriteString("Hello, World!")
35
}
36
37
// A shortcut for net/http/httptest.NewRecorder/NewRequest.
38
w := httptest.NewRecorder()
39
r := httptest.NewRequest("GET", "/", nil)
40
41
httptest.Do(w, r, handler)
42
if expected, got := "Hello, World!", w.Body.String(); expected != got {
43
t.Fatalf("expected body: %s but got: %s", expected, got)
44
}
45
}
Copied!
3. Open your command line and execute:
1
$ go test -v
Copied!

Other example: cookies

1
package main
2
3
import (
4
"fmt"
5
"testing"
6
7
"github.com/kataras/iris/v12/httptest"
8
)
9
10
func TestCookiesBasic(t *testing.T) {
11
app := newApp()
12
e := httptest.New(t, app, httptest.URL("http://example.com"))
13
14
cookieName, cookieValue := "my_cookie_name", "my_cookie_value"
15
16
// Test Set A Cookie.
17
t1 := e.GET(fmt.Sprintf("/cookies/%s/%s", cookieName, cookieValue)).
18
Expect().Status(httptest.StatusOK)
19
// Validate cookie's existence, it should be available now.
20
t1.Cookie(cookieName).Value().Equal(cookieValue)
21
t1.Body().Contains(cookieValue)
22
23
path := fmt.Sprintf("/cookies/%s", cookieName)
24
25
// Test Retrieve A Cookie.
26
t2 := e.GET(path).Expect().Status(httptest.StatusOK)
27
t2.Body().Equal(cookieValue)
28
29
// Test Remove A Cookie.
30
t3 := e.DELETE(path).Expect().Status(httptest.StatusOK)
31
t3.Body().Contains(cookieName)
32
33
t4 := e.GET(path).Expect().Status(httptest.StatusOK)
34
t4.Cookies().Empty()
35
t4.Body().Empty()
36
}
Copied!
1
$ go test -v -run=TestCookiesBasic$
Copied!
Iris web framework itself uses this package to test itself. In the _examples repository directory you will find some useful tests as well. For more information please take a look and read the httpexpect's documentation.
Last modified 1yr ago
Export as PDF
Copy link