| 19 | ) |
| 20 | |
| 21 | func TestWorker(t *testing.T) { |
| 22 | runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) { |
| 23 | formData := url.Values{"baz": {"bat"}} |
| 24 | req := httptest.NewRequest("POST", "http://example.com/worker.php?foo=bar", strings.NewReader(formData.Encode())) |
| 25 | req.Header.Set("Content-Type", strings.Clone("application/x-www-form-urlencoded")) |
| 26 | w := httptest.NewRecorder() |
| 27 | handler(w, req) |
| 28 | |
| 29 | resp := w.Result() |
| 30 | body, _ := io.ReadAll(resp.Body) |
| 31 | |
| 32 | assert.Contains(t, string(body), fmt.Sprintf("Requests handled: %d", i*2)) |
| 33 | |
| 34 | formData2 := url.Values{"baz2": {"bat2"}} |
| 35 | req2 := httptest.NewRequest("POST", "http://example.com/worker.php?foo2=bar2", strings.NewReader(formData2.Encode())) |
| 36 | req2.Header.Set("Content-Type", strings.Clone("application/x-www-form-urlencoded")) |
| 37 | |
| 38 | w2 := httptest.NewRecorder() |
| 39 | handler(w2, req2) |
| 40 | |
| 41 | resp2 := w2.Result() |
| 42 | body2, _ := io.ReadAll(resp2.Body) |
| 43 | |
| 44 | assert.Contains(t, string(body2), fmt.Sprintf("Requests handled: %d", i*2+1)) |
| 45 | }, &testOptions{workerScript: "worker.php", nbWorkers: 1, nbParallelRequests: 1}) |
| 46 | } |
| 47 | |
| 48 | func TestWorkerDie(t *testing.T) { |
| 49 | runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) { |