github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/net/http/httptest/server.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // サーバーの実装
     6  
     7  package httptest
     8  
     9  import (
    10  	"github.com/shogo82148/std/crypto/tls"
    11  	"github.com/shogo82148/std/crypto/x509"
    12  	"github.com/shogo82148/std/net"
    13  	"github.com/shogo82148/std/net/http"
    14  	"github.com/shogo82148/std/sync"
    15  )
    16  
    17  // Serverは、エンドツーエンドのHTTPテストで使用するために、
    18  // ローカルループバックインターフェース上のシステムが選んだポートでリッスンするHTTPサーバーです。
    19  type Server struct {
    20  	URL      string
    21  	Listener net.Listener
    22  
    23  	// EnableHTTP2は、サーバー上でHTTP/2が有効かどうかを制御します。
    24  	// NewUnstartedServerを呼び出すときとServer.StartTLSを呼び出すときの間に設定する必要があります。
    25  	EnableHTTP2 bool
    26  
    27  	// TLSはオプションのTLS構成であり、新しい構成でポピュレートされます
    28  	// TLSが開始された後に。 StartTLSが呼び出される前に開始されていないサーバーに設定されている場合、既存のフィールドは新しい構成にコピーされます。
    29  	TLS *tls.Config
    30  
    31  	// NewUnstartedServer を呼び出した後、Start または StartTLS を実行する前に、Config を変更することができます。
    32  	Config *http.Server
    33  
    34  	// certificate はTLS設定の証明書の解析バージョンです。存在する場合にのみ使用されます。
    35  	certificate *x509.Certificate
    36  
    37  	// wgはこのサーバー上の未処理のHTTPリクエストの数をカウントします。
    38  	// Closeはすべてのリクエストが終了するまでブロックします。
    39  	wg sync.WaitGroup
    40  
    41  	mu     sync.Mutex
    42  	closed bool
    43  	conns  map[net.Conn]http.ConnState
    44  
    45  	// client はサーバーとの通信用に設定されています。
    46  	// Close が呼び出されると、自動的にトランスポートが閉じられます。
    47  	client *http.Client
    48  }
    49  
    50  // NewServer は新しい [Server] を起動して返します。
    51  // 使用が終わったら、呼び出し元は Close を呼び出してシャットダウンする必要があります。
    52  func NewServer(handler http.Handler) *Server
    53  
    54  // NewUnstartedServerは新しい [Server] を返しますが、開始はしません。
    55  //
    56  // 設定を変更した後、呼び出し元はStartまたはStartTLSを呼び出す必要があります。
    57  //
    58  // 使用し終えたらCloseを呼び出してシャットダウンする必要があります。
    59  func NewUnstartedServer(handler http.Handler) *Server
    60  
    61  // Start はNewUnstartedServerからサーバーを起動します。
    62  func (s *Server) Start()
    63  
    64  // StartTLSは、NewUnstartedServerからサーバー上でTLSを開始します。
    65  func (s *Server) StartTLS()
    66  
    67  // NewTLSServerはTLSを使用して新しい [Server] を起動し、それを返します。
    68  // 終了時には、呼び出し元はシャットダウンするためにCloseを呼び出す必要があります。
    69  func NewTLSServer(handler http.Handler) *Server
    70  
    71  // Close はサーバーをシャットダウンし、このサーバーに対して保留中のすべてのリクエストが完了するまでブロックします。
    72  func (s *Server) Close()
    73  
    74  // CloseClientConnectionsはテストサーバーへのすべてのオープン中のHTTP接続を閉じます。
    75  func (s *Server) CloseClientConnections()
    76  
    77  // Certificateは、サーバーがTLSを使用していない場合はnil、それ以外の場合はサーバーが使用する証明書を返します。
    78  func (s *Server) Certificate() *x509.Certificate
    79  
    80  // Clientは、サーバーへのリクエストを行うために設定されたHTTPクライアントを返します。
    81  // サーバーのTLSテスト証明書を信頼するように設定されており、[Server.Close] 時にアイドル接続をクローズします。
    82  func (s *Server) Client() *http.Client