MCPcopy Create free account
hub / github.com/NVIDIA/nvcf / TestHTTPServiceStart

Function TestHTTPServiceStart

src/libraries/go/lib/pkg/core/httpservice_test.go:162–250  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

160}
161
162func TestHTTPServiceStart(t *testing.T) {
163 ctx := WithDefaultLogger(context.Background())
164
165 get := func(c *http.Client, url string) (string, int) {
166 resp, err := c.Get(url)
167 require.NoError(t, err)
168 body, err := io.ReadAll(resp.Body)
169 require.NoError(t, err)
170 return string(body), resp.StatusCode
171 }
172
173 {
174 ctx, cancel := context.WithCancel(ctx)
175
176 socketDir, err := os.MkdirTemp("/tmp", "nvcf")
177 require.NoError(t, err)
178 t.Cleanup(func() { os.RemoveAll(socketDir) })
179 socketPath := filepath.Join(socketDir, "s.sock")
180
181 s := NewHTTPService("unix://" + socketPath)
182 s.AddHealthRoute(ctx)
183 s.Use(NewHTTPMiddleware(ctx)...)
184 s.Start(ctx)
185
186 c := &http.Client{
187 Timeout: 10 * time.Second,
188 Transport: &http.Transport{
189 DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
190 return net.Dial("unix", socketPath)
191 },
192 },
193 }
194 body, code := get(c, "http://localhost/healthz")
195 assert.Equal(t, "ok\n", body)
196 assert.Equal(t, http.StatusOK, code)
197
198 cancel()
199 }
200
201 {
202 ctx, cancel := context.WithCancel(ctx)
203 s := NewHTTPService("0.0.0.0:0")
204 s.AddHealthRoute(ctx)
205 s.Use(NewHTTPMiddleware(ctx)...)
206 ln, err := s.Start(ctx)
207 assert.NoError(t, err)
208
209 t.Logf("addr: %s", ln.Addr().(*net.TCPAddr).String())
210 addr := ln.Addr().(*net.TCPAddr).String()
211
212 c := &http.Client{Timeout: 10 * time.Second}
213 body, code := get(c, fmt.Sprintf("http://%s/healthz", addr))
214 assert.Equal(t, "ok\n", body)
215 assert.Equal(t, http.StatusOK, code)
216
217 cancel()
218 }
219

Callers

nothing calls this directly

Calls 13

AddHealthRouteMethod · 0.95
StartMethod · 0.95
WithDefaultLoggerFunction · 0.85
NewHTTPServiceFunction · 0.85
NewHTTPMiddlewareFunction · 0.85
certSetupFunction · 0.85
LogfMethod · 0.80
AddrMethod · 0.80
GetMethod · 0.65
CleanupMethod · 0.65
DialMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected