|
| 1 | +package main |
| 2 | + |
| 3 | +// These tests exercise HTTPServer's HTTP handler methods directly via |
| 4 | +// net/http/httptest, bypassing Run(). Run() itself (unix socket setup, PID |
| 5 | +// file management, OS signal handling) and handleShutdownRequest (which |
| 6 | +// calls os.Exit) are integration-level, OS-facing plumbing that would |
| 7 | +// require killing or replacing the test process itself to exercise safely, |
| 8 | +// so they're intentionally left uncovered here. |
| 9 | + |
| 10 | +import ( |
| 11 | + "net/http" |
| 12 | + "net/http/httptest" |
| 13 | + "net/url" |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +func TestHandleVersionRequest(t *testing.T) { |
| 21 | + control := newFakeSimulatorControl() |
| 22 | + sm := newTestManager(control, 0, 0, false, nil) |
| 23 | + server := NewHTTPServer(NewSimulatorRequestHandler(sm), "v1.2.3") |
| 24 | + |
| 25 | + req := httptest.NewRequest(http.MethodGet, "/version", nil) |
| 26 | + rec := httptest.NewRecorder() |
| 27 | + |
| 28 | + server.handleVersionRequest(rec, req) |
| 29 | + |
| 30 | + assert.Equal(t, http.StatusOK, rec.Code) |
| 31 | + assert.Equal(t, "v1.2.3\n", rec.Body.String()) |
| 32 | +} |
| 33 | + |
| 34 | +func TestHandleLeasesRequest(t *testing.T) { |
| 35 | + control := newFakeSimulatorControl() |
| 36 | + sm := newTestManager(control, 0, 0, false, nil) |
| 37 | + server := NewHTTPServer(NewSimulatorRequestHandler(sm), "v1") |
| 38 | + |
| 39 | + req := httptest.NewRequest(http.MethodGet, "/leases", nil) |
| 40 | + rec := httptest.NewRecorder() |
| 41 | + |
| 42 | + server.handleLeasesRequest(rec, req) |
| 43 | + |
| 44 | + assert.Equal(t, http.StatusOK, rec.Code) |
| 45 | + assert.Equal(t, "0\n", rec.Body.String()) |
| 46 | +} |
| 47 | + |
| 48 | +func TestHandleSimulatorRequest_RoutesToHandler(t *testing.T) { |
| 49 | + control := newFakeSimulatorControl() |
| 50 | + sm := newTestManager(control, 0, 0, false, nil) |
| 51 | + server := NewHTTPServer(NewSimulatorRequestHandler(sm), "v1") |
| 52 | + |
| 53 | + params := url.Values{"exclusive": {"1"}, "deviceType": {"iPhone"}, "os": {"iOS"}, "version": {"17.0"}} |
| 54 | + req := httptest.NewRequest(http.MethodPost, "/simulator/1?"+params.Encode(), nil) |
| 55 | + rec := httptest.NewRecorder() |
| 56 | + |
| 57 | + server.handleSimulatorRequest(rec, req) |
| 58 | + |
| 59 | + require.Equal(t, http.StatusCreated, rec.Code) |
| 60 | + assert.NotEmpty(t, rec.Body.String()) |
| 61 | +} |
| 62 | + |
| 63 | +func TestHandleSimulatorRequest_BadRequestSurfacesMessage(t *testing.T) { |
| 64 | + control := newFakeSimulatorControl() |
| 65 | + sm := newTestManager(control, 0, 0, false, nil) |
| 66 | + server := NewHTTPServer(NewSimulatorRequestHandler(sm), "v1") |
| 67 | + |
| 68 | + req := httptest.NewRequest(http.MethodDelete, "/simulator/1", nil) |
| 69 | + rec := httptest.NewRecorder() |
| 70 | + |
| 71 | + server.handleSimulatorRequest(rec, req) |
| 72 | + |
| 73 | + assert.Equal(t, http.StatusNotFound, rec.Code) |
| 74 | + assert.Contains(t, rec.Body.String(), "doesn't have a simulator leased") |
| 75 | +} |
0 commit comments