summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
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)
+ }
+}