Skip to content

Commit

Permalink
handle missing x-response-json
Browse files Browse the repository at this point in the history
handle missing x-response-json
  • Loading branch information
notnmeyer committed Oct 23, 2023
1 parent 59f35ae commit ac6db4f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
38 changes: 34 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,53 @@ func main() {
func handler(w http.ResponseWriter, r *http.Request) {
var (
contentType = "application/json; charset=utf-8"
responseBody = r.Header["X-Response-Json"][0]
responseBody string
responseCode int
)

responseCode, err := validateResponseCode(r.Header)
err := validateResponseBody(r.Header)
if err == nil {
responseBody = r.Header["X-Response-Json"][0]
} else {
responseBody = err.Error()
responseCode = http.StatusBadRequest
writeRequest(w, contentType, responseBody, responseCode)
return
}

responseCode, err = validateResponseCode(r.Header)
if err != nil {
responseBody = err.Error()
} else if !isJSON(responseBody) {
responseBody = errorResponseFormatter("x-response-json must be valid JSON").Error()
responseCode = http.StatusBadRequest
writeRequest(w, contentType, responseBody, responseCode)
return
}

writeRequest(w, contentType, responseBody, responseCode)

// w.Header().Set("Content-Type", contentType)
// w.WriteHeader(responseCode)
// w.Write([]byte(responseBody))
}

func writeRequest(w http.ResponseWriter, contentType string, responseBody string, responseCode int) {
w.Header().Set("Content-Type", contentType)
w.WriteHeader(responseCode)
w.Write([]byte(responseBody))
}

func validateResponseBody(header map[string][]string) error {
if _, exists := header["X-Response-Json"]; !exists {
return errorResponseFormatter("x-response-json must be set on the request")
}

if !isJSON(header["X-Response-Json"][0]) {
return errorResponseFormatter("x-response-json must be valid JSON")
}

return nil
}

func validateResponseCode(header map[string][]string) (int, error) {
if val, exists := header["X-Response-Code"]; exists {
// verify its a number
Expand Down
26 changes: 26 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ func TestHandlerValidRequest(t *testing.T) {
}
}

func TestHandlerWithoutXResponseJson(t *testing.T) {
expectedResponseBody := `{"error":"x-response-json must be set on the request"}`
expectedResponseCode := http.StatusBadRequest

// set up the request
req, err := http.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}

// make the request
rr := httptest.NewRecorder()
h := http.HandlerFunc(handler)
h.ServeHTTP(rr, req)

// status code
if status := rr.Code; status != expectedResponseCode {
t.Errorf("handler returned wrong status code: got '%d' want '%d'", status, expectedResponseCode)
}

// response body
if rr.Body.String() != expectedResponseBody {
t.Errorf("handler returned unexpected body: got '%s' want '%s'", rr.Body.String(), expectedResponseBody)
}
}

func TestHandlerWithInvalidXResponseJson(t *testing.T) {
invalidResponseBody := `{"foo":bar}`
expectedResponseBody := `{"error":"x-response-json must be valid JSON"}`
Expand Down

0 comments on commit ac6db4f

Please sign in to comment.