Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MockHandleClient ¶
func MockHandleClient(fn RoundTripFunc) *http.Client
Mock http.Client by HandleFunction
returns *http.Client with Transport replaced with handle mechanism that passed as function parameter.
Example ¶
requiredUrl := "http://some-path" expectedRes := `whatever` mockClient := http_mock.MockHandleClient(func(req *http.Request) *http.Response { // Do stub with request object if req.URL.String() == requiredUrl { fmt.Println(req.URL.String()) } return &http.Response{ // Mocked status code StatusCode: 200, // Mocked response for testing Body: ioutil.NopCloser(bytes.NewBufferString(expectedRes)), // Mocked header // Header: make(http.Header), } }) doStubObj := doStubObject{client: mockClient} res, _ := doStubObj.RequestHttp(requiredUrl) fmt.Println(res)
Output: http://some-path whatever
func MockResponseClient ¶
Mock http.Client by ExpectedResponse
returns *http.Client with Transport replaced and always returns defined http.Response as a response from http call.
Example ¶
expectedResBody := `whatever` expectedRes := http.Response{ StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString(expectedResBody)), } mockClient := http_mock.MockResponseClient(&expectedRes) doStubObj := doStubObject{client: mockClient} res, _ := doStubObj.RequestHttp("http://....") fmt.Println(res)
Output: whatever
Types ¶
type RoundTripFunc ¶
An implement of Transport interface (from net/http Client)
Transport specifies the mechanism by which individual HTTP requests are made.
Click to show internal directories.
Click to hide internal directories.