Documentation
¶
Overview ¶
Package gofight offers simple API http handler testing for Golang framework.
Details about the gofight project are found in github page:
https://github.com/appleboy/gofight
Installation:
$ go get -u github.com/appleboy/gofight
Set Header: You can add custom header via SetHeader func.
SetHeader(gofight.H{
"X-Version": version,
})
Set Cookie: You can add custom cookie via SetCookie func.
SetCookie(gofight.H{
"foo": "bar",
})
Set query string: Using SetQuery to generate query string data.
SetQuery(gofight.H{
"a": "1",
"b": "2",
})
POST FORM Data: Using SetForm to generate form data.
SetForm(gofight.H{
"a": "1",
"b": "2",
})
POST JSON Data: Using SetJSON to generate json data.
SetJSON(gofight.H{
"a": "1",
"b": "2",
})
POST RAW Data: Using SetBody to generate raw data.
SetBody("a=1&b=1")
For more details, see the documentation and example.
Index ¶
- Constants
- type D
- type H
- type HTTPRequest
- type HTTPResponse
- type RequestConfig
- func (rc *RequestConfig) DELETE(path string) *RequestConfig
- func (rc *RequestConfig) GET(path string) *RequestConfig
- func (rc *RequestConfig) HEAD(path string) *RequestConfig
- func (rc *RequestConfig) OPTIONS(path string) *RequestConfig
- func (rc *RequestConfig) PATCH(path string) *RequestConfig
- func (rc *RequestConfig) POST(path string) *RequestConfig
- func (rc *RequestConfig) PUT(path string) *RequestConfig
- func (rc *RequestConfig) Run(r http.Handler, response ResponseFunc)
- func (rc *RequestConfig) SetBody(body string) *RequestConfig
- func (rc *RequestConfig) SetContext(ctx context.Context) *RequestConfig
- func (rc *RequestConfig) SetCookie(cookies H) *RequestConfig
- func (rc *RequestConfig) SetDebug(enable bool) *RequestConfig
- func (rc *RequestConfig) SetFileFromPath(uploads []UploadFile, params ...H) *RequestConfig
- func (rc *RequestConfig) SetForm(body H) *RequestConfig
- func (rc *RequestConfig) SetHeader(headers H) *RequestConfig
- func (rc *RequestConfig) SetJSON(body D) *RequestConfig
- func (rc *RequestConfig) SetJSONInterface(body any) *RequestConfig
- func (rc *RequestConfig) SetPath(str string) *RequestConfig
- func (rc *RequestConfig) SetQuery(query H) *RequestConfig
- func (rc *RequestConfig) SetQueryD(query D) *RequestConfig
- type ResponseFunc
- type UploadFile
Constants ¶
const ( Version = "1.0" UserAgent = "User-Agent" ContentType = "Content-Type" ApplicationJSON = "application/json" ApplicationForm = "application/x-www-form-urlencoded" )
Media types
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HTTPRequest ¶
HTTPRequest is a wrapper around the standard http.Request. It embeds the http.Request struct, allowing you to use all the methods and fields of http.Request while also providing the ability to extend its functionality with additional methods or fields if needed.
type HTTPResponse ¶
type HTTPResponse struct {
*httptest.ResponseRecorder
}
HTTPResponse wraps the httptest.ResponseRecorder to provide additional functionality or to simplify the response handling in tests.
type RequestConfig ¶
type RequestConfig struct {
Method string
Path string
Body string
Headers H
Cookies H
Debug bool
ContentType string
Context context.Context
}
RequestConfig provide user input request structure
func (*RequestConfig) DELETE ¶
func (rc *RequestConfig) DELETE(path string) *RequestConfig
DELETE is request method.
func (*RequestConfig) GET ¶
func (rc *RequestConfig) GET(path string) *RequestConfig
GET is request method.
func (*RequestConfig) HEAD ¶
func (rc *RequestConfig) HEAD(path string) *RequestConfig
HEAD is request method.
func (*RequestConfig) OPTIONS ¶
func (rc *RequestConfig) OPTIONS(path string) *RequestConfig
OPTIONS is request method.
func (*RequestConfig) PATCH ¶
func (rc *RequestConfig) PATCH(path string) *RequestConfig
PATCH is request method.
func (*RequestConfig) POST ¶
func (rc *RequestConfig) POST(path string) *RequestConfig
POST is request method.
func (*RequestConfig) PUT ¶
func (rc *RequestConfig) PUT(path string) *RequestConfig
PUT is request method.
func (*RequestConfig) Run ¶
func (rc *RequestConfig) Run(r http.Handler, response ResponseFunc)
Run executes the HTTP request using the provided http.Handler and processes the response using the given ResponseFunc. It initializes the test request and response writer, serves the HTTP request, and then passes the HTTP response and request to the response function.
Parameters:
- r: The http.Handler that will handle the HTTP request.
- response: A function that processes the HTTP response and request.
func (*RequestConfig) SetBody ¶
func (rc *RequestConfig) SetBody(body string) *RequestConfig
SetBody sets the body of the request if the provided body string is not empty. It returns the updated RequestConfig instance.
Parameters:
- body: A string representing the body content to be set.
Returns:
- *RequestConfig: The updated RequestConfig instance.
func (*RequestConfig) SetContext ¶ added in v2.2.0
func (rc *RequestConfig) SetContext(ctx context.Context) *RequestConfig
SetContext sets the context for the RequestConfig. This allows the request to be aware of deadlines, cancellation signals, and other request-scoped values. It returns the updated RequestConfig instance.
Parameters:
ctx - the context to be set for the RequestConfig
Returns:
*RequestConfig - the updated RequestConfig instance with the new context
func (*RequestConfig) SetCookie ¶
func (rc *RequestConfig) SetCookie(cookies H) *RequestConfig
SetCookie sets the cookies for the request configuration. It takes a map of cookies and assigns it to the Cookies field of the RequestConfig if the provided map is not empty.
Parameters:
- cookies: A map of cookies to be set.
Returns:
- A pointer to the updated RequestConfig.
func (*RequestConfig) SetDebug ¶
func (rc *RequestConfig) SetDebug(enable bool) *RequestConfig
SetDebug supply enable debug mode.
func (*RequestConfig) SetFileFromPath ¶
func (rc *RequestConfig) SetFileFromPath(uploads []UploadFile, params ...H) *RequestConfig
SetFileFromPath upload new file.
func (*RequestConfig) SetForm ¶
func (rc *RequestConfig) SetForm(body H) *RequestConfig
SetForm sets the form data for the request configuration. It takes a map of string keys and values, converts it to url.Values, and encodes it as a URL-encoded form string, which is then assigned to the Body field.
Parameters:
body (H): A map containing the form data to be set.
Returns:
*RequestConfig: The updated request configuration.
func (*RequestConfig) SetHeader ¶
func (rc *RequestConfig) SetHeader(headers H) *RequestConfig
SetHeader supply http header what you defined.
func (*RequestConfig) SetJSON ¶
func (rc *RequestConfig) SetJSON(body D) *RequestConfig
SetJSON supply JSON body.
func (*RequestConfig) SetJSONInterface ¶
func (rc *RequestConfig) SetJSONInterface(body any) *RequestConfig
SetJSONInterface supply JSON body
func (*RequestConfig) SetPath ¶ added in v2.2.0
func (rc *RequestConfig) SetPath(str string) *RequestConfig
SetPath supply new request path to deal with path variable request ex. /reqpath/:book/:apple , usage: r.POST("/reqpath/").SetPath("book1/apple2")...
func (*RequestConfig) SetQuery ¶
func (rc *RequestConfig) SetQuery(query H) *RequestConfig
SetQuery sets the query parameters for the request configuration. It takes a map of query parameters and their values, and appends them to the existing path of the request configuration. If the path already contains query parameters, the new parameters are appended with an '&'. Otherwise, they are appended with a '?'.
Parameters:
query (H): A map containing the query parameters and their values.
Returns:
*RequestConfig: The updated request configuration with the query parameters set.
func (*RequestConfig) SetQueryD ¶ added in v2.2.0
func (rc *RequestConfig) SetQueryD(query D) *RequestConfig
SetQueryD supply query string, support query using string array input. ex. /reqpath/?Ids[]=E&Ids[]=M usage: IDArray:=[]string{"E","M"} r.GET("reqpath").SetQueryD(gofight.D{`Ids[]`: IDArray})
type ResponseFunc ¶
type ResponseFunc func(HTTPResponse, HTTPRequest)
ResponseFunc is a type alias for a function that takes an HTTPResponse and an HTTPRequest as parameters. It is used to define a callback function that can handle or process HTTP responses and requests.
type UploadFile ¶
UploadFile for upload file struct