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 5package elastic 6 7import ( 8 "net/url" 9 "testing" 10) 11 12func TestClusterStatsURLs(t *testing.T) { 13 fFlag := false 14 tFlag := true 15 16 tests := []struct { 17 Service *ClusterStatsService 18 ExpectedPath string 19 ExpectedParams url.Values 20 }{ 21 { 22 Service: &ClusterStatsService{ 23 nodeId: []string{}, 24 }, 25 ExpectedPath: "/_cluster/stats", 26 }, 27 { 28 Service: &ClusterStatsService{ 29 nodeId: []string{"node1"}, 30 }, 31 ExpectedPath: "/_cluster/stats/nodes/node1", 32 }, 33 { 34 Service: &ClusterStatsService{ 35 nodeId: []string{"node1", "node2"}, 36 }, 37 ExpectedPath: "/_cluster/stats/nodes/node1%2Cnode2", 38 }, 39 { 40 Service: &ClusterStatsService{ 41 nodeId: []string{}, 42 flatSettings: &tFlag, 43 }, 44 ExpectedPath: "/_cluster/stats", 45 ExpectedParams: url.Values{"flat_settings": []string{"true"}}, 46 }, 47 { 48 Service: &ClusterStatsService{ 49 nodeId: []string{"node1"}, 50 flatSettings: &fFlag, 51 }, 52 ExpectedPath: "/_cluster/stats/nodes/node1", 53 ExpectedParams: url.Values{"flat_settings": []string{"false"}}, 54 }, 55 } 56 57 for _, test := range tests { 58 gotPath, gotParams, err := test.Service.buildURL() 59 if err != nil { 60 t.Fatalf("expected no error; got: %v", err) 61 } 62 if gotPath != test.ExpectedPath { 63 t.Errorf("expected URL path = %q; got: %q", test.ExpectedPath, gotPath) 64 } 65 if gotParams.Encode() != test.ExpectedParams.Encode() { 66 t.Errorf("expected URL params = %v; got: %v", test.ExpectedParams, gotParams) 67 } 68 } 69} 70