(t *testing.T)
| 160 | } |
| 161 | |
| 162 | func 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 |
nothing calls this directly
no test coverage detected