Documentation ¶
Index ¶
- type ExpectationBuilder
- func (eb *ExpectationBuilder) Method(method string) *ExpectationBuilder
- func (eb *ExpectationBuilder) Path(url string) *ExpectationBuilder
- func (eb *ExpectationBuilder) ReqBody(b interface{}) *ExpectationBuilder
- func (eb *ExpectationBuilder) ReqBodyContains(s string) *ExpectationBuilder
- func (eb *ExpectationBuilder) ReqHeader(k, v string) *ExpectationBuilder
- func (eb *ExpectationBuilder) ReqPathParam(k, v string) *ExpectationBuilder
- func (eb *ExpectationBuilder) ReqQueryParam(k, v string) *ExpectationBuilder
- func (eb *ExpectationBuilder) RespWithBody(b interface{}) *ExpectationBuilder
- func (eb *ExpectationBuilder) RespWithHeader(k, v string) *ExpectationBuilder
- func (eb *ExpectationBuilder) RespWithStatus(s int) *ExpectationBuilder
- func (eb *ExpectationBuilder) Times(t int) *ExpectationBuilder
- type TestServer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ExpectationBuilder ¶
type ExpectationBuilder struct {
// contains filtered or unexported fields
}
ExpectationBuilder help to build expectations for http request and response
func (*ExpectationBuilder) Method ¶
func (eb *ExpectationBuilder) Method(method string) *ExpectationBuilder
Method allows you to set method for Path
func (*ExpectationBuilder) Path ¶
func (eb *ExpectationBuilder) Path(url string) *ExpectationBuilder
Path allows you to set request path in terms of http.ServeMux Path is required
func (*ExpectationBuilder) ReqBody ¶
func (eb *ExpectationBuilder) ReqBody(b interface{}) *ExpectationBuilder
ReqBody allows you to set the request body to which the actual request body should correspond exactly. You can use 2 time of args:
- []byte type allow to set byte slice that will return to the response body
- An any object that can be marshall to json
func (*ExpectationBuilder) ReqBodyContains ¶
func (eb *ExpectationBuilder) ReqBodyContains(s string) *ExpectationBuilder
ReqBodyContains allows you to specify the string that should be found in the request body
func (*ExpectationBuilder) ReqHeader ¶
func (eb *ExpectationBuilder) ReqHeader(k, v string) *ExpectationBuilder
ReqHeader allows you to set request headers expectation in key value format it can be called multiple times.
func (*ExpectationBuilder) ReqPathParam ¶
func (eb *ExpectationBuilder) ReqPathParam(k, v string) *ExpectationBuilder
ReqPathParam allows you to specify the params of request path.
For example if you use builder like this:
expectation := EXPECT().Method("GET").Path("/api/v1/client/{client_id}")
then
expectation.ReqParam("client_id", "123") ts.Add(expectation)
allow you to check that the request path parameter client_id is 123
func (*ExpectationBuilder) ReqQueryParam ¶
func (eb *ExpectationBuilder) ReqQueryParam(k, v string) *ExpectationBuilder
ReqQueryParam allows you to specify the params of request query string or request body form data. POST and PUT body parameters take precedence over URL query string values.
func (*ExpectationBuilder) RespWithBody ¶
func (eb *ExpectationBuilder) RespWithBody(b interface{}) *ExpectationBuilder
RespWithBody allows you to set response body. You can use 2 time of args:
- []byte type allow to set byte slice that will return to the response body
- An any object that can be marshall to json
func (*ExpectationBuilder) RespWithHeader ¶
func (eb *ExpectationBuilder) RespWithHeader(k, v string) *ExpectationBuilder
RespWithHeader allows you to set response headers in key value format it can be called multiple times.
For example:
EXPECT().Method("Get").Path("/api"). RespWithHeader("x-request-id", "123"). RespWithHeader("Server" "Apache")
func (*ExpectationBuilder) RespWithStatus ¶
func (eb *ExpectationBuilder) RespWithStatus(s int) *ExpectationBuilder
RespWithStatus specify http response status
func (*ExpectationBuilder) Times ¶
func (eb *ExpectationBuilder) Times(t int) *ExpectationBuilder
Times allows you to set how many times endpoint should be called with specified parameters by default EXPECT is waiting for 1 call
type TestServer ¶
TestServer is a structure that contains a set of expectations and a server from the httptest package. You should use a new instance of the test server in each test. The algorithm for using the test server is as follows:
func TestFunc(t *testing.T) { // Create new instance ts := testserver.New(t) // Add some expectations ts.Add(EXPECT().Method("GET").Path("/api/profile").RespWithStatus(200).RespWithBody(profile)) ts.Add(EXPECT().Method("POST").Path("/api/order").RespWithStatus(200).RespWithBody(done).Times(2)) ... // Start test Server ts.Start() defer ts.Stop() // Create an instance of the object under test service := order.New(ts.URL) // Call the method under test gotRs, gotErr := service.CreateOrder(args...) // Check the results if wantRs != gotRs { t.Error() } if wantErr != gotErr { t.Error() } }
func (*TestServer) Add ¶
func (ts *TestServer) Add(eb *ExpectationBuilder)
Add new expectation on TestServer instance