Documentation
¶
Index ¶
- Constants
- func ContentTypeIsJSON(s *testcase.Spec)
- func Debug(s *testcase.Spec)
- func HandlerSpec(s *testcase.Spec, subject func(t *testcase.T) http.Handler)
- func Header(t *testcase.T) http.Header
- func LetBody(s *testcase.Spec, f func(t *testcase.T) interface{})
- func LetContext(s *testcase.Spec, f func(t *testcase.T) context.Context)
- func LetMethod(s *testcase.Spec, f func(t *testcase.T) string)
- func LetMethodValue(s *testcase.Spec, m string)
- func LetPath(s *testcase.Spec, f func(t *testcase.T) string)
- func LetPathValue(s *testcase.Spec, p string)
- func Query(t *testcase.T) url.Values
- func ServeHTTP(t *testcase.T) *httptest.ResponseRecorder
Examples ¶
Constants ¶
const (
ContextVarName = letVarPrefix + `context`
)
Variables ¶
This section is empty.
Functions ¶
func ContentTypeIsJSON ¶ added in v0.13.0
func HandlerSpec ¶ added in v0.13.0
HandlerSpec prepares the current testcase spec scope to be ready for http handler testing.
You define your spec subject with this and all the request will be pointed towards this.
func Header ¶
Header allows you to set the current test scope's http path for ServeHTTP.
Example ¶
s := testcase.NewSpec(testingT) httpspec.HandlerSpec(s, func(t *testcase.T) http.Handler { return MyHandler{} }) s.Before(func(t *testcase.T) { // this is ideal to represent query string inputs httpspec.Header(t).Set(`Foo`, `bar`) }) s.Test(`the *http.Request URL Query will have 'Foo: bar'`, func(t *testcase.T) { httpspec.ServeHTTP(t) })
Output:
func LetBody ¶
LetBody allow you to define a http request body value for the ServeHTTP. The value of this can be a struct, map or url.Values. The Serialization for the request body is based on the Header "Content-Type" value. Currently only json and form url encoding is supported.
Example ¶
s := testcase.NewSpec(testingT) httpspec.HandlerSpec(s, func(t *testcase.T) http.Handler { return MyHandler{} }) httpspec.LetBody(s, func(t *testcase.T) interface{} { return map[string]string{"hello": "world"} }) s.Before(func(t *testcase.T) { // this set the content-type for json, so json marshal will be used. httpspec.Header(t).Set(`Content-Type`, `application/json`) }) s.Test(`the *http.Request body io.Reader will have the encoded body`, func(t *testcase.T) { httpspec.ServeHTTP(t) })
Output:
func LetContext ¶
LetContext allow you to Set the ServeHTTP request context
Example ¶
s := testcase.NewSpec(testingT) httpspec.HandlerSpec(s, func(t *testcase.T) http.Handler { return MyHandler{} }) httpspec.LetContext(s, func(t *testcase.T) context.Context { return context.Background() }) s.Test(`the *http.Request#Context() will have foo-bar`, func(t *testcase.T) { httpspec.ServeHTTP(t) })
Output:
Example (WithValue) ¶
s := testcase.NewSpec(testingT) httpspec.HandlerSpec(s, func(t *testcase.T) http.Handler { return MyHandler{} }) s.Before(func(t *testcase.T) { // this is ideal for representing middleware prerequisite // in the form of a value in the context that is guaranteed by a middleware. // Use this only if you cannot make it part of the specification level context value deceleration with LetContext. ctx := t.I(httpspec.ContextVarName).(context.Context) ctx = context.WithValue(ctx, `foo`, `bar`) t.Let(httpspec.ContextVarName, ctx) }) s.Test(`the *http.Request#Context() will have foo-bar`, func(t *testcase.T) { httpspec.ServeHTTP(t) })
Output:
func LetMethod ¶
LetMethod allow you to set the current test scope's http method for ServeHTTP
Example ¶
s := testcase.NewSpec(testingT) httpspec.HandlerSpec(s, func(t *testcase.T) http.Handler { return MyHandler{} }) httpspec.LetMethod(s, func(t *testcase.T) string { // set the HTTP Method to get for the *http.Request return http.MethodGet }) s.Test(`GET /`, func(t *testcase.T) { httpspec.ServeHTTP(t) })
Output:
func LetMethodValue ¶
LetMethodValue allow you to set the current test scope's http method for ServeHTTP
Example ¶
s := testcase.NewSpec(testingT) httpspec.HandlerSpec(s, func(t *testcase.T) http.Handler { return MyHandler{} }) // set the HTTP Method to get for the *http.Request httpspec.LetMethodValue(s, http.MethodGet) s.Test(`GET /`, func(t *testcase.T) { httpspec.ServeHTTP(t) })
Output:
func LetPathValue ¶
LetPathValue allows you to set the current test scope's http path for ServeHTTP.
func Query ¶
Query allows you to retrieve the current test scope's http path query that will be used for ServeHTTP. In a Before Block you can access the query and then specify the values in it.
Example ¶
s := testcase.NewSpec(testingT) httpspec.HandlerSpec(s, func(t *testcase.T) http.Handler { return MyHandler{} }) s.Before(func(t *testcase.T) { // this is ideal to represent query string inputs httpspec.Query(t).Set(`foo`, `bar`) }) s.Test(`the *http.Request URL Query will have foo=bar`, func(t *testcase.T) { httpspec.ServeHTTP(t) })
Output:
func ServeHTTP ¶
func ServeHTTP(t *testcase.T) *httptest.ResponseRecorder
ServeHTTP will make a request to the spec context it requires the following spec variables
- method -> http method <string>
- path -> http path <string>
- query -> http query string <url.Values>
- body -> http payload <io.Reader|io.ReadCloser>
Types ¶
This section is empty.