MCPcopy Index your code
hub / github.com/google/go-github / setup

Function setup

github/github_test.go:71–135  ·  view source on GitHub ↗

setup sets up a test HTTP server along with a github.Client that is configured to talk to that test server. Tests should register handlers on mux which provide mock responses for the API method being tested.

(t *testing.T)

Source from the content-addressed store, hash-verified

69// configured to talk to that test server. Tests should register handlers on
70// mux which provide mock responses for the API method being tested.
71func setup(t *testing.T) (client *Client, mux *http.ServeMux, serverURL string) {
72 t.Helper()
73 // mux is the HTTP request multiplexer used with the test server.
74 mux = http.NewServeMux()
75
76 // We want to ensure that tests catch mistakes where the endpoint URL is
77 // specified as absolute rather than relative. It only makes a difference
78 // when there's a non-empty base URL path. So, use that. See issue #752.
79 apiHandler := http.NewServeMux()
80 apiHandler.Handle(baseURLPath+"/", http.StripPrefix(baseURLPath, mux))
81 apiHandler.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
82 fmt.Fprintln(os.Stderr, "FAIL: Client.BaseURL path prefix is not preserved in the request URL:")
83 fmt.Fprintln(os.Stderr)
84 fmt.Fprintln(os.Stderr, "\t"+req.URL.String())
85 fmt.Fprintln(os.Stderr)
86 fmt.Fprintln(os.Stderr, "\tDid you accidentally use an absolute endpoint URL rather than relative?")
87 fmt.Fprintln(os.Stderr, "\tSee https://github.com/google/go-github/issues/752 for information.")
88 http.Error(w, "Client.BaseURL path prefix is not preserved in the request URL.", http.StatusInternalServerError)
89 })
90
91 // server is a test HTTP server used to provide mock API responses.
92 server := httptest.NewServer(apiHandler)
93
94 testDialer := &net.Dialer{Timeout: 30 * time.Second}
95
96 // Create a custom transport with isolated connection pool
97 transport := &http.Transport{
98 // Wrap dialed connections so transport does not take concrete-TCP sendfile fast paths
99 // that can race under Windows + -race in upload tests.
100 DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
101 conn, err := testDialer.DialContext(ctx, network, addr)
102 if err != nil {
103 return nil, err
104 }
105 return &raceSafeTestConn{Conn: conn}, nil
106 },
107 // Controls connection reuse - false allows reuse, true forces new connections for each request
108 DisableKeepAlives: false,
109 // Maximum concurrent connections per host (active + idle)
110 MaxConnsPerHost: 10,
111 // Maximum idle connections maintained per host for reuse
112 MaxIdleConnsPerHost: 5,
113 // Maximum total idle connections across all hosts
114 MaxIdleConns: 20,
115 // How long an idle connection remains in the pool before being closed
116 IdleConnTimeout: 20 * time.Second,
117 }
118
119 // Create HTTP client with the isolated transport
120 httpClient := &http.Client{
121 Transport: transport,
122 Timeout: 30 * time.Second,
123 }
124 // client is the GitHub client being tested and is
125 // configured to use test server.
126 client = mustNewClient(t, WithHTTPClient(httpClient))
127
128 url, _ := url.Parse(server.URL + baseURLPath + "/")

Calls 4

mustNewClientFunction · 0.85
WithHTTPClientFunction · 0.85
StringMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…