summaryrefslogtreecommitdiff
path: root/client/client_test.go
blob: b6af2b96b7402eaf319f130aa2b4be572969ae61 (plain)
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
package client

import (
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

func TestEncode(t *testing.T) {
	var hf http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("response"))
	}
	server := httptest.NewServer(hf)
	defer server.Close()
	c := CaesarClient{Endpoint: server.URL} // HL
	r, err := c.EncodeMessage(strings.NewReader("secret message"))
	if err != nil {
		t.Errorf("should not have gotten error: %v", err)
	}
	if r == nil {
		t.Fatalf("should have gotten a reader")
	}
	resp, err := ioutil.ReadAll(r)
	if err != nil {
		t.Fatalf("could not read message: %v", err)
	}
	if string(resp) != "response" {
		t.Errorf("expected 'response' but got '%s'", resp)
	}
}

func TestUserAgent(t *testing.T) {
	var ua string
	var hf http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
		ua = r.Header.Get("user-agent")
	}
	server := httptest.NewServer(hf)
	defer server.Close()

	c := CaesarClient{Endpoint: server.URL}
	c.EncodeMessage(strings.NewReader("secret message"))

	expected := "caesar-client/1.0"
	if ua != expected {
		t.Errorf("expected user agent header %q but got %q", expected, ua)
	}
}