| 9 | ) |
| 10 | |
| 11 | func TestChecker(t *testing.T) { |
| 12 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 13 | w.Header().Set("X-Checkup", r.Header.Get("X-Checkup")) |
| 14 | fmt.Fprintln(w, "I'm up", "@"+r.Host) |
| 15 | })) |
| 16 | endpt := "http://" + srv.Listener.Addr().String() |
| 17 | hc := Checker{Name: "Test", URL: endpt, Attempts: 2} |
| 18 | |
| 19 | // Try an up server |
| 20 | result, err := hc.Check() |
| 21 | if err != nil { |
| 22 | t.Errorf("Didn't expect an error: %v", err) |
| 23 | } |
| 24 | if got, want := result.Title, "Test"; got != want { |
| 25 | t.Errorf("Expected result.Title='%s', got '%s'", want, got) |
| 26 | } |
| 27 | if got, want := result.Endpoint, endpt; got != want { |
| 28 | t.Errorf("Expected result.Endpoint='%s', got '%s'", want, got) |
| 29 | } |
| 30 | if got, want := result.Down, false; got != want { |
| 31 | t.Errorf("Expected result.Down=%v, got %v", want, got) |
| 32 | } |
| 33 | if got, want := result.Degraded, false; got != want { |
| 34 | t.Errorf("Expected result.Degraded=%v, got %v", want, got) |
| 35 | } |
| 36 | if got, want := result.Healthy, true; got != want { |
| 37 | t.Errorf("Expected result.Healthy=%v, got %v", want, got) |
| 38 | } |
| 39 | if got, want := len(result.Times), hc.Attempts; got != want { |
| 40 | t.Errorf("Expected %d attempts, got %d", want, got) |
| 41 | } |
| 42 | ts := time.Unix(0, result.Timestamp) |
| 43 | if time.Since(ts) > 5*time.Second { |
| 44 | t.Errorf("Expected timestamp to be recent, got %s", ts) |
| 45 | } |
| 46 | |
| 47 | // Try various different down criteria |
| 48 | |
| 49 | hc.UpStatus = 201 |
| 50 | result, err = hc.Check() |
| 51 | if err != nil { |
| 52 | t.Errorf("Didn't expect an error: %v", err) |
| 53 | } |
| 54 | if got, want := result.Down, true; got != want { |
| 55 | t.Errorf("Expected result.Down=%v, got %v", want, got) |
| 56 | } |
| 57 | |
| 58 | hc.UpStatus = 200 |
| 59 | hc.ThresholdRTT = 1 * time.Nanosecond |
| 60 | result, err = hc.Check() |
| 61 | if err != nil { |
| 62 | t.Errorf("Didn't expect an error: %v", err) |
| 63 | } |
| 64 | if got, want := result.Degraded, true; got != want { |
| 65 | t.Errorf("Expected result.Down=%v, got %v", want, got) |
| 66 | } |
| 67 | |
| 68 | hc.ThresholdRTT = 0 |