本文整理汇总了Golang中net/http/httptest.Server.Start方法的典型用法代码示例。如果您正苦于以下问题:Golang Server.Start方法的具体用法?Golang Server.Start怎么用?Golang Server.Start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/http/httptest.Server
的用法示例。
在下文中一共展示了Server.Start方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestWithHttptestWithSpecifiedPort
func TestWithHttptestWithSpecifiedPort(t *testing.T) {
router := New()
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
l, _ := net.Listen("tcp", ":8033")
ts := httptest.Server{
Listener: l,
Config: &http.Server{Handler: router},
}
ts.Start()
defer ts.Close()
testRequest(t, "http://localhost:8033/example")
}
示例2:
Ω([]string{"DELETE", "GET"}).Should(ContainElement(r.Method))
Ω(r.URL.Path).Should(Equal("/"))
agentCalled++
w.WriteHeader(status)
if r.Method == "GET" {
w.Write([]byte("{\"port\": 12345, \"password\": \"super-secret\"}"))
}
})
listener, err := net.Listen("tcp", hostAndPort)
Ω(err).ShouldNot(HaveOccurred())
server = httptest.NewUnstartedServer(handler)
server.Listener = listener
server.Start()
Eventually(isListeningChecker(hostAndPort)).Should(BeTrue())
})
AfterEach(func() {
server.Close()
Eventually(isListeningChecker(hostAndPort)).Should(BeFalse())
})
Describe("#Reset", func() {
Context("when the DELETE request is successful", func() {
BeforeEach(func() {
status = http.StatusOK
})
It("makes a DELETE request to the rootURL", func() {
示例3:
Context("when the server is not running", func() {
It("errors when connecting", func(done Done) {
err := l.Start("ws://localhost:1234", "myApp", outputChan, stopChan)
Expect(err).To(HaveOccurred())
close(done)
}, 2)
})
Context("when the converter returns nil messages", func() {
BeforeEach(func() {
converter = func([]byte) ([]byte, error) { return nil, nil }
})
JustBeforeEach(func() {
ts.Start()
Eventually(func() bool {
resp, _ := http.Head(fmt.Sprintf("http://%s", ts.Listener.Addr()))
return resp != nil && resp.StatusCode == http.StatusOK
}).Should(BeTrue())
})
It("ignores nil messages received from the server", func() {
go l.Start(fmt.Sprintf("ws://%s", ts.Listener.Addr()), "myApp", outputChan, stopChan)
messageChan <- []byte("ignored")
Consistently(outputChan).ShouldNot(Receive())
Consistently(mockBatcher.BatchCounterInput).ShouldNot(BeCalled())
})
})
示例4: testUpgradeWithCurl
func testUpgradeWithCurl(t *testing.T, mode testUpgradeMode, lenient bool) {
if runtime.GOOS != "linux" {
t.Skip("skipping Docker test when not on Linux; requires --net which won't work with boot2docker anyway")
}
requireCurl(t)
const msg = "Hello from curl!\n"
var ts2 *httptest.Server
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Foo", "Bar")
w.Header().Set("Client-Proto", r.Proto)
io.WriteString(w, msg)
})
ts := httptest.NewUnstartedServer(handler)
switch mode {
case testUpgradeNoTLS:
ts.TLS = nil
default:
ts2 = httptest.NewUnstartedServer(handler)
ts2.TLS = nil
}
wrapped := UpgradeServer(ts.Config, &Server{
PermitProhibitedCipherSuites: lenient,
})
modes := make([]testUpgradeMode, 0, 2)
switch mode {
case testUpgradeNoTLS:
modes = append(modes, mode)
ts.Config = wrapped
ts.Start()
t.Logf("Running test server for curl to hit at: %s", ts.URL)
case testUpgradeDualTLS:
modes = append(modes, testUpgradeNoTLS)
fallthrough
default:
modes = append(modes, testUpgradeTLSOnly)
ts2.Config = wrapped
ts.TLS = ts.Config.TLSConfig // the httptest.Server has its own copy of this TLS config
ts.StartTLS()
ts2.Start()
defer ts2.Close()
t.Logf("Running test server for curl to hit at: %s and %s", ts.URL, ts2.URL)
}
defer ts.Close()
defer func() { testHookOnConn = nil }()
for _, mode := range modes {
var gotConn int32
testHookOnConn = func() { atomic.StoreInt32(&gotConn, 1) }
T := ts
if mode == testUpgradeNoTLS && ts2 != nil {
T = ts2
}
container := curl(t, "-D-", "--silent", "--http2", "--insecure", T.URL)
defer kill(container)
resc := make(chan interface{}, 1)
go func() {
res, err := dockerLogs(container)
if err != nil {
resc <- err
} else {
resc <- res
}
}()
select {
case res := <-resc:
if err, ok := res.(error); ok {
t.Fatal(err)
}
testDualUpgradeOutput(t, string(res.([]byte)), (T.TLS == nil),
"HTTP/2.0 200",
"foo:Bar",
"client-proto:HTTP/2",
msg)
case <-time.After(3 * time.Second):
t.Errorf("timeout waiting for curl")
}
if atomic.LoadInt32(&gotConn) == 0 {
t.Error("never saw an http2 connection")
}
}
}