summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorBen Burwell <ben@benburwell.com>2019-09-18 14:07:39 -0400
committerBen Burwell <ben@benburwell.com>2019-09-18 14:07:39 -0400
commit1a0c9f2e5704ee5acc59252a62d329d586e5aee2 (patch)
tree29cfee15eaff218e050a4da9999d405a29d71af5 /client
parenta3a1e9decb6a5e36b76c9c7e1a149db81c60bd6e (diff)
Add client docs, test
Diffstat (limited to 'client')
-rw-r--r--client/client.go10
-rw-r--r--client/client_test.go17
2 files changed, 26 insertions, 1 deletions
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)
+ }
+}