summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Burwell <ben@benburwell.com>2019-09-20 16:46:10 -0400
committerBen Burwell <ben@benburwell.com>2019-09-20 16:46:10 -0400
commite1f8ea2a889494859a61a9259110e40c70002d55 (patch)
tree52a96d1e80840512cac11449b549ceada1f24ef1
parente1eee80afa1ddf579aea52435c69813459ce3811 (diff)
Simplify examples
-rw-r--r--client/client.go4
-rw-r--r--client/client_test.go36
-rw-r--r--server/auth.go15
-rw-r--r--server/main.go8
-rw-r--r--server/main_test.go25
-rwxr-xr-xserver/serverbin7349148 -> 7349116 bytes
6 files changed, 6 insertions, 82 deletions
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
--- a/server/server
+++ b/server/server
Binary files differ