Skip to content

Commit

Permalink
Merge pull request #17 from Comcast/fix/only-post-method
Browse files Browse the repository at this point in the history
Fix/only post method
  • Loading branch information
schmidtw authored May 24, 2017
2 parents 18d42ac + 4a179f5 commit 91e1ad3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/caduceus/caduceus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func TestServerHandler(t *testing.T) {
}

req := httptest.NewRequest("POST", "localhost:8080", strings.NewReader("Test payload."))
badReq := httptest.NewRequest("GET", "localhost:8080", strings.NewReader("Test payload."))

t.Run("TestServeHTTPHappyPath", func(t *testing.T) {
req.Header.Set("Content-Type", "application/json")
Expand All @@ -151,6 +152,18 @@ func TestServerHandler(t *testing.T) {
fakeHealth.AssertExpectations(t)
})

t.Run("TestServeHTTPBadMethod", func(t *testing.T) {
badReq.Header.Set("Content-Type", "application/json")

w := httptest.NewRecorder()
serverWrapper.ServeHTTP(w, badReq)
resp := w.Result()

assert.Equal(400, resp.StatusCode)
fakeHandler.AssertExpectations(t)
fakeHealth.AssertExpectations(t)
})

t.Run("TestServeHTTPTooManyHeaders", func(t *testing.T) {
req.Header.Add("Content-Type", "too/many/headers")

Expand Down
12 changes: 9 additions & 3 deletions src/caduceus/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"fmt"
"github.com/Comcast/webpa-common/logging"
"io/ioutil"
"net/http"
Expand All @@ -21,17 +22,22 @@ type ServerHandler struct {
func (sh *ServerHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
defer request.Body.Close()

sh.Info("Receiving incoming post...")
sh.Info("Receiving incoming request...")

timeStamps := CaduceusTimestamps{
TimeReceived: time.Now(),
}

if request.Method != "POST" {
response.WriteHeader(http.StatusBadRequest)
response.Write([]byte(fmt.Sprintf("Unsupported method \"%s\"... Caduceus only supports \"POST\" method.\n", request.Method)))
return
}

myPayload, err := ioutil.ReadAll(request.Body)
if err != nil {
statusMsg := "Unable to retrieve the request body: " + err.Error() + ".\n"
response.WriteHeader(http.StatusBadRequest)
response.Write([]byte(statusMsg))
response.Write([]byte(fmt.Sprintf("Unable to retrieve the request body: %s.\n", err.Error)))
return
}

Expand Down

0 comments on commit 91e1ad3

Please sign in to comment.