http_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package rpc
  17. import (
  18. "net/http"
  19. "net/http/httptest"
  20. "strings"
  21. "testing"
  22. )
  23. func confirmStatusCode(t *testing.T, got, want int) {
  24. t.Helper()
  25. if got == want {
  26. return
  27. }
  28. if gotName := http.StatusText(got); len(gotName) > 0 {
  29. if wantName := http.StatusText(want); len(wantName) > 0 {
  30. t.Fatalf("response status code: got %d (%s), want %d (%s)", got, gotName, want, wantName)
  31. }
  32. }
  33. t.Fatalf("response status code: got %d, want %d", got, want)
  34. }
  35. func confirmRequestValidationCode(t *testing.T, method, contentType, body string, expectedStatusCode int) {
  36. t.Helper()
  37. request := httptest.NewRequest(method, "http://url.com", strings.NewReader(body))
  38. if len(contentType) > 0 {
  39. request.Header.Set("Content-Type", contentType)
  40. }
  41. code, err := validateRequest(request)
  42. if code == 0 {
  43. if err != nil {
  44. t.Errorf("validation: got error %v, expected nil", err)
  45. }
  46. } else if err == nil {
  47. t.Errorf("validation: code %d: got nil, expected error", code)
  48. }
  49. confirmStatusCode(t, code, expectedStatusCode)
  50. }
  51. func TestHTTPErrorResponseWithDelete(t *testing.T) {
  52. confirmRequestValidationCode(t, http.MethodDelete, contentType, "", http.StatusMethodNotAllowed)
  53. }
  54. func TestHTTPErrorResponseWithPut(t *testing.T) {
  55. confirmRequestValidationCode(t, http.MethodPut, contentType, "", http.StatusMethodNotAllowed)
  56. }
  57. func TestHTTPErrorResponseWithMaxContentLength(t *testing.T) {
  58. body := make([]rune, maxRequestContentLength+1)
  59. confirmRequestValidationCode(t,
  60. http.MethodPost, contentType, string(body), http.StatusRequestEntityTooLarge)
  61. }
  62. func TestHTTPErrorResponseWithEmptyContentType(t *testing.T) {
  63. confirmRequestValidationCode(t, http.MethodPost, "", "", http.StatusUnsupportedMediaType)
  64. }
  65. func TestHTTPErrorResponseWithValidRequest(t *testing.T) {
  66. confirmRequestValidationCode(t, http.MethodPost, contentType, "", 0)
  67. }
  68. func confirmHTTPRequestYieldsStatusCode(t *testing.T, method, contentType, body string, expectedStatusCode int) {
  69. t.Helper()
  70. s := Server{}
  71. ts := httptest.NewServer(&s)
  72. defer ts.Close()
  73. request, err := http.NewRequest(method, ts.URL, strings.NewReader(body))
  74. if err != nil {
  75. t.Fatalf("failed to create a valid HTTP request: %v", err)
  76. }
  77. if len(contentType) > 0 {
  78. request.Header.Set("Content-Type", contentType)
  79. }
  80. resp, err := http.DefaultClient.Do(request)
  81. if err != nil {
  82. t.Fatalf("request failed: %v", err)
  83. }
  84. confirmStatusCode(t, resp.StatusCode, expectedStatusCode)
  85. }
  86. func TestHTTPResponseWithEmptyGet(t *testing.T) {
  87. confirmHTTPRequestYieldsStatusCode(t, http.MethodGet, "", "", http.StatusOK)
  88. }