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:

package main

import (

func newApp() *iris.Application {
	app := iris.New()

	opts := basicauth.Options{
		Allow: basicauth.AllowUsers(map[string]string{"myusername": "mypassword"}),

	authentication := basicauth.New(opts) // or just: basicauth.Default(map...)

	app.Get("/", func(ctx iris.Context) { ctx.Redirect("/admin") })

	// to party

	needAuth := app.Party("/admin", authentication)
		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()
	// third parameter it will be always true because the middleware
	// makes sure for that, otherwise this handler will not be executed.
	// OR:
	// ctx.User().GetUsername()
	// ctx.User().GetPassword()
	ctx.Writef("%s %s:%s", ctx.Path(), username, password)

func main() {
	app := newApp()

2. Now, create a main_test.go file and copy-paste the following.

package main

import (


func TestNewApp(t *testing.T) {
	app := newApp()
	e := httptest.New(t, app)

	// redirects to /admin without basic auth
	// without basic auth

	// with valid basic auth
	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")

	// with invalid basic auth
	e.GET("/admin/settings").WithBasicAuth("invalidusername", "invalidpassword").

func TestHandlerUsingNetHTTP(t *testing.T) {
	handler := func(ctx iris.Context) {
		ctx.WriteString("Hello, World!")

	// A shortcut for net/http/httptest.NewRecorder/NewRequest.
	w := httptest.NewRecorder()
	r := httptest.NewRequest("GET", "/", nil)

	httptest.Do(w, r, handler)
	if expected, got := "Hello, World!", w.Body.String(); expected != got {
		t.Fatalf("expected body: %s but got: %s", expected, got)

3. Open your command line and execute:

$ go test -v

Other example: cookies

package main

import (


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"

    // Test Set A Cookie.
    t1 := e.GET(fmt.Sprintf("/cookies/%s/%s", cookieName, cookieValue)).
    // Validate cookie's existence, it should be available now.

    path := fmt.Sprintf("/cookies/%s", cookieName)

    // Test Retrieve A Cookie.
    t2 := e.GET(path).Expect().Status(httptest.StatusOK)

    // Test Remove A Cookie.
    t3 := e.DELETE(path).Expect().Status(httptest.StatusOK)

    t4 := e.GET(path).Expect().Status(httptest.StatusOK)
$ go test -v -run=TestCookiesBasic$

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 updated