2 Star 0 Fork 0

mirrors_qiniu/mockhttp.v1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mockhttp.go 2.52 KB
一键复制 编辑 原始数据 按行查看 历史
xushiwei 提交于 2013-09-10 02:18 . v1.0.00
package mockhttp
import (
"io"
"strconv"
"reflect"
"io/ioutil"
"net/http"
"net/http/httptest"
"github.com/qiniu/log"
"github.com/qiniu/api/auth/digest"
"github.com/qiniu/rpc"
)
// --------------------------------------------------------------------
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
}
// --------------------------------------------------------------------
var Route = map[string]http.Handler{}
// --------------------------------------------------------------------
// type Transport
type transportImpl struct {}
var Transport transportImpl
func (r transportImpl) RoundTrip(req *http.Request) (resp *http.Response, err error) {
h := Route[req.Host]
if h == nil {
log.Fatal("Server not found:", req.Host)
}
cp := *req
cp.Body = &mockServerRequestBody{req.Body, false}
req = &cp
rw := httptest.NewRecorder()
h.ServeHTTP(rw, req)
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 Client = rpc.Client{&http.Client{Transport: Transport}}
func ClientMac(key, secret string) rpc.Client {
mac := &digest.Mac{key, []byte(secret)}
tr := digest.NewTransport(mac, Transport)
return rpc.Client{&http.Client{Transport: tr}}
}
// --------------------------------------------------------------------
func BindEx(host string, p interface{}, method string, args ...interface{}) {
mux := http.NewServeMux()
v := reflect.ValueOf(p)
f := v.MethodByName(method)
if !f.IsValid() {
log.Fatal("mockhttp.Bind: method not found -", method)
} else {
in := make([]reflect.Value, len(args)+1)
in[0] = reflect.ValueOf(mux)
for i, arg := range args {
in[i+1] = reflect.ValueOf(arg)
}
f.Call(in)
}
Route[host] = mux
}
func Bind(host string, p interface{}) {
if p == nil {
Route[host] = http.DefaultServeMux
return
}
if h, ok := p.(http.Handler); ok {
Route[host] = h
} else {
BindEx(host, p, "RegisterHandlers")
}
}
// --------------------------------------------------------------------
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_qiniu/mockhttp.v1.git
git@gitee.com:mirrors_qiniu/mockhttp.v1.git
mirrors_qiniu
mockhttp.v1
mockhttp.v1
develop

搜索帮助