Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
resp := rec.Result()

if !IsSuccess(resp) || !IsJSON(resp) {
h.write(w, resp)
h.write(w, NewJsonResponse(http.StatusOK, resp.Header, route.Default))
return
}

Expand Down
95 changes: 88 additions & 7 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ func TestHandler_RouteNotMatched_PassesThrough(t *testing.T) {
assert.Equal(t, "ok", rr.Body.String())
}

func TestHandler_Not2xx_PassesThrough(t *testing.T) {
func TestHandler_Not2xx_JSON_ReturnsDefaults(t *testing.T) {
cfg := &Config{
Routes: []Route{
{Path: "/products/{product}/positions"},
{
Path: "/products/{product}/positions",
Default: map[string]interface{}{"positions": []interface{}{}},
},
},
}

Expand All @@ -69,14 +72,90 @@ func TestHandler_Not2xx_PassesThrough(t *testing.T) {

rr := doRequest(t, h, "GET", "/products/iphonex64s/positions")

assert.Equal(t, http.StatusInternalServerError, rr.Code)
assert.Equal(t, `{"error":"boom"}`, rr.Body.String())
assert.Equal(t, http.StatusOK, rr.Code)

out := decodeJSON(t, rr.Body.Bytes())
assert.Equal(t, []interface{}{}, out["positions"])
_, hasError := out["error"]
assert.False(t, hasError)
}

func TestHandler_NotJSON_PassesThrough(t *testing.T) {
func TestHandler_401_JSON_ReturnsDefaults(t *testing.T) {
cfg := &Config{
Routes: []Route{
{Path: "/products/{product}/positions"},
{
Path: "/products/{product}/positions",
Default: map[string]interface{}{"positions": []interface{}{}, "shops": nil},
},
},
}

next := newNext(401, "application/json", `{"error":"unauthorized"}`)
h := NewHandler(cfg, next, noopLogger{})

rr := doRequest(t, h, "GET", "/products/iphonex64s/positions")

assert.Equal(t, http.StatusOK, rr.Code)

out := decodeJSON(t, rr.Body.Bytes())
_, ok := out["positions"]
assert.True(t, ok)
_, ok = out["shops"]
assert.True(t, ok)
_, hasError := out["error"]
assert.False(t, hasError)
}

func TestHandler_403_JSON_ReturnsDefaults(t *testing.T) {
cfg := &Config{
Routes: []Route{
{
Path: "/products/{product}/positions",
Default: map[string]interface{}{"positions": []interface{}{}, "extra": "default_val"},
},
},
}

next := newNext(403, "application/json", `{"error":"forbidden","positions":[1,2]}`)
h := NewHandler(cfg, next, noopLogger{})

rr := doRequest(t, h, "GET", "/products/iphonex64s/positions")

assert.Equal(t, http.StatusOK, rr.Code)

out := decodeJSON(t, rr.Body.Bytes())
assert.Equal(t, []interface{}{}, out["positions"])
assert.Equal(t, "default_val", out["extra"])
}

func TestHandler_Not2xx_NotJSON_ReturnsDefaults(t *testing.T) {
cfg := &Config{
Routes: []Route{
{
Path: "/products/{product}/positions",
Default: map[string]interface{}{"positions": []interface{}{}},
},
},
}

next := newNext(500, "text/plain", "internal error")
h := NewHandler(cfg, next, noopLogger{})

rr := doRequest(t, h, "GET", "/products/iphonex64s/positions")

assert.Equal(t, http.StatusOK, rr.Code)

out := decodeJSON(t, rr.Body.Bytes())
assert.Equal(t, []interface{}{}, out["positions"])
}

func TestHandler_NotJSON_ReturnsDefaults(t *testing.T) {
cfg := &Config{
Routes: []Route{
{
Path: "/products/{product}/positions",
Default: map[string]interface{}{"positions": []interface{}{}},
},
},
}

Expand All @@ -86,7 +165,9 @@ func TestHandler_NotJSON_PassesThrough(t *testing.T) {
rr := doRequest(t, h, "GET", "/products/iphonex64s/positions")

assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, "hello", rr.Body.String())

out := decodeJSON(t, rr.Body.Bytes())
assert.Equal(t, []interface{}{}, out["positions"])
}

func TestHandler_JSONSuccess_AddsDefaults(t *testing.T) {
Expand Down
Loading