Documentation
¶
Overview ¶
Example ¶
package main import ( "fmt" "io" "net/http" "connectrpc.com/connect/internal/memhttp" ) func main() { hello := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = io.WriteString(w, "Hello, world!") }) srv := memhttp.NewServer(hello) defer srv.Close() res, err := srv.Client().Get(srv.URL()) if err != nil { panic(err) } defer res.Body.Close() fmt.Println(res.Status) }
Output: 200 OK
Index ¶
- type Option
- type Server
- func (s *Server) Cleanup() error
- func (s *Server) Client() *http.Client
- func (s *Server) Close() error
- func (s *Server) RegisterOnShutdown(f func())
- func (s *Server) Shutdown(ctx context.Context) error
- func (s *Server) Transport() *http2.Transport
- func (s *Server) TransportHTTP1() *http.Transport
- func (s *Server) URL() string
- func (s *Server) Wait() error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
An Option configures a Server.
func WithCleanupTimeout ¶
WithCleanupTimeout customizes the default five-second timeout for the server's Cleanup method.
func WithOptions ¶
WithOptions composes multiple Options into one.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a net/http server that uses in-memory pipes instead of TCP. By default, it supports http/2 via h2c. It otherwise uses the same configuration as the zero value of http.Server.
func NewServer ¶
NewServer creates a new Server that uses the given handler. Configuration options may be provided via [Option]s.
func (*Server) Cleanup ¶
Cleanup calls shutdown with a background context set with the cleanup timeout. The default timeout duration is 5 seconds.
func (*Server) Client ¶
Client returns an http.Client configured to use in-memory pipes rather than TCP and speak HTTP/2. It is configured to use the same http2.Transport as [Transport].
Callers may reconfigure the returned client without affecting other clients.
Example ¶
package main import ( "fmt" "io" "net/http" "time" "connectrpc.com/connect/internal/memhttp" ) func main() { hello := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = io.WriteString(w, "Hello, world!") }) srv := memhttp.NewServer(hello) defer srv.Close() client := srv.Client() client.Timeout = 10 * time.Second res, err := client.Get(srv.URL()) if err != nil { panic(err) } defer res.Body.Close() fmt.Println(res.Status) }
Output: 200 OK
func (*Server) Close ¶
Close closes the server's listener. It does not wait for connections to finish.
func (*Server) RegisterOnShutdown ¶
func (s *Server) RegisterOnShutdown(f func())
RegisterOnShutdown registers a function to call on Shutdown. See http.Server.RegisterOnShutdown for details.
func (*Server) Shutdown ¶
Shutdown gracefully shuts down the server, without interrupting any active connections. See http.Server.Shutdown for details.
Example ¶
package main import ( "context" "fmt" "io" "net/http" "time" "connectrpc.com/connect/internal/memhttp" ) func main() { hello := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = io.WriteString(w, "Hello, world!") }) srv := memhttp.NewServer(hello) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() if err := srv.Shutdown(ctx); err != nil { panic(err) } fmt.Println("Server has shut down") }
Output: Server has shut down
func (*Server) Transport ¶
Transport returns a http2.Transport configured to use in-memory pipes rather than TCP and speak both HTTP/1.1 and HTTP/2.
Callers may reconfigure the returned transport without affecting other transports.
func (*Server) TransportHTTP1 ¶
TransportHTTP1 returns a http.Transport configured to use in-memory pipes rather than TCP and speak HTTP/1.1.
Callers may reconfigure the returned transport without affecting other transports.
func (*Server) Wait ¶
Wait blocks until the server exits, then returns an error if not a http.ErrServerClosed error.