Skip to content

Commit 9887edd

Browse files
authored
rpc: add explicit 200 response for empty HTTP GET (#20952)
1 parent 1893266 commit 9887edd

2 files changed

Lines changed: 57 additions & 10 deletions

File tree

‎rpc/http.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ func (t *httpServerConn) SetWriteDeadline(time.Time) error { return nil }
208208
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
209209
// Permit dumb empty requests for remote health-checks (AWS)
210210
if r.Method == http.MethodGet && r.ContentLength == 0 && r.URL.RawQuery == "" {
211+
w.WriteHeader(http.StatusOK)
211212
return
212213
}
213214
if code, err := validateRequest(r); err != nil {

‎rpc/http_test.go‎

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,78 @@ import (
2323
"testing"
2424
)
2525

26+
func confirmStatusCode(t *testing.T, got, want int) {
27+
t.Helper()
28+
if got == want {
29+
return
30+
}
31+
if gotName := http.StatusText(got); len(gotName) > 0 {
32+
if wantName := http.StatusText(want); len(wantName) > 0 {
33+
t.Fatalf("response status code: got %d (%s), want %d (%s)", got, gotName, want, wantName)
34+
}
35+
}
36+
t.Fatalf("response status code: got %d, want %d", got, want)
37+
}
38+
39+
func confirmRequestValidationCode(t *testing.T, method, contentType, body string, expectedStatusCode int) {
40+
t.Helper()
41+
request := httptest.NewRequest(method, "http://url.com", strings.NewReader(body))
42+
if len(contentType) > 0 {
43+
request.Header.Set("Content-Type", contentType)
44+
}
45+
code, err := validateRequest(request)
46+
if code == 0 {
47+
if err != nil {
48+
t.Errorf("validation: got error %v, expected nil", err)
49+
}
50+
} else if err == nil {
51+
t.Errorf("validation: code %d: got nil, expected error", code)
52+
}
53+
confirmStatusCode(t, code, expectedStatusCode)
54+
}
55+
2656
func TestHTTPErrorResponseWithDelete(t *testing.T) {
27-
testHTTPErrorResponse(t, http.MethodDelete, contentType, "", http.StatusMethodNotAllowed)
57+
confirmRequestValidationCode(t, http.MethodDelete, contentType, "", http.StatusMethodNotAllowed)
2858
}
2959

3060
func TestHTTPErrorResponseWithPut(t *testing.T) {
31-
testHTTPErrorResponse(t, http.MethodPut, contentType, "", http.StatusMethodNotAllowed)
61+
confirmRequestValidationCode(t, http.MethodPut, contentType, "", http.StatusMethodNotAllowed)
3262
}
3363

3464
func TestHTTPErrorResponseWithMaxContentLength(t *testing.T) {
3565
body := make([]rune, maxRequestContentLength+1)
36-
testHTTPErrorResponse(t,
66+
confirmRequestValidationCode(t,
3767
http.MethodPost, contentType, string(body), http.StatusRequestEntityTooLarge)
3868
}
3969

4070
func TestHTTPErrorResponseWithEmptyContentType(t *testing.T) {
41-
testHTTPErrorResponse(t, http.MethodPost, "", "", http.StatusUnsupportedMediaType)
71+
confirmRequestValidationCode(t, http.MethodPost, "", "", http.StatusUnsupportedMediaType)
4272
}
4373

4474
func TestHTTPErrorResponseWithValidRequest(t *testing.T) {
45-
testHTTPErrorResponse(t, http.MethodPost, contentType, "", 0)
75+
confirmRequestValidationCode(t, http.MethodPost, contentType, "", 0)
4676
}
4777

48-
func testHTTPErrorResponse(t *testing.T, method, contentType, body string, expected int) {
49-
request := httptest.NewRequest(method, "http://url.com", strings.NewReader(body))
50-
request.Header.Set("content-type", contentType)
51-
if code, _ := validateRequest(request); code != expected {
52-
t.Fatalf("response code should be %d not %d", expected, code)
78+
func confirmHTTPRequestYieldsStatusCode(t *testing.T, method, contentType, body string, expectedStatusCode int) {
79+
t.Helper()
80+
s := Server{}
81+
ts := httptest.NewServer(&s)
82+
defer ts.Close()
83+
84+
request, err := http.NewRequest(method, ts.URL, strings.NewReader(body))
85+
if err != nil {
86+
t.Fatalf("failed to create a valid HTTP request: %v", err)
87+
}
88+
if len(contentType) > 0 {
89+
request.Header.Set("Content-Type", contentType)
5390
}
91+
resp, err := http.DefaultClient.Do(request)
92+
if err != nil {
93+
t.Fatalf("request failed: %v", err)
94+
}
95+
confirmStatusCode(t, resp.StatusCode, expectedStatusCode)
96+
}
97+
98+
func TestHTTPResponseWithEmptyGet(t *testing.T) {
99+
confirmHTTPRequestYieldsStatusCode(t, http.MethodGet, "", "", http.StatusOK)
54100
}

0 commit comments

Comments
 (0)