Skip to content

Commit 8fb8afe

Browse files
author
Dan Laine
authored
Use http.Error instead of separately writing error code and message (#1564)
1 parent e8b6a5e commit 8fb8afe

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

api/server/server.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,7 @@ func lockMiddleware(
377377
func rejectMiddleware(handler http.Handler, ctx *snow.ConsensusContext) http.Handler {
378378
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // If chain isn't done bootstrapping, ignore API calls
379379
if ctx.State.Get().State != snow.NormalOp {
380-
w.WriteHeader(http.StatusServiceUnavailable)
381-
// Doesn't matter if there's an error while writing. They'll get the StatusServiceUnavailable code.
382-
_, _ = w.Write([]byte("API call rejected because chain is not done bootstrapping"))
380+
http.Error(w, "API call rejected because chain is not done bootstrapping", http.StatusServiceUnavailable)
383381
} else {
384382
handler.ServeHTTP(w, r)
385383
}

api/server/server_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package server
5+
6+
import (
7+
"net/http"
8+
"net/http/httptest"
9+
"testing"
10+
11+
"github.com/stretchr/testify/require"
12+
13+
"github.com/ava-labs/avalanchego/snow"
14+
)
15+
16+
func TestRejectMiddleware(t *testing.T) {
17+
type test struct {
18+
name string
19+
handlerFunc func(*require.Assertions) http.Handler
20+
state snow.State
21+
expectedStatusCode int
22+
}
23+
24+
tests := []test{
25+
{
26+
name: "chain is state syncing",
27+
handlerFunc: func(require *require.Assertions) http.Handler {
28+
return http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
29+
require.Fail("shouldn't have called handler")
30+
})
31+
},
32+
state: snow.StateSyncing,
33+
expectedStatusCode: http.StatusServiceUnavailable,
34+
},
35+
{
36+
name: "chain is bootstrapping",
37+
handlerFunc: func(require *require.Assertions) http.Handler {
38+
return http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
39+
require.Fail("shouldn't have called handler")
40+
})
41+
},
42+
state: snow.Bootstrapping,
43+
expectedStatusCode: http.StatusServiceUnavailable,
44+
},
45+
{
46+
name: "chain is done bootstrapping",
47+
handlerFunc: func(*require.Assertions) http.Handler {
48+
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
49+
w.WriteHeader(http.StatusTeapot)
50+
})
51+
},
52+
state: snow.NormalOp,
53+
expectedStatusCode: http.StatusTeapot,
54+
},
55+
}
56+
57+
for _, tt := range tests {
58+
t.Run(tt.name, func(t *testing.T) {
59+
require := require.New(t)
60+
61+
ctx := &snow.ConsensusContext{}
62+
ctx.State.Set(snow.EngineState{
63+
State: tt.state,
64+
})
65+
66+
middleware := rejectMiddleware(tt.handlerFunc(require), ctx)
67+
w := httptest.NewRecorder()
68+
middleware.ServeHTTP(w, nil)
69+
require.Equal(tt.expectedStatusCode, w.Code)
70+
})
71+
}
72+
}

0 commit comments

Comments
 (0)