ping_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2012-2015 Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "net/http"
  7. "testing"
  8. )
  9. func TestPingGet(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t)
  11. res, code, err := client.Ping().Do()
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. if code != http.StatusOK {
  16. t.Errorf("expected status code = %d; got %d", http.StatusOK, code)
  17. }
  18. if res == nil {
  19. t.Fatalf("expected to return result, got: %v", res)
  20. }
  21. if res.Status != http.StatusOK {
  22. t.Errorf("expected Status = %d; got %d", http.StatusOK, res.Status)
  23. }
  24. if res.Name == "" {
  25. t.Errorf("expected Name != \"\"; got %q", res.Name)
  26. }
  27. if res.Version.Number == "" {
  28. t.Errorf("expected Version.Number != \"\"; got %q", res.Version.Number)
  29. }
  30. }
  31. func TestPingHead(t *testing.T) {
  32. client := setupTestClientAndCreateIndex(t)
  33. res, code, err := client.Ping().HttpHeadOnly(true).Do()
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. if code != http.StatusOK {
  38. t.Errorf("expected status code = %d; got %d", http.StatusOK, code)
  39. }
  40. if res != nil {
  41. t.Errorf("expected not to return result, got: %v", res)
  42. }
  43. }
  44. func TestPingHeadFailure(t *testing.T) {
  45. client := setupTestClientAndCreateIndex(t)
  46. res, code, err := client.Ping().
  47. URL("http://127.0.0.1:9299").
  48. HttpHeadOnly(true).
  49. Do()
  50. if err == nil {
  51. t.Error("expected error, got nil")
  52. }
  53. if code == http.StatusOK {
  54. t.Errorf("expected status code != %d; got %d", http.StatusOK, code)
  55. }
  56. if res != nil {
  57. t.Errorf("expected not to return result, got: %v", res)
  58. }
  59. }