feat: switch glide to govendor. (#186)
* feat: switch glide to govendor. * fix: testing
This commit is contained in:
21
vendor/gopkg.in/appleboy/gin-status-api.v1/LICENSE
generated
vendored
Normal file
21
vendor/gopkg.in/appleboy/gin-status-api.v1/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Bo-Yi Wu
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
29
vendor/gopkg.in/appleboy/gin-status-api.v1/README.md
generated
vendored
Normal file
29
vendor/gopkg.in/appleboy/gin-status-api.v1/README.md
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
# gin-status-api
|
||||
|
||||
Golang cpu, memory, gc, etc information api handler written in Go (Golang) for [Gin](https://github.com/gin-gonic/gin) framework
|
||||
|
||||
[](https://godoc.org/github.com/appleboy/gin-status-api) [](https://travis-ci.org/appleboy/gin-status-api) [](https://goreportcard.com/report/github.com/appleboy/gin-status-api) [](https://coveralls.io/github/appleboy/gin-status-api?branch=master)
|
||||
|
||||
## Usage
|
||||
|
||||
Downladn this package.
|
||||
|
||||
```bash
|
||||
$ go get gopkg.in/appleboy/gin-status-api.v1
|
||||
```
|
||||
|
||||
To import this package, add the following line to your code:
|
||||
|
||||
```go
|
||||
import "gopkg.in/appleboy/gin-status-api.v1"
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
Please see [example](example/server.go).
|
||||
|
||||
## Response
|
||||
|
||||
Please read [detail information](https://github.com/fukata/golang-stats-api-handler#toc3).
|
||||
|
||||

|
||||
12
vendor/gopkg.in/appleboy/gin-status-api.v1/status.go
generated
vendored
Normal file
12
vendor/gopkg.in/appleboy/gin-status-api.v1/status.go
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
package status
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
api "gopkg.in/fukata/golang-stats-api-handler.v1"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// StatusHandler is gin handle for get system status.
|
||||
func StatusHandler(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, api.GetStats())
|
||||
}
|
||||
21
vendor/gopkg.in/appleboy/gofight.v2/LICENSE
generated
vendored
Normal file
21
vendor/gopkg.in/appleboy/gofight.v2/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Bo-Yi Wu
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
25
vendor/gopkg.in/appleboy/gofight.v2/Makefile
generated
vendored
Normal file
25
vendor/gopkg.in/appleboy/gofight.v2/Makefile
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
.PHONY: all example test
|
||||
|
||||
TARGETS_NOVENDOR := $(shell glide novendor)
|
||||
export PROJECT_PATH = /go/src/github.com/appleboy/gofight
|
||||
|
||||
all: install test
|
||||
|
||||
install:
|
||||
go get -t -d -v ./...
|
||||
|
||||
example:
|
||||
cd example && go test -v -cover .
|
||||
|
||||
test: example
|
||||
go test -v -cover .
|
||||
|
||||
docker_test: clean
|
||||
docker run --rm \
|
||||
-v $(PWD):$(PROJECT_PATH) \
|
||||
-w=$(PROJECT_PATH) \
|
||||
appleboy/golang-testing \
|
||||
sh -c "make install && coverage all"
|
||||
|
||||
clean:
|
||||
rm -rf .cover vendor
|
||||
253
vendor/gopkg.in/appleboy/gofight.v2/README.md
generated
vendored
Normal file
253
vendor/gopkg.in/appleboy/gofight.v2/README.md
generated
vendored
Normal file
@@ -0,0 +1,253 @@
|
||||
# Gofight
|
||||
|
||||
[](https://godoc.org/github.com/appleboy/gofight) [](https://travis-ci.org/appleboy/gofight) [](https://coveralls.io/github/appleboy/gofight?branch=master) [](https://goreportcard.com/report/github.com/appleboy/gofight) [](https://codebeat.co/projects/github-com-appleboy-gofight) [](https://codecov.io/gh/appleboy/gofight)
|
||||
|
||||
API Handler Testing for Golang framework.
|
||||
|
||||
## Support Framework
|
||||
|
||||
* [x] [Http Handler](https://golang.org/pkg/net/http/) Golang package http provides HTTP client and server implementations.
|
||||
* [x] [Gin](https://github.com/gin-gonic/gin)
|
||||
* [x] [Echo](https://github.com/labstack/echo) support [v3.0.0](https://github.com/labstack/echo/releases/tag/v3.0.0) up
|
||||
* [x] [Mux](https://github.com/gorilla/mux)
|
||||
* [x] [HttpRouter](https://github.com/julienschmidt/httprouter)
|
||||
|
||||
## Usage
|
||||
|
||||
Downladn this package.
|
||||
|
||||
```bash
|
||||
$ go get gopkg.in/appleboy/gofight.v1
|
||||
```
|
||||
|
||||
To import this package, add the following line to your code:
|
||||
|
||||
```go
|
||||
import "gopkg.in/appleboy/gofight.v1"
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
The following is basic testing example.
|
||||
|
||||
Main Program:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func BasicHelloHandler(w http.ResponseWriter, r *http.Request) {
|
||||
io.WriteString(w, "Hello World")
|
||||
}
|
||||
|
||||
func BasicEngine() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/", BasicHelloHandler)
|
||||
|
||||
return mux
|
||||
}
|
||||
```
|
||||
|
||||
Testing:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/appleboy/gofight"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBasicHelloWorld(t *testing.T) {
|
||||
r := gofight.New()
|
||||
|
||||
r.GET("/").
|
||||
// turn on the debug mode.
|
||||
SetDebug(true).
|
||||
Run(BasicEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
||||
|
||||
assert.Equal(t, "Hello World", r.Body.String())
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
### Set Header
|
||||
|
||||
You can add custom header via `SetHeader` func.
|
||||
|
||||
```go
|
||||
func TestBasicHelloWorld(t *testing.T) {
|
||||
r := gofight.New()
|
||||
version := "0.0.1"
|
||||
|
||||
r.GET("/").
|
||||
// turn on the debug mode.
|
||||
SetDebug(true).
|
||||
SetHeader(gofight.H{
|
||||
"X-Version": version,
|
||||
}).
|
||||
Run(BasicEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
||||
|
||||
assert.Equal(t, version, rq.Header.Get("X-Version"))
|
||||
assert.Equal(t, "Hello World", r.Body.String())
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
### POST FORM Data
|
||||
|
||||
Using `SetFORM` to generate form data.
|
||||
|
||||
```go
|
||||
func TestPostFormData(t *testing.T) {
|
||||
r := gofight.New()
|
||||
|
||||
r.POST("/form").
|
||||
SetFORM(gofight.H{
|
||||
"a": "1",
|
||||
"b": "2",
|
||||
}).
|
||||
Run(BasicEngine(), func(r HTTPResponse, rq HTTPRequest) {
|
||||
data := []byte(r.Body.String())
|
||||
|
||||
a, _ := jsonparser.GetString(data, "a")
|
||||
b, _ := jsonparser.GetString(data, "b")
|
||||
|
||||
assert.Equal(t, "1", a)
|
||||
assert.Equal(t, "2", b)
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
### POST JSON Data
|
||||
|
||||
Using `SetJSON` to generate json data.
|
||||
|
||||
```go
|
||||
func TestPostJSONData(t *testing.T) {
|
||||
r := gofight.New()
|
||||
|
||||
r.POST("/json").
|
||||
SetJSON(gofight.D{
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
}).
|
||||
Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
|
||||
data := []byte(r.Body.String())
|
||||
|
||||
a, _ := jsonparser.GetInt(data, "a")
|
||||
b, _ := jsonparser.GetInt(data, "b")
|
||||
|
||||
assert.Equal(t, 1, int(a))
|
||||
assert.Equal(t, 2, int(b))
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
### POST RAW Data
|
||||
|
||||
Using `SetBody` to generate raw data.
|
||||
|
||||
```go
|
||||
func TestPostRawData(t *testing.T) {
|
||||
r := gofight.New()
|
||||
|
||||
r.POST("/raw").
|
||||
SetBody("a=1&b=1").
|
||||
Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
|
||||
data := []byte(r.Body.String())
|
||||
|
||||
a, _ := jsonparser.GetString(data, "a")
|
||||
b, _ := jsonparser.GetString(data, "b")
|
||||
|
||||
assert.Equal(t, "1", a)
|
||||
assert.Equal(t, "2", b)
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
### Set Query String
|
||||
|
||||
Using `SetQuery` to generate raw data.
|
||||
|
||||
```go
|
||||
func TestQueryString(t *testing.T) {
|
||||
r := gofight.New()
|
||||
|
||||
r.GET("/hello").
|
||||
SetQuery(gofight.H{
|
||||
"a": "1",
|
||||
"b": "2",
|
||||
}).
|
||||
Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
or append exist query parameter.
|
||||
|
||||
```go
|
||||
func TestQueryString(t *testing.T) {
|
||||
r := gofight.New()
|
||||
|
||||
r.GET("/hello?foo=bar").
|
||||
SetQuery(gofight.H{
|
||||
"a": "1",
|
||||
"b": "2",
|
||||
}).
|
||||
Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
### Set Cookie String
|
||||
|
||||
Using `SetCookie` to generate raw data.
|
||||
|
||||
```go
|
||||
func TestQueryString(t *testing.T) {
|
||||
r := gofight.New()
|
||||
|
||||
r.GET("/hello").
|
||||
SetCookie(gofight.H{
|
||||
"foo": "bar",
|
||||
}).
|
||||
Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
assert.Equal(t, "foo=bar", rq.Header.Get("cookie"))
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
* Basic HTTP Router: [basic.go](example/basic.go), [basic_test.go](example/basic_test.go)
|
||||
* Gin Framework: [gin.go](example/gin.go), [gin_test.go](example/gin_test.go)
|
||||
* Echo Framework: [echo.go](example/echo.go), [echo_test.go](example/echo_test.go)
|
||||
* Mux Framework: [mux.go](example/mux.go), [mux_test.go](example/mux_test.go)
|
||||
* HttpRouter Framework: [httprouter.go](example/httprouter.go), [httprouter_test.go](example/httprouter_test.go)
|
||||
|
||||
## Testing
|
||||
|
||||
```
|
||||
$ make
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
Copyright 2016 Bo-Yi Wu [@appleboy](https://twitter.com/appleboy).
|
||||
|
||||
Licensed under the MIT License.
|
||||
314
vendor/gopkg.in/appleboy/gofight.v2/gofight.go
generated
vendored
Normal file
314
vendor/gopkg.in/appleboy/gofight.v2/gofight.go
generated
vendored
Normal file
@@ -0,0 +1,314 @@
|
||||
// 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"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
// "github.com/labstack/echo"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Media types
|
||||
const (
|
||||
Version = "1.0"
|
||||
UserAgent = "User-Agent"
|
||||
ContentType = "Content-Type"
|
||||
ApplicationJSON = "application/json"
|
||||
ApplicationForm = "application/x-www-form-urlencoded"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// TestRequest is testing url string if server is running
|
||||
func TestRequest(t *testing.T, url string) {
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
}
|
||||
client := &http.Client{Transport: tr}
|
||||
|
||||
resp, err := client.Get(url)
|
||||
defer resp.Body.Close()
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, ioerr := ioutil.ReadAll(resp.Body)
|
||||
assert.NoError(t, ioerr)
|
||||
assert.Equal(t, "200 OK", resp.Status, "should get a 200")
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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" {
|
||||
if strings.HasPrefix(rc.Body, "{") {
|
||||
req.Header.Set(ContentType, ApplicationJSON)
|
||||
} else {
|
||||
req.Header.Set(ContentType, ApplicationForm)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user