consul/api/watch/watch_test.go

51 lines
1.0 KiB
Go
Raw Normal View History

2014-08-20 18:19:43 +00:00
package watch
import (
2014-08-21 18:38:44 +00:00
"bytes"
"encoding/json"
2014-08-20 18:19:43 +00:00
"testing"
)
func TestParseBasic(t *testing.T) {
2017-10-24 13:24:57 +00:00
t.Parallel()
2014-08-21 18:38:44 +00:00
params := makeParams(t, `{"type":"key", "datacenter":"dc2", "token":"12345", "key":"foo"}`)
p, err := Parse(params)
2014-08-20 18:19:43 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if p.Datacenter != "dc2" {
t.Fatalf("Bad: %#v", p)
}
if p.Token != "12345" {
t.Fatalf("Bad: %#v", p)
}
if p.Type != "key" {
t.Fatalf("Bad: %#v", p)
}
}
2014-08-20 23:38:15 +00:00
func TestParse_exempt(t *testing.T) {
2017-10-24 13:24:57 +00:00
t.Parallel()
2014-08-21 18:38:44 +00:00
params := makeParams(t, `{"type":"key", "key":"foo", "handler": "foobar"}`)
p, err := ParseExempt(params, []string{"handler"})
2014-08-20 23:38:15 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if p.Type != "key" {
t.Fatalf("Bad: %#v", p)
}
ex := p.Exempt["handler"]
2014-08-21 18:38:44 +00:00
if ex != "foobar" {
2014-08-20 23:38:15 +00:00
t.Fatalf("bad: %v", ex)
}
}
2014-08-21 18:38:44 +00:00
func makeParams(t *testing.T, s string) map[string]interface{} {
var out map[string]interface{}
dec := json.NewDecoder(bytes.NewReader([]byte(s)))
if err := dec.Decode(&out); err != nil {
t.Fatalf("err: %v", err)
}
return out
}