本文整理匯總了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