From 679a62a9407c09be0cfb4e22455dca5ae694ce01 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Mon, 16 Sep 2019 15:56:31 -0400 Subject: Flesh out --- server/auth.go | 15 ++++++++++++++ server/main.go | 34 ++++++++++++++++++++++++++++++ server/main_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ server/server | Bin 0 -> 7349148 bytes 4 files changed, 107 insertions(+) create mode 100644 server/auth.go create mode 100644 server/main.go create mode 100644 server/main_test.go create mode 100755 server/server (limited to 'server') diff --git a/server/auth.go b/server/auth.go new file mode 100644 index 0000000..15ea164 --- /dev/null +++ b/server/auth.go @@ -0,0 +1,15 @@ +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 new file mode 100644 index 0000000..23f2959 --- /dev/null +++ b/server/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "io/ioutil" + "net/http" + + "bnbl.io/caesar" +) + +func main() { + server := handleCaesar(TokenAuthenticator()) + http.ListenAndServe(":8088", server) +} + +func handleCaesar(auth Authenticator) 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 + } + body, err := ioutil.ReadAll(r.Body) + defer r.Body.Close() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + encoded := caesar.Encode(string(body)) + w.Write([]byte(encoded)) + } +} diff --git a/server/main_test.go b/server/main_test.go new file mode 100644 index 0000000..75ddcdb --- /dev/null +++ b/server/main_test.go @@ -0,0 +1,58 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestHandleCaesarAllowed(t *testing.T) { + allowAll := func(r *http.Request) bool { return true } + bodyReader := strings.NewReader("hello world") + req := httptest.NewRequest("POST", "/", bodyReader) + resp := httptest.NewRecorder() + + handler := handleCaesar(allowAll) + handler(resp, req) + + if resp.Code != http.StatusOK { + t.Errorf("expected OK status %d but got %d", http.StatusOK, resp.Code) + } + + body := string(resp.Body.Bytes()) + if body != "khoor zruog" { + t.Errorf("got unexpected body %s", body) + } +} + +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(resp, req) + if resp.Code != http.StatusBadRequest { + t.Errorf("status should have been bad request, got %d", resp.Code) + } + if len(resp.Body.Bytes()) != 0 { + t.Errorf("should not have gotten a response body") + } +} diff --git a/server/server b/server/server new file mode 100755 index 0000000..979db70 Binary files /dev/null and b/server/server differ -- cgit v1.2.3