From e1f8ea2a889494859a61a9259110e40c70002d55 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Fri, 20 Sep 2019 16:46:10 -0400 Subject: Simplify examples --- client/client.go | 4 ---- client/client_test.go | 36 +----------------------------------- server/auth.go | 15 --------------- server/main.go | 8 ++------ server/main_test.go | 25 +++---------------------- server/server | Bin 7349148 -> 7349116 bytes 6 files changed, 6 insertions(+), 82 deletions(-) delete mode 100644 server/auth.go diff --git a/client/client.go b/client/client.go index af7c2d8..b68b2be 100644 --- a/client/client.go +++ b/client/client.go @@ -16,9 +16,6 @@ type CaesarClient struct { // Endpoint is the URL to which HTTP requests should be made Endpoint string - // Token is an authorization token to supply to the server - Token string - c *http.Client once sync.Once } @@ -33,7 +30,6 @@ func (c *CaesarClient) EncodeMessage(r io.Reader) (io.Reader, error) { if err != nil { return nil, err } - req.Header.Set("authorization", "token "+c.Token) req.Header.Set("user-agent", "caesar-client/1.0") resp, err := c.c.Do(req) if err != nil { diff --git a/client/client_test.go b/client/client_test.go index 9ccc149..b6af2b9 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -8,40 +8,6 @@ import ( "testing" ) -func TestBadAuth(t *testing.T) { - var hf http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) { - if r.Header.Get("authorization") != "token TEST-TOKEN" { - w.WriteHeader(http.StatusUnauthorized) - return - } - w.Write([]byte("encoded yay")) - } - tests := []struct { - name string - tok string - expectErr bool - expectBody bool - }{ - {"valid token", "TEST-TOKEN", false, true}, - {"empty token", "", true, false}, - {"invalid token", "bad token", true, false}, - } - server := httptest.NewServer(hf) - defer server.Close() - c := CaesarClient{Endpoint: server.URL} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - r, err := c.EncodeMessage(strings.NewReader("secret message")) - if err == nil { - t.Errorf("should have gotten error") - } - if r != nil { - t.Errorf("should not have gotten a reader") - } - }) - } -} - func TestEncode(t *testing.T) { var hf http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("response")) @@ -54,7 +20,7 @@ func TestEncode(t *testing.T) { t.Errorf("should not have gotten error: %v", err) } if r == nil { - t.Errorf("should have gotten a reader") + t.Fatalf("should have gotten a reader") } resp, err := ioutil.ReadAll(r) if err != nil { diff --git a/server/auth.go b/server/auth.go deleted file mode 100644 index 15ea164..0000000 --- a/server/auth.go +++ /dev/null @@ -1,15 +0,0 @@ -package main - -import ( - "net/http" -) - -// An Authenticator takes an HTTP request and returns true iff it is allowed to -// use our service. -type Authenticator func(req *http.Request) bool - -func TokenAuthenticator() Authenticator { - return func(req *http.Request) bool { - return req.Header.Get("authorization") == "magic" - } -} diff --git a/server/main.go b/server/main.go index 23f2959..9ebcb22 100644 --- a/server/main.go +++ b/server/main.go @@ -8,16 +8,12 @@ import ( ) func main() { - server := handleCaesar(TokenAuthenticator()) + server := handleCaesar() http.ListenAndServe(":8088", server) } -func handleCaesar(auth Authenticator) http.HandlerFunc { +func handleCaesar() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - if !auth(r) { - w.WriteHeader(http.StatusUnauthorized) - return - } if r.Method != http.MethodPost { w.WriteHeader(http.StatusBadRequest) return diff --git a/server/main_test.go b/server/main_test.go index 75ddcdb..e588ae1 100644 --- a/server/main_test.go +++ b/server/main_test.go @@ -7,13 +7,12 @@ import ( "testing" ) -func TestHandleCaesarAllowed(t *testing.T) { - allowAll := func(r *http.Request) bool { return true } +func TestHandleCaesar(t *testing.T) { bodyReader := strings.NewReader("hello world") req := httptest.NewRequest("POST", "/", bodyReader) resp := httptest.NewRecorder() - handler := handleCaesar(allowAll) + handler := handleCaesar() handler(resp, req) if resp.Code != http.StatusOK { @@ -26,28 +25,10 @@ func TestHandleCaesarAllowed(t *testing.T) { } } -func TestCaesarNotAllowed(t *testing.T) { - denyAll := func(r *http.Request) bool { return false } - req := httptest.NewRequest("POST", "/", nil) - resp := httptest.NewRecorder() - - handler := handleCaesar(denyAll) - handler(resp, req) - - if resp.Code != http.StatusUnauthorized { - t.Errorf("should not have been authorized") - } - - if len(resp.Body.Bytes()) != 0 { - t.Errorf("should not have gotten a response body") - } -} - func TestCaesarBadRequest(t *testing.T) { - allowAll := func(r *http.Request) bool { return true } req := httptest.NewRequest("GET", "/", nil) resp := httptest.NewRecorder() - handler := handleCaesar(allowAll) + handler := handleCaesar() handler(resp, req) if resp.Code != http.StatusBadRequest { t.Errorf("status should have been bad request, got %d", resp.Code) diff --git a/server/server b/server/server index 979db70..25d8f3f 100755 Binary files a/server/server and b/server/server differ -- cgit v1.2.3