LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Implementation of Server | 5 // Implementation of Server |
6 | 6 |
7 package httptest | 7 package httptest |
8 | 8 |
9 import ( | 9 import ( |
10 "fmt" | 10 "fmt" |
11 "http" | 11 "http" |
| 12 "os" |
12 "net" | 13 "net" |
13 ) | 14 ) |
14 | 15 |
15 // A Server is an HTTP server listening on a system-chosen port on the | 16 // A Server is an HTTP server listening on a system-chosen port on the |
16 // local loopback interface, for use in end-to-end HTTP tests. | 17 // local loopback interface, for use in end-to-end HTTP tests. |
17 type Server struct { | 18 type Server struct { |
18 URL string // base URL of form http://ipaddr:port with no trailing
slash | 19 URL string // base URL of form http://ipaddr:port with no trailing
slash |
19 Listener net.Listener | 20 Listener net.Listener |
| 21 } |
| 22 |
| 23 // historyListener keeps track of all connections that it's ever |
| 24 // accepted. |
| 25 type historyListener struct { |
| 26 net.Listener |
| 27 history []net.Conn |
| 28 } |
| 29 |
| 30 func (hs *historyListener) Accept() (c net.Conn, err os.Error) { |
| 31 c, err = hs.Listener.Accept() |
| 32 if err == nil { |
| 33 hs.history = append(hs.history, c) |
| 34 } |
| 35 return |
20 } | 36 } |
21 | 37 |
22 // NewServer starts and returns a new Server. | 38 // NewServer starts and returns a new Server. |
23 // The caller should call Close when finished, to shut it down. | 39 // The caller should call Close when finished, to shut it down. |
24 func NewServer(handler http.Handler) *Server { | 40 func NewServer(handler http.Handler) *Server { |
25 ts := new(Server) | 41 ts := new(Server) |
26 l, err := net.Listen("tcp", "127.0.0.1:0") | 42 l, err := net.Listen("tcp", "127.0.0.1:0") |
27 if err != nil { | 43 if err != nil { |
28 if l, err = net.Listen("tcp6", "[::1]:0"); err != nil { | 44 if l, err = net.Listen("tcp6", "[::1]:0"); err != nil { |
29 panic(fmt.Sprintf("httptest: failed to listen on a port:
%v", err)) | 45 panic(fmt.Sprintf("httptest: failed to listen on a port:
%v", err)) |
30 } | 46 } |
31 } | 47 } |
32 » ts.Listener = l | 48 » ts.Listener = &historyListener{l, make([]net.Conn, 0)} |
33 ts.URL = "http://" + l.Addr().String() | 49 ts.URL = "http://" + l.Addr().String() |
34 server := &http.Server{Handler: handler} | 50 server := &http.Server{Handler: handler} |
35 » go server.Serve(l) | 51 » go server.Serve(ts.Listener) |
36 return ts | 52 return ts |
37 } | 53 } |
38 | 54 |
39 // Close shuts down the server. | 55 // Close shuts down the server. |
40 func (s *Server) Close() { | 56 func (s *Server) Close() { |
41 s.Listener.Close() | 57 s.Listener.Close() |
42 } | 58 } |
| 59 |
| 60 // CloseClientConnections closes any currently open HTTP connections |
| 61 // to the test Server. |
| 62 func (s *Server) CloseClientConnections() { |
| 63 hl, ok := s.Listener.(*historyListener) |
| 64 if !ok { |
| 65 return |
| 66 } |
| 67 for _, conn := range hl.history { |
| 68 conn.Close() |
| 69 } |
| 70 } |
LEFT | RIGHT |