2831c8993d
* Move the watch package into the api module It was already just a thin wrapper around the API anyways. The biggest change was to the testing. Instead of using a test agent directly from the agent package it now uses the binary on the PATH just like the other API tests. The other big changes were to fix up the connect based watch tests so that we didn’t need to pull in the connect package (and therefore all of Consul)
67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package agent
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hashicorp/consul/api/watch"
|
|
)
|
|
|
|
func TestMakeWatchHandler(t *testing.T) {
|
|
t.Parallel()
|
|
defer os.Remove("handler_out")
|
|
defer os.Remove("handler_index_out")
|
|
script := "bash -c 'echo $CONSUL_INDEX >> handler_index_out && cat >> handler_out'"
|
|
handler := makeWatchHandler(os.Stderr, script)
|
|
handler(100, []string{"foo", "bar", "baz"})
|
|
raw, err := ioutil.ReadFile("handler_out")
|
|
if err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
if string(raw) != "[\"foo\",\"bar\",\"baz\"]\n" {
|
|
t.Fatalf("bad: %s", raw)
|
|
}
|
|
raw, err = ioutil.ReadFile("handler_index_out")
|
|
if err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
if string(raw) != "100\n" {
|
|
t.Fatalf("bad: %s", raw)
|
|
}
|
|
}
|
|
|
|
func TestMakeHTTPWatchHandler(t *testing.T) {
|
|
t.Parallel()
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
idx := r.Header.Get("X-Consul-Index")
|
|
if idx != "100" {
|
|
t.Fatalf("bad: %s", idx)
|
|
}
|
|
// Get the first one
|
|
customHeader := r.Header.Get("X-Custom")
|
|
if customHeader != "abc" {
|
|
t.Fatalf("bad: %s", idx)
|
|
}
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
if err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
if string(body) != "[\"foo\",\"bar\",\"baz\"]\n" {
|
|
t.Fatalf("bad: %s", body)
|
|
}
|
|
w.Write([]byte("Ok, i see"))
|
|
}))
|
|
defer server.Close()
|
|
config := watch.HttpHandlerConfig{
|
|
Path: server.URL,
|
|
Header: map[string][]string{"X-Custom": {"abc", "def"}},
|
|
Timeout: time.Minute,
|
|
}
|
|
handler := makeHTTPWatchHandler(os.Stderr, &config)
|
|
handler(100, []string{"foo", "bar", "baz"})
|
|
}
|