📖 7 min read (~ 1300 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!"

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!"

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

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

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

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

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/v2/codegen