1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
// 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.
//
package gofight
import (
"bytes"
"encoding/json"
"io"
"log"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"time"
)
// Media types
const (
Version = "1.0"
UserAgent = "User-Agent"
ContentType = "Content-Type"
ApplicationJSON = "application/json"
ApplicationForm = "application/x-www-form-urlencoded"
)
// Timeout for http client
// see https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779
var Timeout = time.Second * 10
// HTTPResponse is basic HTTP response type
type HTTPResponse *httptest.ResponseRecorder
// HTTPRequest is basic HTTP request type
type HTTPRequest *http.Request
// ResponseFunc response handling func type
type ResponseFunc func(HTTPResponse, HTTPRequest)
// H is HTTP Header Type
type H map[string]string
// D is HTTP Data Type
type D map[string]interface{}
// RequestConfig provide user input request structure
type RequestConfig struct {
Method string
Path string
Body string
Headers H
Cookies H
Debug bool
ContentType string
}
// UploadFile for upload file struct
type UploadFile struct {
Path string
Name string
Content []byte
}
// New supply initial structure
func New() *RequestConfig {
return &RequestConfig{}
}
// SetDebug supply enable debug mode.
func (rc *RequestConfig) SetDebug(enable bool) *RequestConfig {
rc.Debug = enable
return rc
}
// GET is request method.
func (rc *RequestConfig) GET(path string) *RequestConfig {
rc.Path = path
rc.Method = "GET"
return rc
}
// POST is request method.
func (rc *RequestConfig) POST(path string) *RequestConfig {
rc.Path = path
rc.Method = "POST"
return rc
}
// PUT is request method.
func (rc *RequestConfig) PUT(path string) *RequestConfig {
rc.Path = path
rc.Method = "PUT"
return rc
}
// DELETE is request method.
func (rc *RequestConfig) DELETE(path string) *RequestConfig {
rc.Path = path
rc.Method = "DELETE"
return rc
}
// PATCH is request method.
func (rc *RequestConfig) PATCH(path string) *RequestConfig {
rc.Path = path
rc.Method = "PATCH"
return rc
}
// HEAD is request method.
func (rc *RequestConfig) HEAD(path string) *RequestConfig {
rc.Path = path
rc.Method = "HEAD"
return rc
}
// OPTIONS is request method.
func (rc *RequestConfig) OPTIONS(path string) *RequestConfig {
rc.Path = path
rc.Method = "OPTIONS"
return rc
}
// SetHeader supply http header what you defined.
func (rc *RequestConfig) SetHeader(headers H) *RequestConfig {
if len(headers) > 0 {
rc.Headers = headers
}
return rc
}
// SetJSON supply JSON body.
func (rc *RequestConfig) SetJSON(body D) *RequestConfig {
if b, err := json.Marshal(body); err == nil {
rc.Body = string(b)
}
return rc
}
// SetJSONInterface supply JSON body
func (rc *RequestConfig) SetJSONInterface(body interface{}) *RequestConfig {
if b, err := json.Marshal(body); err == nil {
rc.Body = string(b)
}
return rc
}
// SetForm supply form body.
func (rc *RequestConfig) SetForm(body H) *RequestConfig {
f := make(url.Values)
for k, v := range body {
f.Set(k, v)
}
rc.Body = f.Encode()
return rc
}
// SetFileFromPath upload new file.
func (rc *RequestConfig) SetFileFromPath(uploads []UploadFile, params ...H) *RequestConfig {
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
for _, f := range uploads {
reader := bytes.NewReader(f.Content)
if reader.Size() == 0 {
file, err := os.Open(f.Path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
part, err := writer.CreateFormFile(f.Name, filepath.Base(f.Path))
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(part, file)
if err != nil {
log.Fatal(err)
}
} else {
part, err := writer.CreateFormFile(f.Name, filepath.Base(f.Path))
if err != nil {
log.Fatal(err)
}
_, err = reader.WriteTo(part)
if err != nil {
log.Fatal(err)
}
}
}
if len(params) > 0 {
for key, val := range params[0] {
_ = writer.WriteField(key, val)
}
}
err := writer.Close()
if err != nil {
log.Fatal(err)
}
rc.ContentType = writer.FormDataContentType()
rc.Body = body.String()
return rc
}
// SetQuery supply query string.
func (rc *RequestConfig) SetQuery(query H) *RequestConfig {
f := make(url.Values)
for k, v := range query {
f.Set(k, v)
}
if strings.Contains(rc.Path, "?") {
rc.Path = rc.Path + "&" + f.Encode()
} else {
rc.Path = rc.Path + "?" + f.Encode()
}
return rc
}
// SetBody supply raw body.
func (rc *RequestConfig) SetBody(body string) *RequestConfig {
if len(body) > 0 {
rc.Body = body
}
return rc
}
// SetCookie supply cookies what you defined.
func (rc *RequestConfig) SetCookie(cookies H) *RequestConfig {
if len(cookies) > 0 {
rc.Cookies = cookies
}
return rc
}
func (rc *RequestConfig) initTest() (*http.Request, *httptest.ResponseRecorder) {
qs := ""
if strings.Contains(rc.Path, "?") {
ss := strings.Split(rc.Path, "?")
rc.Path = ss[0]
qs = ss[1]
}
body := bytes.NewBufferString(rc.Body)
req, _ := http.NewRequest(rc.Method, rc.Path, body)
if len(qs) > 0 {
req.URL.RawQuery = qs
}
// Auto add user agent
req.Header.Set(UserAgent, "Gofight-client/"+Version)
if rc.Method == "POST" || rc.Method == "PUT" || rc.Method == "PATCH" {
if strings.HasPrefix(rc.Body, "{") {
req.Header.Set(ContentType, ApplicationJSON)
} else {
req.Header.Set(ContentType, ApplicationForm)
}
}
if rc.ContentType != "" {
req.Header.Set(ContentType, rc.ContentType)
}
if len(rc.Headers) > 0 {
for k, v := range rc.Headers {
req.Header.Set(k, v)
}
}
if len(rc.Cookies) > 0 {
for k, v := range rc.Cookies {
req.AddCookie(&http.Cookie{Name: k, Value: v})
}
}
if rc.Debug {
log.Printf("Request Method: %s", rc.Method)
log.Printf("Request Path: %s", rc.Path)
log.Printf("Request Body: %s", rc.Body)
log.Printf("Request Headers: %s", rc.Headers)
log.Printf("Request Cookies: %s", rc.Cookies)
log.Printf("Request Header: %s", req.Header)
}
w := httptest.NewRecorder()
return req, w
}
// Run execute http request
func (rc *RequestConfig) Run(r http.Handler, response ResponseFunc) {
req, w := rc.initTest()
r.ServeHTTP(w, req)
response(w, req)
}
|