Skip to content

Commit

Permalink
Extract inline vars from the status handler (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
mccutchen authored Nov 11, 2022
1 parent 7d0621f commit bd122ea
Showing 1 changed file with 47 additions and 46 deletions.
93 changes: 47 additions & 46 deletions httpbin/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ import (
"github.com/mccutchen/go-httpbin/v2/httpbin/digest"
)

var acceptedMediaTypes = []string{
"image/webp",
"image/svg+xml",
"image/jpeg",
"image/png",
"image/",
}

func notImplementedHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not implemented", http.StatusNotImplemented)
}
Expand Down Expand Up @@ -151,36 +143,29 @@ func (h *HTTPBin) Headers(w http.ResponseWriter, r *http.Request) {
writeJSON(w, body, http.StatusOK)
}

// Status responds with the specified status code. TODO: support random choice
// from multiple, optionally weighted status codes.
func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
if len(parts) != 3 {
http.Error(w, "Not found", http.StatusNotFound)
return
}
code, err := strconv.Atoi(parts[2])
if err != nil {
http.Error(w, "Invalid status", http.StatusBadRequest)
return
}

type statusCase struct {
headers map[string]string
body []byte
}
type statusCase struct {
headers map[string]string
body []byte
}

redirectHeaders := &statusCase{
var (
statusRedirectHeaders = &statusCase{
headers: map[string]string{
"Location": "/redirect/1",
},
}
notAcceptableBody, _ := jsonMarshalNoEscape(map[string]interface{}{
"message": "Client did not request a supported media type",
"accept": acceptedMediaTypes,
})

http300body := []byte(`<!doctype html>
statusNotAcceptableBody = []byte(`{
"message": "Client did not request a supported media type",
"accept": [
"image/webp",
"image/svg+xml",
"image/jpeg",
"image/png",
"image/"
]
}
`)
statusHTTP300body = []byte(`<!doctype html>
<head>
<title>Multiple Choices</title>
</head>
Expand All @@ -192,28 +177,28 @@ func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) {
</body>
</html>`)

http308body := []byte(`<!doctype html>
statusHTTP308Body = []byte(`<!doctype html>
<head>
<title>Permanent Redirect</title>
</head>
<body>Permanently redirected to <a href="/image/jpeg">/image/jpeg</a>
</body>
</html>`)

specialCases := map[int]*statusCase{
statusSpecialCases = map[int]*statusCase{
300: {
body: http300body,
body: statusHTTP300body,
headers: map[string]string{
"Location": "/image/jpeg",
},
},
301: redirectHeaders,
302: redirectHeaders,
303: redirectHeaders,
305: redirectHeaders,
307: redirectHeaders,
301: statusRedirectHeaders,
302: statusRedirectHeaders,
303: statusRedirectHeaders,
305: statusRedirectHeaders,
307: statusRedirectHeaders,
308: {
body: http308body,
body: statusHTTP308Body,
headers: map[string]string{
"Location": "/image/jpeg",
},
Expand All @@ -230,7 +215,7 @@ func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) {
},
},
406: {
body: notAcceptableBody,
body: statusNotAcceptableBody,
headers: map[string]string{
"Content-Type": jsonContentType,
},
Expand All @@ -247,18 +232,34 @@ func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) {
},
},
}
)

if specialCase, ok := specialCases[code]; ok {
// Status responds with the specified status code. TODO: support random choice
// from multiple, optionally weighted status codes.
func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
if len(parts) != 3 {
http.Error(w, "Not found", http.StatusNotFound)
return
}
code, err := strconv.Atoi(parts[2])
if err != nil {
http.Error(w, "Invalid status", http.StatusBadRequest)
return
}

if specialCase, ok := statusSpecialCases[code]; ok {
for key, val := range specialCase.headers {
w.Header().Set(key, val)
}
w.WriteHeader(code)
if specialCase.body != nil {
w.Write(specialCase.body)
}
} else {
w.WriteHeader(code)
return
}

w.WriteHeader(code)
}

// Unstable - returns 500, sometimes
Expand Down

0 comments on commit bd122ea

Please sign in to comment.