ping_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2012-present 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. "context"
  7. "net/http"
  8. "testing"
  9. )
  10. func TestPingGet(t *testing.T) {
  11. client := setupTestClientAndCreateIndex(t)
  12. res, code, err := client.Ping(DefaultURL).Do(context.TODO())
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. if code != http.StatusOK {
  17. t.Errorf("expected status code = %d; got %d", http.StatusOK, code)
  18. }
  19. if res == nil {
  20. t.Fatalf("expected to return result, got: %v", res)
  21. }
  22. if res.Name == "" {
  23. t.Errorf("expected Name != \"\"; got %q", res.Name)
  24. }
  25. if res.Version.Number == "" {
  26. t.Errorf("expected Version.Number != \"\"; got %q", res.Version.Number)
  27. }
  28. }
  29. func TestPingHead(t *testing.T) {
  30. client := setupTestClientAndCreateIndex(t)
  31. res, code, err := client.Ping(DefaultURL).HttpHeadOnly(true).Do(context.TODO())
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. if code != http.StatusOK {
  36. t.Errorf("expected status code = %d; got %d", http.StatusOK, code)
  37. }
  38. if res != nil {
  39. t.Errorf("expected not to return result, got: %v", res)
  40. }
  41. }
  42. func TestPingHeadFailure(t *testing.T) {
  43. client := setupTestClientAndCreateIndex(t)
  44. res, code, err := client.
  45. Ping("http://127.0.0.1:9299").
  46. HttpHeadOnly(true).
  47. Do(context.TODO())
  48. if err == nil {
  49. t.Error("expected error, got nil")
  50. }
  51. if code == http.StatusOK {
  52. t.Errorf("expected status code != %d; got %d", http.StatusOK, code)
  53. }
  54. if res != nil {
  55. t.Errorf("expected not to return result, got: %v", res)
  56. }
  57. }