Documentation ¶
Index ¶
- Constants
- func GetJSON(respData interface{}, url string, params interface{}, headers ...*DataMap) error
- func PostForm(respData interface{}, url string, form interface{}, headers ...*DataMap) error
- func PostJSON(respData interface{}, url string, data interface{}, headers ...*DataMap) error
- func RequestJSON(respData interface{}, method Method, url string, format requestDataFormat, ...) error
- func SetClient(client Client)
- type Client
- type DataMap
- type DefaultClient
- type DefaultMockClient
- type FastHTTPClient
- type GetMockFileNameFunc
- type Method
- type Option
- type Options
Constants ¶
const ( ClientNetHTTP = iota + 1 ClientFastHTTP )
client type
const ( NoData requestDataFormat = 0 QueryString requestDataFormat = 1 Form requestDataFormat = 2 JSON requestDataFormat = 3 Raw requestDataFormat = 4 )
request data formats
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client interface { // RequestJSON supports different request format with JSON Response RequestJSON(respData interface{}, method Method, url string, format requestDataFormat, data interface{}, headers ...*DataMap) (err error) // GetRawClient returns the internal http client GetRawClient() interface{} }
Client is the http client standard interface.
func NewDefaultClient ¶
func NewDefaultClient() Client
NewDefaultClient initializes a new DefaultClient
func NewDefaultHTTPMockClient ¶
func NewDefaultHTTPMockClient(getFileFunc GetMockFileNameFunc) Client
NewDefaultHTTPMockClient creates a http client for testing
type DefaultClient ¶
type DefaultClient struct {
// contains filtered or unexported fields
}
DefaultClient is the client to handle the various HTTP request, with net/http.Client as internal client
func (*DefaultClient) GetRawClient ¶
func (c *DefaultClient) GetRawClient() interface{}
GetRawClient gets the internal client
func (*DefaultClient) RequestJSON ¶
func (c *DefaultClient) RequestJSON(respData interface{}, method Method, url string, format requestDataFormat, data interface{}, headers ...*DataMap) (err error)
RequestJSON Send http request to query JSON data `data` can be `*url.Values`, `*DataMap`, or struct, for any requestDataFormat
Note: 1. if pass `url.Values` or `DataMap` as data, should pass the pointer 2. For struct data, can specify the field name by `form` tag. E.g. struct { Name string `form:"name"` }
Examples:
Response struct
``` type ApiResponse struct { Error string `json:"error"` } resp := ApiResponse{} ```
Pass *url.Values as parameters
``` v := url.Values{} v.Set("name", "abc") v.Add("values", "1") v.Add("values", "2") http.HTTPRequestJSON(resp, "GET", "http://localhost/api", http.QueryString, &v) ```
The request will be:
``` GET /api?name=abc&values=1&values=2 HTTP/1.1 Host: localhost ``` 2. Pass DataMap as parameters ``` http.HTTPRequestJSON(resp, "POST", "http://localhost/api", http.Form, &http.DataMap{ "name": "abc", "values": []int{1, 2}, }) ```
The request will be:
``` POST /api HTTP/1.1 Host: localhost Content-Type: application/x-www-form-urlencoded name=abc&values=1&values=2 ``` 3. Pass Struct as parameters ``` type ApiRequest struct { Name string `json:"name" form:"name"` Values []int `json:"values" form:"values"` } http.HTTPRequestJSON(resp, "POST", "http://localhost/api", http.JSON, &ApiRequest{ Name: "abc", Values: []int{1, 2}, }) ```
The request will be:
``` POST /api HTTP/1.1 Host: localhost Content-Type: application/json {"name":"abc","values":[1,2]} ```
type DefaultMockClient ¶
type DefaultMockClient struct {
// contains filtered or unexported fields
}
DefaultMockClient read json content from mock file and unmarshal it to response.
func (DefaultMockClient) GetRawClient ¶
func (hcm DefaultMockClient) GetRawClient() interface{}
GetRawClient gets the internal client
func (DefaultMockClient) RequestJSON ¶
func (hcm DefaultMockClient) RequestJSON(respData interface{}, method Method, url string, format requestDataFormat, data interface{}, headers ...*DataMap) (err error)
RequestJSON mocks RequestJSON in http but returns mock data
type FastHTTPClient ¶
type FastHTTPClient struct {
// contains filtered or unexported fields
}
FastHTTPClient is the client to handle the various HTTP request, with net/fasthttp.Client as internal client
func (*FastHTTPClient) GetRawClient ¶
func (c *FastHTTPClient) GetRawClient() interface{}
GetRawClient gets the internal client
func (*FastHTTPClient) RequestJSON ¶
func (c *FastHTTPClient) RequestJSON(respData interface{}, method Method, url string, format requestDataFormat, data interface{}, headers ...*DataMap) (err error)
RequestJSON Send http request to query JSON data
type GetMockFileNameFunc ¶
GetMockFileNameFunc returns a file name, of which contains mock data for test
type Option ¶
type Option func(*Options)
Option is modifier to update Options
func RawClientType ¶
RawClientType sets `Options.ClientType`