diff options
author | Ben Burwell <ben@benburwell.com> | 2019-09-23 22:25:22 -0400 |
---|---|---|
committer | Ben Burwell <ben@benburwell.com> | 2019-09-23 22:25:22 -0400 |
commit | aeb2100b5acc764f1e22eaa197525edf8bc7e6c7 (patch) | |
tree | 8d247a0c782500f718677a5257361f4cc34df5ac /client | |
parent | f312016e0b70f38b48e509472680bbf6cde64179 (diff) |
Update client
Diffstat (limited to 'client')
-rw-r--r-- | client/client.go | 21 | ||||
-rw-r--r-- | client/client_test.go | 24 |
2 files changed, 20 insertions, 25 deletions
diff --git a/client/client.go b/client/client.go index b68b2be..60d4461 100644 --- a/client/client.go +++ b/client/client.go @@ -3,7 +3,6 @@ package client import ( - "bytes" "fmt" "io" "net/http" @@ -20,28 +19,28 @@ type CaesarClient struct { 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) { +// EncodeMessage asks the Caesar server to encode the supplied message, writes +// the encoded message to the supplied writer, and returns an error in case the +// request fails. +func (c *CaesarClient) EncodeMessage(w io.Writer, r io.Reader) error { c.once.Do(func() { c.c = &http.Client{Timeout: 10 * time.Second} }) req, err := http.NewRequest("POST", c.Endpoint, r) if err != nil { - return nil, err + return err } req.Header.Set("user-agent", "caesar-client/1.0") resp, err := c.c.Do(req) if err != nil { - return nil, err + return err } if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("http status %s", resp.Status) + return fmt.Errorf("http status %s", resp.Status) } - var body bytes.Buffer defer resp.Body.Close() - if _, err := io.Copy(&body, resp.Body); err != nil { - return nil, err + if _, err := io.Copy(w, resp.Body); err != nil { + return err } - return &body, nil + return nil } diff --git a/client/client_test.go b/client/client_test.go index a724857..bfb8480 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -1,6 +1,7 @@ package client import ( + "bytes" "io/ioutil" "net/http" "net/http/httptest" @@ -14,32 +15,27 @@ func TestEncode(t *testing.T) { })) defer server.Close() client := CaesarClient{Endpoint: server.URL} // HL - r, err := client.EncodeMessage(strings.NewReader("secret message")) - if err != nil { + var b bytes.Buffer + if err := client.EncodeMessage(&b, strings.NewReader("secret message")); 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" { + resp := b.String() + if 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) { + server := httptest.NewServer(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")) + if err := c.EncodeMessage(ioutil.Discard, strings.NewReader("secret message")); err != nil { + t.Errorf("expected no error but got %v", err) + } expected := "caesar-client/1.0" if ua != expected { |