mirror of
https://github.com/eiblog/eiblog.git
synced 2026-02-17 11:52:27 +08:00
update vendor
This commit is contained in:
110
vendor/github.com/qiniu/x/mockhttp.v7/mockhttp.go
generated
vendored
Normal file
110
vendor/github.com/qiniu/x/mockhttp.v7/mockhttp.go
generated
vendored
Normal file
@@ -0,0 +1,110 @@
|
||||
package mockhttp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
|
||||
"qiniupkg.com/x/log.v7"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrServerNotFound = errors.New("server not found")
|
||||
)
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
type mockServerRequestBody struct {
|
||||
reader io.Reader
|
||||
closeSignal bool
|
||||
}
|
||||
|
||||
func (r *mockServerRequestBody) Read(p []byte) (int, error) {
|
||||
if r.closeSignal || r.reader == nil {
|
||||
return 0, io.EOF
|
||||
}
|
||||
return r.reader.Read(p)
|
||||
}
|
||||
|
||||
func (r *mockServerRequestBody) Close() error {
|
||||
r.closeSignal = true
|
||||
if c, ok := r.reader.(io.Closer); ok {
|
||||
return c.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// type Transport
|
||||
|
||||
type Transport struct {
|
||||
route map[string]http.Handler
|
||||
}
|
||||
|
||||
func NewTransport() *Transport {
|
||||
|
||||
return &Transport{
|
||||
route: make(map[string]http.Handler),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Transport) ListenAndServe(host string, h http.Handler) {
|
||||
|
||||
if h == nil {
|
||||
h = http.DefaultServeMux
|
||||
}
|
||||
p.route[host] = h
|
||||
}
|
||||
|
||||
func (p *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
||||
|
||||
h := p.route[req.URL.Host]
|
||||
if h == nil {
|
||||
log.Warn("Server not found:", req.Host)
|
||||
return nil, ErrServerNotFound
|
||||
}
|
||||
|
||||
cp := *req
|
||||
cp.URL.Scheme = ""
|
||||
cp.URL.Host = ""
|
||||
cp.RemoteAddr = "127.0.0.1:8000"
|
||||
cp.Body = &mockServerRequestBody{req.Body, false}
|
||||
req = &cp
|
||||
|
||||
rw := httptest.NewRecorder()
|
||||
h.ServeHTTP(rw, req)
|
||||
|
||||
req.Body.Close()
|
||||
|
||||
ctlen := int64(-1)
|
||||
if v := rw.HeaderMap.Get("Content-Length"); v != "" {
|
||||
ctlen, _ = strconv.ParseInt(v, 10, 64)
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
Status: "",
|
||||
StatusCode: rw.Code,
|
||||
Header: rw.HeaderMap,
|
||||
Body: ioutil.NopCloser(rw.Body),
|
||||
ContentLength: ctlen,
|
||||
TransferEncoding: nil,
|
||||
Close: false,
|
||||
Trailer: nil,
|
||||
Request: req,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
var DefaultTransport = NewTransport()
|
||||
var DefaultClient = &http.Client{Transport: DefaultTransport}
|
||||
|
||||
func ListenAndServe(host string, h http.Handler) {
|
||||
|
||||
DefaultTransport.ListenAndServe(host, h)
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
114
vendor/github.com/qiniu/x/mockhttp.v7/mockhttp_test.go
generated
vendored
Normal file
114
vendor/github.com/qiniu/x/mockhttp.v7/mockhttp_test.go
generated
vendored
Normal file
@@ -0,0 +1,114 @@
|
||||
package mockhttp_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"qiniupkg.com/x/mockhttp.v7"
|
||||
"qiniupkg.com/x/rpc.v7"
|
||||
)
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
func reply(w http.ResponseWriter, code int, data interface{}) {
|
||||
|
||||
msg, _ := json.Marshal(data)
|
||||
h := w.Header()
|
||||
h.Set("Content-Length", strconv.Itoa(len(msg)))
|
||||
h.Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
w.Write(msg)
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
type FooRet struct {
|
||||
A int `json:"a"`
|
||||
B string `json:"b"`
|
||||
C string `json:"c"`
|
||||
}
|
||||
|
||||
type HandleRet map[string]string
|
||||
|
||||
type FooServer struct{}
|
||||
|
||||
func (p *FooServer) foo(w http.ResponseWriter, req *http.Request) {
|
||||
reply(w, 200, &FooRet{1, req.Host, req.URL.Path})
|
||||
}
|
||||
|
||||
func (p *FooServer) handle(w http.ResponseWriter, req *http.Request) {
|
||||
reply(w, 200, HandleRet{"foo": "1", "bar": "2"})
|
||||
}
|
||||
|
||||
func (p *FooServer) postDump(w http.ResponseWriter, req *http.Request) {
|
||||
req.Body.Close()
|
||||
io.Copy(w, req.Body)
|
||||
}
|
||||
|
||||
func (p *FooServer) RegisterHandlers(mux *http.ServeMux) {
|
||||
mux.HandleFunc("/foo", func(w http.ResponseWriter, req *http.Request) { p.foo(w, req) })
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { p.handle(w, req) })
|
||||
mux.HandleFunc("/dump", func(w http.ResponseWriter, req *http.Request) { p.postDump(w, req) })
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
func TestBasic(t *testing.T) {
|
||||
|
||||
server := new(FooServer)
|
||||
server.RegisterHandlers(http.DefaultServeMux)
|
||||
|
||||
mockhttp.ListenAndServe("foo.com", nil)
|
||||
|
||||
c := rpc.Client{mockhttp.DefaultClient}
|
||||
{
|
||||
var foo FooRet
|
||||
err := c.Call(nil, &foo, "POST", "http://foo.com/foo")
|
||||
if err != nil {
|
||||
t.Fatal("call foo failed:", err)
|
||||
}
|
||||
if foo.A != 1 || foo.B != "foo.com" || foo.C != "/foo" {
|
||||
t.Fatal("call foo: invalid ret")
|
||||
}
|
||||
fmt.Println(foo)
|
||||
}
|
||||
{
|
||||
var ret map[string]string
|
||||
err := c.Call(nil, &ret, "POST", "http://foo.com/bar")
|
||||
if err != nil {
|
||||
t.Fatal("call foo failed:", err)
|
||||
}
|
||||
if ret["foo"] != "1" || ret["bar"] != "2" {
|
||||
t.Fatal("call bar: invalid ret")
|
||||
}
|
||||
fmt.Println(ret)
|
||||
}
|
||||
{
|
||||
resp, err := c.Post("http://foo.com/dump", "", nil)
|
||||
if err != nil {
|
||||
t.Fatal("post foo failed:", err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
resp, err = c.Post("http://foo.com/dump", "", strings.NewReader("abc"))
|
||||
if err != nil {
|
||||
t.Fatal("post foo failed:", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatal("ioutil.ReadAll:", err)
|
||||
}
|
||||
if len(b) != 0 {
|
||||
t.Fatal("body should be empty:", string(b))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user