Documentation ¶
Index ¶
- Constants
- Variables
- func SetLoggerLevel(level string)
- type GentlemanConfig
- type GentlemanRequest
- func (r *GentlemanRequest) SendAndMatch(matcher RespMatcher) (*gt.Response, error)
- func (r *GentlemanRequest) SendAndMustMatch(matcher RespMatcher) *gt.Response
- func (r *GentlemanRequest) SendAndStatusMatch(status int) (*gt.Response, error)
- func (r *GentlemanRequest) SendAndStatusMustMatch(status int) *gt.Response
- type GentlemanResponse
- type HttpClientConfig
- type RespMatcher
- type SlintExt
Examples ¶
Constants ¶
const DEFAULT_TIMEOUT = 10 * time.Second
Default value of request time out for HTTP client.
Variables ¶
var CommonGentleman = &struct { NewDefaultClient func() *gt.Client NewClientByConfig func(config *GentlemanConfig) *gt.Client NewDefaultRequest func() *gt.Request NewRequestByConfig func(config *GentlemanConfig) *gt.Request }{ NewDefaultClient: gtNewDefaultClient, NewClientByConfig: gtNewClientByConfig, NewDefaultRequest: gtNewDefaultRequest, NewRequestByConfig: gtNewRequestByConfig, }
Utility functions for building "*gentleman.Client" or "*gentleman.Request" with default configuration.
Request Timeout: See "DEFAULT_TIMEOUT"
NewDefaultClient()
Constructs a client with default configuration.
NewClientByConfig(*GentlemanConfig)
Constructs a client with provided configuration.
NewDefaultRequest()
Constructs a request with default configuration.
NewRequestByConfig(*GentlemanConfig)
Constructs a request with provided configuration.
Functions ¶
func SetLoggerLevel ¶
func SetLoggerLevel(level string)
Types ¶
type GentlemanConfig ¶
Common configurations used for building of "*gentleman.Client" object.
type GentlemanRequest ¶
Wrapper object with providing additional functions to "*gentleman.Request"
Example (SendAndMatch) ¶
Send request and confirm by customized matcher.
defer gock.Off() /** * Mock service */ gock.New("http://example-2.gock/success"). Reply(http.StatusOK) // :~) req := newClientByGock(). URL("http://example-2.gock/"). Get(). AddPath("success") _, err := client.ToGentlemanReq(req).SendAndMatch( func(resp *gt.Response) error { if resp.StatusCode != http.StatusOK { return errors.New("Not Success") } return nil }, ) if err != nil { fmt.Printf("Err: %v\n", err) return } fmt.Println("Success")
Output: Success
Example (SendAndStatusMatch) ¶
Send request and confirm status code.
defer gock.Off() /** * Mock service */ gock.New("http://example-1.gock/success"). Reply(http.StatusOK) // :~) req := newClientByGock(). URL("http://example-1.gock/"). Get(). AddPath("success") _, err := client.ToGentlemanReq(req).SendAndStatusMatch(http.StatusOK) if err != nil { fmt.Printf("Err: %v\n", err) return } fmt.Println("Success")
Output: Success
func ToGentlemanReq ¶
func ToGentlemanReq(req *gt.Request) *GentlemanRequest
Converts "*gentleman.Request" to "*GentlemanRequest"
func (*GentlemanRequest) SendAndMatch ¶
func (r *GentlemanRequest) SendAndMatch(matcher RespMatcher) (*gt.Response, error)
Sends request with checking by implementation of matcher.
If the status code is not as expected, the error would not be nil.
func (*GentlemanRequest) SendAndMustMatch ¶
func (r *GentlemanRequest) SendAndMustMatch(matcher RespMatcher) *gt.Response
Sends request with checking by implementation of matcher.
If the status code is not as expected, the function would be panic.
func (*GentlemanRequest) SendAndStatusMatch ¶
func (r *GentlemanRequest) SendAndStatusMatch(status int) (*gt.Response, error)
Sends request with expected status code
If the status code is not as expected, the error would not be nil.
func (*GentlemanRequest) SendAndStatusMustMatch ¶
func (r *GentlemanRequest) SendAndStatusMustMatch(status int) *gt.Response
Sends request with expected status code
If the status code is not as expected, the function would be panic.
type GentlemanResponse ¶
Wrapper object with providing additional functions to "*gentleman.Response"
Example (MustBindJson) ¶
Binds JSON object from response object(panic if some error has occurred)
defer gock.Off() /** * Mock service */ gock.New("http://example-4.gock/json-2"). Reply(http.StatusOK). JSON(map[string]interface{}{ "name": "Jon Snow", "age": 18, }) // :~) req := newClientByGock(). URL("http://example-4.gock/"). Get(). AddPath("json-2") jsonBody := &struct { Name string `json:"name"` Age int `json:"age"` }{} resp := client.ToGentlemanReq(req).SendAndStatusMustMatch(http.StatusOK) client.ToGentlemanResp(resp).MustBindJson(&jsonBody) fmt.Printf("%s %d", jsonBody.Name, jsonBody.Age)
Output: Jon Snow 18
Example (MustGetJson) ¶
Gets JSON object from response object(panic if some error has occurred)
defer gock.Off() /** * Mock service */ gock.New("http://example-3.gock/json-1"). Reply(http.StatusOK). JSON(map[string]interface{}{ "name": "King", "age": 33, }) // :~) req := newClientByGock(). URL("http://example-3.gock/"). Get(). AddPath("json-1") resp := client.ToGentlemanReq(req).SendAndStatusMustMatch(http.StatusOK) json := client.ToGentlemanResp(resp).MustGetJson() fmt.Printf("%s %d", json.Get("name").MustString(), json.Get("age").MustInt())
Output: King 33
func ToGentlemanResp ¶
func ToGentlemanResp(resp *gt.Response) *GentlemanResponse
Converts "*gentleman.Response" to wrapper object of utility.
func (*GentlemanResponse) BindJson ¶
func (r *GentlemanResponse) BindJson(v interface{}) error
Binds the body of response to input object(as JSON).
Be careful: This function would **close the response** because of the implementation of Gentleman library.
func (*GentlemanResponse) GetJson ¶
func (r *GentlemanResponse) GetJson() (*sjson.Json, error)
Gets the json object(by go-simplejson) with error.
Be careful: This function would **close the response** because of the implementation of Gentleman library.
func (*GentlemanResponse) MustBindJson ¶
func (r *GentlemanResponse) MustBindJson(v interface{})
Binds the body of response to input object(as JSON).
This function would panic if some error has occurred.
Be careful: This function would **close the response** because of the implementation of Gentleman library.
func (*GentlemanResponse) MustGetJson ¶
func (r *GentlemanResponse) MustGetJson() *sjson.Json
Gets the json object(by go-simplejson) or panic if some error has occurred.
Be careful: This function would **close the response** because of the implementation of Gentleman library.
func (*GentlemanResponse) ToDetailString ¶
func (r *GentlemanResponse) ToDetailString() string
Gets the string content of body.
The format of returned value:
If the body is empty, gives:
Response[<StatusCode>(<ContentType>)]
If the body is viable, gives:
Response[<StatusCode>](ContentType). Body: << <Shortened Body> >>
The shortened body would be 128 characters of maximum by preserving prefix and suffix string of body.
Be careful: This function would **close the response** because of the implementation of Gentleman library.
type HttpClientConfig ¶
func NewDefaultConfig ¶
func NewDefaultConfig() *HttpClientConfig
Constructs default configuration by pre-defined values.
type RespMatcher ¶
This type is used with "*GentlemanRequest", which gets called for checking the expected response.