/
goprana_test.go
142 lines (117 loc) · 2.63 KB
/
goprana_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2015 The goprana Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package goprana_test
import (
"encoding/json"
"net/http"
"testing"
"github.com/jroimartin/goprana"
)
const (
appName = "go-gotquotes"
vip = "go-gotquotes"
)
type quoteResp struct {
Quote string
Counter int
}
var reqChecks = map[string]quoteResp{
"/api/quote/1": quoteResp{
Quote: "You know nothing, Jon Snow.",
},
}
var propChecks = map[string]string{
"eureka.vipAddress": "go-gotquotes",
"eureka.port": "5000",
}
func TestDynamicProperties(t *testing.T) {
c := goprana.NewClient(goprana.DefaultPort)
ids := []string{}
for k := range propChecks {
ids = append(ids, k)
}
props, err := c.DynamicProperties(ids...)
if err != nil {
t.Fatal(err)
}
for id, want := range propChecks {
val, ok := props[id]
if !ok {
t.Errorf("property %v not found", id)
continue
}
if val != want {
t.Errorf("got property %v=%v, want %v", id, val, want)
}
}
}
func TestGet(t *testing.T) {
c := goprana.NewClient(goprana.DefaultPort)
for path, want := range reqChecks {
resp, err := c.Get(vip, path)
if err != nil {
t.Fatal(err)
}
dec := json.NewDecoder(resp.Body)
var r quoteResp
if err := dec.Decode(&r); err != nil {
t.Fatal(err)
}
resp.Body.Close()
if r.Quote != want.Quote {
t.Errorf("got quote %v, want %v", r.Quote, want.Quote)
}
if r.Counter <= 0 {
t.Errorf("got quote counter %v, want >0", r.Counter)
}
}
}
func TestPost(t *testing.T) {
c := goprana.NewClient(goprana.DefaultPort)
for path := range reqChecks {
resp, err := c.Post(vip, path, "", nil)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusNotImplemented {
t.Errorf("got status %v, want 501 Not Implemented", resp.StatusCode)
}
}
}
func TestDo(t *testing.T) {
c := goprana.NewClient(goprana.DefaultPort)
for path, want := range reqChecks {
req, err := http.NewRequest("GET", "", nil)
if err != nil {
t.Fatal(err)
}
resp, err := c.Do(vip, path, req)
if err != nil {
t.Fatal(err)
}
dec := json.NewDecoder(resp.Body)
var r quoteResp
if err := dec.Decode(&r); err != nil {
t.Fatal(err)
}
resp.Body.Close()
if r.Quote != want.Quote {
t.Errorf("got quote %v, want %v", r.Quote, want.Quote)
}
if r.Counter <= 0 {
t.Errorf("got quote counter %v, want >0", r.Counter)
}
}
}
func TestHosts(t *testing.T) {
c := goprana.NewClient(goprana.DefaultPort)
hosts, err := c.Hosts(appName, vip)
if err != nil {
t.Fatal(err)
}
if len(hosts) == 0 {
t.Error("got 0 hosts")
}
}