本文整理汇总了Golang中github.com/elazarl/goproxy.ProxyHttpServer.OnRequest方法的典型用法代码示例。如果您正苦于以下问题:Golang ProxyHttpServer.OnRequest方法的具体用法?Golang ProxyHttpServer.OnRequest怎么用?Golang ProxyHttpServer.OnRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/elazarl/goproxy.ProxyHttpServer
的用法示例。
在下文中一共展示了ProxyHttpServer.OnRequest方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1:
It("connects using invalid URL", func() {
errMsg := "Invalid consumerProxyFunc URL"
consumerProxyFunc = func(*http.Request) (*url.URL, error) {
return nil, errors.New(errMsg)
}
perform()
var err error
Eventually(errorChan).Should(Receive(&err))
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(errMsg))
})
It("connects to a consumerProxyFunc server rejecting CONNECT requests", func() {
goProxyHandler.OnRequest().HandleConnect(goproxy.AlwaysReject)
perform()
var err error
Eventually(errorChan).Should(Receive(&err))
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Error dialing traffic controller server"))
})
It("connects to a non-consumerProxyFunc server", func() {
nonProxyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Go away, I am not a consumerProxyFunc!", http.StatusBadRequest)
}))
consumerProxyFunc = func(*http.Request) (*url.URL, error) {
return url.Parse(nonProxyServer.URL)
示例2: ProxyBasic
// ProxyBasic will force HTTP authentication before any request to the proxy is processed
func ProxyBasic(proxy *goproxy.ProxyHttpServer, realm string, f func(user, passwd string) bool) {
proxy.OnRequest().Do(Basic(realm, f))
proxy.OnRequest().HandleConnect(BasicConnect(realm, f))
}
示例3:
Context("with a message in the trafficcontroller", func() {
BeforeEach(func() {
messagesToSend <- marshalMessage(createMessage("hello", 0))
})
It("connects using valid URL to running proxy server", func() {
message := <-incoming
Expect(message.GetLogMessage().GetMessage()).To(Equal([]byte("hello")))
})
})
Context("with an auth proxy server", func() {
BeforeEach(func() {
goProxyHandler.OnRequest().HandleConnect(auth.BasicConnect("my_realm", func(user, passwd string) bool {
return user == "user" && passwd == "password"
}))
proxy = func(*http.Request) (*url.URL, error) {
proxyURL, err := url.Parse(testProxyServer.URL)
proxyURL.User = url.UserPassword("user", "password")
if err != nil {
return nil, err
}
return proxyURL, nil
}
messagesToSend <- marshalMessage(createMessage("hello", 0))
})
It("connects successfully", func() {
message := <-incoming