README
¶
httpspec
httpspec allow you to create HTTP API specifications with ease.
Documentation
The documentation maintained in GoDoc, including the examples.
Usage
package mypkg
func TestMyHandlerCreate(t *testing.T) {
s := testcase.NewSpec(t)
// subject
httpspec.SubjectLet(s, func(t *testcase.T) http.Handler {
return MyHandler{}
})
// Arrange
httpspec.ContentTypeIsJSON(s)
httpspec.Method.LetValue(s, http.MethodPost)
httpspec.Path.LetValue(s, `/`)
httpspec.Body.Let(s, func(t *testcase.T) interface{} {
// this will end up as {"foo":"bar"} in the request body
return map[string]string{"foo": "bar"}
})
s.Then(`it will...`, func(t *testcase.T) {
// Act
rr := httpspec.SubjectGet(t)
// Assert
require.Equal(t, http.StatusOK, rr.Code)
var resp CreateResponse
require.Nil(t, json.Unmarshal(rr.Body.Bytes(), &resp))
// ...
})
}
Documentation
¶
Overview ¶
Example (Usage) ¶
Output:
Example (UsageWithDotImport) ¶
Output:
Index ¶
- Variables
- func ContentTypeIsJSON(s *testcase.Spec)
- func ContextGet(t *testcase.T) context.Context
- func Debug(s *testcase.Spec)
- func HandlerLet(s *testcase.Spec, subject func(t *testcase.T) http.Handler)
- func HeaderGet(t *testcase.T) http.Header
- func QueryGet(t *testcase.T) url.Values
- func ServeHTTP(t *testcase.T) *httptest.ResponseRecorder
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( Handler = testcase.Var{Name: `httpspec:Handler`} Context = testcase.Var{Name: `httpspec:Context`, Init: func(t *testcase.T) interface{} { return context.Background() }} Method = testcase.Var{Name: `httpspec:Method`, Init: func(t *testcase.T) interface{} { return http.MethodGet }} Path = testcase.Var{Name: `httpspec:Path`, Init: func(t *testcase.T) interface{} { return `/` }} Body = testcase.Var{Name: `httpspec:Body`, Init: func(t *testcase.T) interface{} { return &bytes.Buffer{} }} Query = testcase.Var{Name: `httpspec:QueryGet`, Init: func(t *testcase.T) interface{} { return url.Values{} }} Header = testcase.Var{Name: `httpspec:HeaderGet`, Init: func(t *testcase.T) interface{} { return http.Header{} }} )
Functions ¶
func ContentTypeIsJSON ¶ added in v0.13.0
Example ¶
Output:
func ContextGet ¶ added in v0.27.0
ContextGet allow to retrieve the current test scope's request context.
func HandlerLet ¶ added in v0.33.0
HandlerLet 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 HeaderGet ¶ added in v0.27.0
HeaderGet allows you to set the current test scope's http PathGet for ServeHTTP.
func QueryGet ¶ added in v0.27.0
QueryGet allows you to retrieve the current test scope's http PathGet query that will be used for ServeHTTP. In a Before Block you can access the query and then specify the values in it.
func ServeHTTP ¶
func ServeHTTP(t *testcase.T) *httptest.ResponseRecorder
ServeHTTP will make a request to the spec context it requires the following spec variables
- MethodGet -> http MethodGet <string>
- PathGet -> http PathGet <string>
- query -> http query string <url.Values>
- body -> http payload <io.Reader|io.ReadCloser>
Types ¶
This section is empty.