github.com/ethereum/go-ethereum@v1.14.3/rpc/http_test.go (about)

     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  
    17  package rpc
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"strings"
    25  	"testing"
    26  )
    27  
    28  func confirmStatusCode(t *testing.T, got, want int) {
    29  	t.Helper()
    30  	if got == want {
    31  		return
    32  	}
    33  	if gotName := http.StatusText(got); len(gotName) > 0 {
    34  		if wantName := http.StatusText(want); len(wantName) > 0 {
    35  			t.Fatalf("response status code: got %d (%s), want %d (%s)", got, gotName, want, wantName)
    36  		}
    37  	}
    38  	t.Fatalf("response status code: got %d, want %d", got, want)
    39  }
    40  
    41  func confirmRequestValidationCode(t *testing.T, method, contentType, body string, expectedStatusCode int) {
    42  	t.Helper()
    43  
    44  	s := NewServer()
    45  	request := httptest.NewRequest(method, "http://url.com", strings.NewReader(body))
    46  	if len(contentType) > 0 {
    47  		request.Header.Set("Content-Type", contentType)
    48  	}
    49  	code, err := s.validateRequest(request)
    50  	if code == 0 {
    51  		if err != nil {
    52  			t.Errorf("validation: got error %v, expected nil", err)
    53  		}
    54  	} else if err == nil {
    55  		t.Errorf("validation: code %d: got nil, expected error", code)
    56  	}
    57  	confirmStatusCode(t, code, expectedStatusCode)
    58  }
    59  
    60  func TestHTTPErrorResponseWithDelete(t *testing.T) {
    61  	confirmRequestValidationCode(t, http.MethodDelete, contentType, "", http.StatusMethodNotAllowed)
    62  }
    63  
    64  func TestHTTPErrorResponseWithPut(t *testing.T) {
    65  	confirmRequestValidationCode(t, http.MethodPut, contentType, "", http.StatusMethodNotAllowed)
    66  }
    67  
    68  func TestHTTPErrorResponseWithMaxContentLength(t *testing.T) {
    69  	body := make([]rune, defaultBodyLimit+1)
    70  	confirmRequestValidationCode(t,
    71  		http.MethodPost, contentType, string(body), http.StatusRequestEntityTooLarge)
    72  }
    73  
    74  func TestHTTPErrorResponseWithEmptyContentType(t *testing.T) {
    75  	confirmRequestValidationCode(t, http.MethodPost, "", "", http.StatusUnsupportedMediaType)
    76  }
    77  
    78  func TestHTTPErrorResponseWithValidRequest(t *testing.T) {
    79  	confirmRequestValidationCode(t, http.MethodPost, contentType, "", 0)
    80  }
    81  
    82  func confirmHTTPRequestYieldsStatusCode(t *testing.T, method, contentType, body string, expectedStatusCode int) {
    83  	t.Helper()
    84  	s := Server{}
    85  	ts := httptest.NewServer(&s)
    86  	defer ts.Close()
    87  
    88  	request, err := http.NewRequest(method, ts.URL, strings.NewReader(body))
    89  	if err != nil {
    90  		t.Fatalf("failed to create a valid HTTP request: %v", err)
    91  	}
    92  	if len(contentType) > 0 {
    93  		request.Header.Set("Content-Type", contentType)
    94  	}
    95  	resp, err := http.DefaultClient.Do(request)
    96  	if err != nil {
    97  		t.Fatalf("request failed: %v", err)
    98  	}
    99  	resp.Body.Close()
   100  	confirmStatusCode(t, resp.StatusCode, expectedStatusCode)
   101  }
   102  
   103  func TestHTTPResponseWithEmptyGet(t *testing.T) {
   104  	confirmHTTPRequestYieldsStatusCode(t, http.MethodGet, "", "", http.StatusOK)
   105  }
   106  
   107  // This checks that maxRequestContentLength is not applied to the response of a request.
   108  func TestHTTPRespBodyUnlimited(t *testing.T) {
   109  	const respLength = defaultBodyLimit * 3
   110  
   111  	s := NewServer()
   112  	defer s.Stop()
   113  	s.RegisterName("test", largeRespService{respLength})
   114  	ts := httptest.NewServer(s)
   115  	defer ts.Close()
   116  
   117  	c, err := DialHTTP(ts.URL)
   118  	if err != nil {
   119  		t.Fatal(err)
   120  	}
   121  	defer c.Close()
   122  
   123  	var r string
   124  	if err := c.Call(&r, "test_largeResp"); err != nil {
   125  		t.Fatal(err)
   126  	}
   127  	if len(r) != respLength {
   128  		t.Fatalf("response has wrong length %d, want %d", len(r), respLength)
   129  	}
   130  }
   131  
   132  // Tests that an HTTP error results in an HTTPError instance
   133  // being returned with the expected attributes.
   134  func TestHTTPErrorResponse(t *testing.T) {
   135  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   136  		http.Error(w, "error has occurred!", http.StatusTeapot)
   137  	}))
   138  	defer ts.Close()
   139  
   140  	c, err := DialHTTP(ts.URL)
   141  	if err != nil {
   142  		t.Fatal(err)
   143  	}
   144  
   145  	var r string
   146  	err = c.Call(&r, "test_method")
   147  	if err == nil {
   148  		t.Fatal("error was expected")
   149  	}
   150  
   151  	httpErr, ok := err.(HTTPError)
   152  	if !ok {
   153  		t.Fatalf("unexpected error type %T", err)
   154  	}
   155  
   156  	if httpErr.StatusCode != http.StatusTeapot {
   157  		t.Error("unexpected status code", httpErr.StatusCode)
   158  	}
   159  	if httpErr.Status != "418 I'm a teapot" {
   160  		t.Error("unexpected status text", httpErr.Status)
   161  	}
   162  	if body := string(httpErr.Body); body != "error has occurred!\n" {
   163  		t.Error("unexpected body", body)
   164  	}
   165  
   166  	if errMsg := httpErr.Error(); errMsg != "418 I'm a teapot: error has occurred!\n" {
   167  		t.Error("unexpected error message", errMsg)
   168  	}
   169  }
   170  
   171  func TestHTTPPeerInfo(t *testing.T) {
   172  	s := newTestServer()
   173  	defer s.Stop()
   174  	ts := httptest.NewServer(s)
   175  	defer ts.Close()
   176  
   177  	c, err := Dial(ts.URL)
   178  	if err != nil {
   179  		t.Fatal(err)
   180  	}
   181  	c.SetHeader("user-agent", "ua-testing")
   182  	c.SetHeader("origin", "origin.example.com")
   183  
   184  	// Request peer information.
   185  	var info PeerInfo
   186  	if err := c.Call(&info, "test_peerInfo"); err != nil {
   187  		t.Fatal(err)
   188  	}
   189  
   190  	if info.RemoteAddr == "" {
   191  		t.Error("RemoteAddr not set")
   192  	}
   193  	if info.Transport != "http" {
   194  		t.Errorf("wrong Transport %q", info.Transport)
   195  	}
   196  	if info.HTTP.Version != "HTTP/1.1" {
   197  		t.Errorf("wrong HTTP.Version %q", info.HTTP.Version)
   198  	}
   199  	if info.HTTP.UserAgent != "ua-testing" {
   200  		t.Errorf("wrong HTTP.UserAgent %q", info.HTTP.UserAgent)
   201  	}
   202  	if info.HTTP.Origin != "origin.example.com" {
   203  		t.Errorf("wrong HTTP.Origin %q", info.HTTP.UserAgent)
   204  	}
   205  }
   206  
   207  func TestNewContextWithHeaders(t *testing.T) {
   208  	expectedHeaders := 0
   209  	server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
   210  		for i := 0; i < expectedHeaders; i++ {
   211  			key, want := fmt.Sprintf("key-%d", i), fmt.Sprintf("val-%d", i)
   212  			if have := request.Header.Get(key); have != want {
   213  				t.Errorf("wrong request headers for %s, want: %s, have: %s", key, want, have)
   214  			}
   215  		}
   216  		writer.WriteHeader(http.StatusOK)
   217  		_, _ = writer.Write([]byte(`{}`))
   218  	}))
   219  	defer server.Close()
   220  
   221  	client, err := Dial(server.URL)
   222  	if err != nil {
   223  		t.Fatalf("failed to dial: %s", err)
   224  	}
   225  	defer client.Close()
   226  
   227  	newHdr := func(k, v string) http.Header {
   228  		header := http.Header{}
   229  		header.Set(k, v)
   230  		return header
   231  	}
   232  	ctx1 := NewContextWithHeaders(context.Background(), newHdr("key-0", "val-0"))
   233  	ctx2 := NewContextWithHeaders(ctx1, newHdr("key-1", "val-1"))
   234  	ctx3 := NewContextWithHeaders(ctx2, newHdr("key-2", "val-2"))
   235  
   236  	expectedHeaders = 3
   237  	if err := client.CallContext(ctx3, nil, "test"); err != ErrNoResult {
   238  		t.Error("call failed", err)
   239  	}
   240  
   241  	expectedHeaders = 2
   242  	if err := client.CallContext(ctx2, nil, "test"); err != ErrNoResult {
   243  		t.Error("call failed:", err)
   244  	}
   245  }