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