栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Go中模拟HTTPS响应

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Go中模拟HTTPS响应

net/http
可以模仿,扩展或更改其中的大多数行为。尽管这
http.Client
是实现HTTP客户端语义的具体类型,但其所有字段都可以导出并可以自定义。

Client.Transport
特别是,可以替换该字段以使客户端执行任何操作,从使用自定义协议(例如ftp://或file://)到直接连接到本地处理程序(不生成HTTP协议字节或通过网络发送任何内容)
)。

客户端的功能,例如

http.Get
,所有利用导出的
http.DefaultClient
包变量(你可以修改),因此,利用这些方便的功能代码并
没有
,例如,必须改变到在定制客户端变量调用方法。请注意,虽然在公共可用的库中修改全局行为是不合理的,但在应用程序和测试(包括库测试)中进行修改非常有用。

http://play.golang.org/p/afljO086iB包含一个自定义

http.RoundTripper
脚本,该自定义脚本重写了请求URL,以便将其路由到本地托管的
httptest.Server
;另一个示例将请求直接传递给
http.Handler
,以及自定义
http.ResponseWriter
实现,为了创建一个
http.Response
。第二种方法不像第一种方法那样努力(它没有填写Response值中的许多字段),但是效率更高,并且应该足够兼容以与大多数处理程序和客户端调用程序一起使用。

上面链接的代码也包括在下面:

package mainimport (    "fmt"    "io"    "log"    "net/http"    "net/http/httptest"    "net/url"    "os"    "path"    "strings")func Handler(w http.ResponseWriter, r *http.Request) {    fmt.Fprintf(w, "hello %sn", path.base(r.URL.Path))}func main() {    s := httptest.NewServer(http.HandlerFunc(Handler))    u, err := url.Parse(s.URL)    if err != nil {        log.Fatalln("failed to parse httptest.Server URL:", err)    }    http.DefaultClient.Transport = RewriteTransport{URL: u}    resp, err := http.Get("https://google.com/path-one")    if err != nil {        log.Fatalln("failed to send first request:", err)    }    fmt.Println("[First Response]")    resp.Write(os.Stdout)    fmt.Print("n", strings.Repeat("-", 80), "nn")    http.DefaultClient.Transport = HandlerTransport{http.HandlerFunc(Handler)}    resp, err = http.Get("https://google.com/path-two")    if err != nil {        log.Fatalln("failed to send second request:", err)    }    fmt.Println("[Second Response]")    resp.Write(os.Stdout)}// RewriteTransport is an http.RoundTripper that rewrites requests// using the provided URL's Scheme and Host, and its Path as a prefix.// The Opaque field is untouched.// If Transport is nil, http.DefaultTransport is usedtype RewriteTransport struct {    Transport http.RoundTripper    URL       *url.URL}func (t RewriteTransport) RoundTrip(req *http.Request) (*http.Response, error) {    // note that url.URL.ResolveReference doesn't work here    // since t.u is an absolute url    req.URL.Scheme = t.URL.Scheme    req.URL.Host = t.URL.Host    req.URL.Path = path.Join(t.URL.Path, req.URL.Path)    rt := t.Transport    if rt == nil {        rt = http.DefaultTransport    }    return rt.RoundTrip(req)}type HandlerTransport struct{ h http.Handler }func (t HandlerTransport) RoundTrip(req *http.Request) (*http.Response, error) {    r, w := io.Pipe()    resp := &http.Response{        Proto:      "HTTP/1.1",        ProtoMajor: 1,        ProtoMinor: 1,        Header:     make(http.Header),        Body:       r,        Request:    req,    }    ready := make(chan struct{})    prw := &pipeResponseWriter{r, w, resp, ready}    go func() {        defer w.Close()        t.h.ServeHTTP(prw, req)    }()    <-ready    return resp, nil}type pipeResponseWriter struct {    r     *io.PipeReader    w     *io.PipeWriter    resp  *http.Response    ready chan<- struct{}}func (w *pipeResponseWriter) Header() http.Header {    return w.resp.Header}func (w *pipeResponseWriter) Write(p []byte) (int, error) {    if w.ready != nil {        w.WriteHeader(http.StatusOK)    }    return w.w.Write(p)}func (w *pipeResponseWriter) WriteHeader(status int) {    if w.ready == nil {        // already called        return    }    w.resp.StatusCode = status    w.resp.Status = fmt.Sprintf("%d %s", status, http.StatusText(status))    close(w.ready)    w.ready = nil}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/484284.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号