summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorBen Burwell <ben@benburwell.com>2019-09-16 15:56:31 -0400
committerBen Burwell <ben@benburwell.com>2019-09-16 16:29:16 -0400
commit679a62a9407c09be0cfb4e22455dca5ae694ce01 (patch)
treea99e8d61ada1677aee6f63a78545463308c5badd /server
parent528352cddbe0290653c56a27a4134637ad0624e5 (diff)
Flesh out
Diffstat (limited to 'server')
-rw-r--r--server/auth.go15
-rw-r--r--server/main.go34
-rw-r--r--server/main_test.go58
-rwxr-xr-xserver/serverbin0 -> 7349148 bytes
4 files changed, 107 insertions, 0 deletions
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
--- /dev/null
+++ b/server/server
Binary files differ