http_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. }
  89. // This checks that maxRequestContentLength is not applied to the response of a request.
  90. func TestHTTPRespBodyUnlimited(t *testing.T) {
  91. const respLength = maxRequestContentLength * 3
  92. s := NewServer()
  93. defer s.Stop()
  94. s.RegisterName("test", largeRespService{respLength})
  95. ts := httptest.NewServer(s)
  96. defer ts.Close()
  97. c, err := DialHTTP(ts.URL)
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. defer c.Close()
  102. var r string
  103. if err := c.Call(&r, "test_largeResp"); err != nil {
  104. t.Fatal(err)
  105. }
  106. if len(r) != respLength {
  107. t.Fatalf("response has wrong length %d, want %d", len(r), respLength)
  108. }
  109. }
  110. // Tests that an HTTP error results in an HTTPError instance
  111. // being returned with the expected attributes.
  112. func TestHTTPErrorResponse(t *testing.T) {
  113. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  114. http.Error(w, "error has occurred!", http.StatusTeapot)
  115. }))
  116. defer ts.Close()
  117. c, err := DialHTTP(ts.URL)
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. var r string
  122. err = c.Call(&r, "test_method")
  123. if err == nil {
  124. t.Fatal("error was expected")
  125. }
  126. httpErr, ok := err.(HTTPError)
  127. if !ok {
  128. t.Fatalf("unexpected error type %T", err)
  129. }
  130. if httpErr.StatusCode != http.StatusTeapot {
  131. t.Error("unexpected status code", httpErr.StatusCode)
  132. }
  133. if httpErr.Status != "418 I'm a teapot" {
  134. t.Error("unexpected status text", httpErr.Status)
  135. }
  136. if body := string(httpErr.Body); body != "error has occurred!\n" {
  137. t.Error("unexpected body", body)
  138. }
  139. if errMsg := httpErr.Error(); errMsg != "418 I'm a teapot: error has occurred!\n" {
  140. t.Error("unexpected error message", errMsg)
  141. }
  142. }
  143. func TestHTTPPeerInfo(t *testing.T) {
  144. s := newTestServer()
  145. defer s.Stop()
  146. ts := httptest.NewServer(s)
  147. defer ts.Close()
  148. c, err := Dial(ts.URL)
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. c.SetHeader("user-agent", "ua-testing")
  153. c.SetHeader("origin", "origin.example.com")
  154. // Request peer information.
  155. var info PeerInfo
  156. if err := c.Call(&info, "test_peerInfo"); err != nil {
  157. t.Fatal(err)
  158. }
  159. if info.RemoteAddr == "" {
  160. t.Error("RemoteAddr not set")
  161. }
  162. if info.Transport != "http" {
  163. t.Errorf("wrong Transport %q", info.Transport)
  164. }
  165. if info.HTTP.Version != "HTTP/1.1" {
  166. t.Errorf("wrong HTTP.Version %q", info.HTTP.Version)
  167. }
  168. if info.HTTP.UserAgent != "ua-testing" {
  169. t.Errorf("wrong HTTP.UserAgent %q", info.HTTP.UserAgent)
  170. }
  171. if info.HTTP.Origin != "origin.example.com" {
  172. t.Errorf("wrong HTTP.Origin %q", info.HTTP.UserAgent)
  173. }
  174. }