MCPcopy
hub / github.com/dropbox/godropbox / SetupTestServer

Function SetupTestServer

net2/http2/test_utils/test_utils.go:31–68  ·  view source on GitHub ↗

Create a dummy server for unittesting. DO NOT USE IN PRODUCTION.

(ssl bool)

Source from the content-addressed store, hash-verified

29
30// Create a dummy server for unittesting. DO NOT USE IN PRODUCTION.
31func SetupTestServer(ssl bool) (*TestServer, string) {
32 closeChan := make(chan bool, 1)
33
34 serveMux := http.NewServeMux()
35 serveMux.HandleFunc(
36 "/slow_request",
37 func(writer http.ResponseWriter, req *http.Request) {
38
39 select {
40 case <-closeChan:
41 return
42 case <-time.After(500 * time.Millisecond):
43 return
44 }
45 })
46 serveMux.HandleFunc(
47 "/",
48 func(writer http.ResponseWriter, req *http.Request) {
49 _, _ = writer.Write([]byte("ok"))
50 })
51 serveMux.HandleFunc(
52 "/redirect",
53 func(writer http.ResponseWriter, req *http.Request) {
54 http.Redirect(writer, req, "/", http.StatusMovedPermanently)
55 })
56
57 server := httptest.NewUnstartedServer(serveMux)
58 server.Config.ReadTimeout = 5 * time.Second
59 server.Config.WriteTimeout = 5 * time.Second
60 if ssl {
61 server.StartTLS()
62 } else {
63 server.Start()
64 }
65
66 addr := server.Listener.Addr().String()
67 return &TestServer{server, closeChan}, addr
68}
69
70// This returns a random port for unit testing. DO NOT USE IN PRODUCTION.
71func RandomListenPort(c *C) int {

Callers 9

TestHTTPMethod · 0.92
TestConnectTimeoutMethod · 0.92
TestResponseTimeoutMethod · 0.92
TestMaxConnMethod · 0.92
TestCloseMethod · 0.92
TestRedirectMethod · 0.92
TestRetriesMethod · 0.92

Calls 4

AddrMethod · 0.80
AfterMethod · 0.65
StringMethod · 0.65
WriteMethod · 0.45

Tested by 9

TestHTTPMethod · 0.74
TestConnectTimeoutMethod · 0.74
TestResponseTimeoutMethod · 0.74
TestMaxConnMethod · 0.74
TestCloseMethod · 0.74
TestRedirectMethod · 0.74
TestRetriesMethod · 0.74