Create a dummy server for unittesting. DO NOT USE IN PRODUCTION.
(ssl bool)
| 29 | |
| 30 | // Create a dummy server for unittesting. DO NOT USE IN PRODUCTION. |
| 31 | func 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. |
| 71 | func RandomListenPort(c *C) int { |