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