📖 11 min read (~ 2200 words).

Http

Asserting HTTP Response And Body

Assertions

GoDoc

All links point to https://pkg.go.dev/github.com/go-openapi/testify/v2

This domain exposes 7 functionalities.

HTTPBodyContains

HTTPBodyContains asserts that a specified handler returns a body that contains a string.

Returns whether the assertion was successful (true) or not (false).

Examples
	assertions.HTTPBodyContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
	success: httpBody, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"
	failure: httpBody, "GET", "/", url.Values{"name": []string{"Bob"}}, "Hello, World!"
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestHTTPBodyContains(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"net/http"
    	"net/url"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestHTTPBodyContains(t *testing.T)
    	success := assert.HTTPBodyContains(t, httpBody, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!")
    	fmt.Printf("success: %t\n", success)
    
    }
    
    func httpBody(w http.ResponseWriter, r *http.Request) {
    	name := r.FormValue("name")
    	_, _ = fmt.Fprintf(w, "Hello, %s!", name)
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestHTTPBodyContains(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"net/http"
    	"net/url"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestHTTPBodyContains(t *testing.T)
    	require.HTTPBodyContains(t, httpBody, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!")
    	fmt.Println("passed")
    
    }
    
    func httpBody(w http.ResponseWriter, r *http.Request) {
    	name := r.FormValue("name")
    	_, _ = fmt.Fprintf(w, "Hello, %s!", name)
    }

HTTPBodyNotContains

HTTPBodyNotContains asserts that a specified handler returns a body that does not contain a string.

Returns whether the assertion was successful (true) or not (false).

Examples
	assertions.HTTPBodyNotContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
	success: httpBody, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, Bob!"
	failure: httpBody, "GET", "/", url.Values{"name": []string{"Bob"}}, "Hello, Bob!"
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestHTTPBodyNotContains(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"net/http"
    	"net/url"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestHTTPBodyNotContains(t *testing.T)
    	success := assert.HTTPBodyNotContains(t, httpBody, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, Bob!")
    	fmt.Printf("success: %t\n", success)
    
    }
    
    func httpBody(w http.ResponseWriter, r *http.Request) {
    	name := r.FormValue("name")
    	_, _ = fmt.Fprintf(w, "Hello, %s!", name)
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestHTTPBodyNotContains(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"net/http"
    	"net/url"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestHTTPBodyNotContains(t *testing.T)
    	require.HTTPBodyNotContains(t, httpBody, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, Bob!")
    	fmt.Println("passed")
    
    }
    
    func httpBody(w http.ResponseWriter, r *http.Request) {
    	name := r.FormValue("name")
    	_, _ = fmt.Fprintf(w, "Hello, %s!", name)
    }

HTTPError

HTTPError asserts that a specified handler returns an error status code.

Returns whether the assertion was successful (true) or not (false).

Examples
	assertions.HTTPError(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}
	success: httpError, "GET", "/", nil
	failure: httpOK, "GET", "/", nil
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestHTTPError(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"net/http"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestHTTPError(t *testing.T)
    	success := assert.HTTPError(t, httpError, "GET", "/", nil)
    	fmt.Printf("success: %t\n", success)
    
    }
    
    func httpError(w http.ResponseWriter, _ *http.Request) {
    	w.WriteHeader(http.StatusInternalServerError)
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestHTTPError(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"net/http"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestHTTPError(t *testing.T)
    	require.HTTPError(t, httpError, "GET", "/", nil)
    	fmt.Println("passed")
    
    }
    
    func httpError(w http.ResponseWriter, _ *http.Request) {
    	w.WriteHeader(http.StatusInternalServerError)
    }

HTTPRedirect

HTTPRedirect asserts that a specified handler returns a redirect status code.

Returns whether the assertion was successful (true) or not (false).

Examples
	assertions.HTTPRedirect(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}
	success: httpRedirect, "GET", "/", nil
	failure: httpError, "GET", "/", nil
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestHTTPRedirect(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"net/http"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestHTTPRedirect(t *testing.T)
    	success := assert.HTTPRedirect(t, httpRedirect, "GET", "/", nil)
    	fmt.Printf("success: %t\n", success)
    
    }
    
    func httpRedirect(w http.ResponseWriter, _ *http.Request) {
    	w.WriteHeader(http.StatusTemporaryRedirect)
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestHTTPRedirect(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"net/http"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestHTTPRedirect(t *testing.T)
    	require.HTTPRedirect(t, httpRedirect, "GET", "/", nil)
    	fmt.Println("passed")
    
    }
    
    func httpRedirect(w http.ResponseWriter, _ *http.Request) {
    	w.WriteHeader(http.StatusTemporaryRedirect)
    }

HTTPStatusCode

HTTPStatusCode asserts that a specified handler returns a specified status code.

Returns whether the assertion was successful (true) or not (false).

Examples
	assertions.HTTPStatusCode(t, myHandler, "GET", "/notImplemented", nil, 501)
	success: httpOK, "GET", "/", nil, http.StatusOK
	failure: httpError, "GET", "/", nil, http.StatusOK
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestHTTPStatusCode(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"net/http"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestHTTPStatusCode(t *testing.T)
    	success := assert.HTTPStatusCode(t, httpOK, "GET", "/", nil, http.StatusOK)
    	fmt.Printf("success: %t\n", success)
    
    }
    
    func httpOK(w http.ResponseWriter, _ *http.Request) {
    	w.WriteHeader(http.StatusOK)
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestHTTPStatusCode(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"net/http"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestHTTPStatusCode(t *testing.T)
    	require.HTTPStatusCode(t, httpOK, "GET", "/", nil, http.StatusOK)
    	fmt.Println("passed")
    
    }
    
    func httpOK(w http.ResponseWriter, _ *http.Request) {
    	w.WriteHeader(http.StatusOK)
    }

HTTPSuccess

HTTPSuccess asserts that a specified handler returns a success status code.

Returns whether the assertion was successful (true) or not (false).

Examples
	assertions.HTTPSuccess(t, myHandler, "POST", "http://www.google.com", nil)
	success: httpOK, "GET", "/", nil
	failure: httpError, "GET", "/", nil
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestHTTPSuccess(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"net/http"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestHTTPSuccess(t *testing.T)
    	success := assert.HTTPSuccess(t, httpOK, "GET", "/", nil)
    	fmt.Printf("success: %t\n", success)
    
    }
    
    func httpOK(w http.ResponseWriter, _ *http.Request) {
    	w.WriteHeader(http.StatusOK)
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestHTTPSuccess(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"net/http"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestHTTPSuccess(t *testing.T)
    	require.HTTPSuccess(t, httpOK, "GET", "/", nil)
    	fmt.Println("passed")
    
    }
    
    func httpOK(w http.ResponseWriter, _ *http.Request) {
    	w.WriteHeader(http.StatusOK)
    }

Other helpers

HTTPBody

HTTPBody is a helper that returns the HTTP body of the response. It returns the empty string if building a new request fails.


Generated with github.com/go-openapi/testify/codegen/v2