From 1a0c9f2e5704ee5acc59252a62d329d586e5aee2 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Wed, 18 Sep 2019 14:07:39 -0400 Subject: Add client docs, test --- client/client.go | 10 +++++++++- client/client_test.go | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/client/client.go b/client/client.go index e39a6a1..af7c2d8 100644 --- a/client/client.go +++ b/client/client.go @@ -1,3 +1,5 @@ +// Package client makes requests to a Caesar Cipher Server in order to encode +// and decode messages. package client import ( @@ -9,14 +11,20 @@ import ( "time" ) +// A CaesarClient handles communication with a server type CaesarClient struct { + // Endpoint is the URL to which HTTP requests should be made Endpoint string - Token string + + // Token is an authorization token to supply to the server + Token string c *http.Client once sync.Once } +// EncodeMessage asks the Caesar server to encode the supplied message and +// returns the result along with an error in case the request fails. func (c *CaesarClient) EncodeMessage(r io.Reader) (io.Reader, error) { c.once.Do(func() { c.c = &http.Client{Timeout: 10 * time.Second} diff --git a/client/client_test.go b/client/client_test.go index b4a1b2a..9ccc149 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -64,3 +64,20 @@ func TestEncode(t *testing.T) { 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) + } +} -- cgit v1.2.3