github.com/theQRL/go-zond@v0.1.1/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  	resp.Body.Close()
    98  	confirmStatusCode(t, resp.StatusCode, expectedStatusCode)
    99  }
   100  
   101  func TestHTTPResponseWithEmptyGet(t *testing.T) {
   102  	confirmHTTPRequestYieldsStatusCode(t, http.MethodGet, "", "", http.StatusOK)
   103  }
   104  
   105  // This checks that maxRequestContentLength is not applied to the response of a request.
   106  func TestHTTPRespBodyUnlimited(t *testing.T) {
   107  	const respLength = maxRequestContentLength * 3
   108  
   109  	s := NewServer()
   110  	defer s.Stop()
   111  	s.RegisterName("test", largeRespService{respLength})
   112  	ts := httptest.NewServer(s)
   113  	defer ts.Close()
   114  
   115  	c, err := DialHTTP(ts.URL)
   116  	if err != nil {
   117  		t.Fatal(err)
   118  	}
   119  	defer c.Close()
   120  
   121  	var r string
   122  	if err := c.Call(&r, "test_largeResp"); err != nil {
   123  		t.Fatal(err)
   124  	}
   125  	if len(r) != respLength {
   126  		t.Fatalf("response has wrong length %d, want %d", len(r), respLength)
   127  	}
   128  }
   129  
   130  // Tests that an HTTP error results in an HTTPError instance
   131  // being returned with the expected attributes.
   132  func TestHTTPErrorResponse(t *testing.T) {
   133  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   134  		http.Error(w, "error has occurred!", http.StatusTeapot)
   135  	}))
   136  	defer ts.Close()
   137  
   138  	c, err := DialHTTP(ts.URL)
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  
   143  	var r string
   144  	err = c.Call(&r, "test_method")
   145  	if err == nil {
   146  		t.Fatal("error was expected")
   147  	}
   148  
   149  	httpErr, ok := err.(HTTPError)
   150  	if !ok {
   151  		t.Fatalf("unexpected error type %T", err)
   152  	}
   153  
   154  	if httpErr.StatusCode != http.StatusTeapot {
   155  		t.Error("unexpected status code", httpErr.StatusCode)
   156  	}
   157  	if httpErr.Status != "418 I'm a teapot" {
   158  		t.Error("unexpected status text", httpErr.Status)
   159  	}
   160  	if body := string(httpErr.Body); body != "error has occurred!\n" {
   161  		t.Error("unexpected body", body)
   162  	}
   163  
   164  	if errMsg := httpErr.Error(); errMsg != "418 I'm a teapot: error has occurred!\n" {
   165  		t.Error("unexpected error message", errMsg)
   166  	}
   167  }
   168  
   169  func TestHTTPPeerInfo(t *testing.T) {
   170  	s := newTestServer()
   171  	defer s.Stop()
   172  	ts := httptest.NewServer(s)
   173  	defer ts.Close()
   174  
   175  	c, err := Dial(ts.URL)
   176  	if err != nil {
   177  		t.Fatal(err)
   178  	}
   179  	c.SetHeader("user-agent", "ua-testing")
   180  	c.SetHeader("origin", "origin.example.com")
   181  
   182  	// Request peer information.
   183  	var info PeerInfo
   184  	if err := c.Call(&info, "test_peerInfo"); err != nil {
   185  		t.Fatal(err)
   186  	}
   187  
   188  	if info.RemoteAddr == "" {
   189  		t.Error("RemoteAddr not set")
   190  	}
   191  	if info.Transport != "http" {
   192  		t.Errorf("wrong Transport %q", info.Transport)
   193  	}
   194  	if info.HTTP.Version != "HTTP/1.1" {
   195  		t.Errorf("wrong HTTP.Version %q", info.HTTP.Version)
   196  	}
   197  	if info.HTTP.UserAgent != "ua-testing" {
   198  		t.Errorf("wrong HTTP.UserAgent %q", info.HTTP.UserAgent)
   199  	}
   200  	if info.HTTP.Origin != "origin.example.com" {
   201  		t.Errorf("wrong HTTP.Origin %q", info.HTTP.UserAgent)
   202  	}
   203  }
   204  
   205  func TestNewContextWithHeaders(t *testing.T) {
   206  	expectedHeaders := 0
   207  	server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
   208  		for i := 0; i < expectedHeaders; i++ {
   209  			key, want := fmt.Sprintf("key-%d", i), fmt.Sprintf("val-%d", i)
   210  			if have := request.Header.Get(key); have != want {
   211  				t.Errorf("wrong request headers for %s, want: %s, have: %s", key, want, have)
   212  			}
   213  		}
   214  		writer.WriteHeader(http.StatusOK)
   215  		_, _ = writer.Write([]byte(`{}`))
   216  	}))
   217  	defer server.Close()
   218  
   219  	client, err := Dial(server.URL)
   220  	if err != nil {
   221  		t.Fatalf("failed to dial: %s", err)
   222  	}
   223  	defer client.Close()
   224  
   225  	newHdr := func(k, v string) http.Header {
   226  		header := http.Header{}
   227  		header.Set(k, v)
   228  		return header
   229  	}
   230  	ctx1 := NewContextWithHeaders(context.Background(), newHdr("key-0", "val-0"))
   231  	ctx2 := NewContextWithHeaders(ctx1, newHdr("key-1", "val-1"))
   232  	ctx3 := NewContextWithHeaders(ctx2, newHdr("key-2", "val-2"))
   233  
   234  	expectedHeaders = 3
   235  	if err := client.CallContext(ctx3, nil, "test"); err != ErrNoResult {
   236  		t.Error("call failed", err)
   237  	}
   238  
   239  	expectedHeaders = 2
   240  	if err := client.CallContext(ctx2, nil, "test"); err != ErrNoResult {
   241  		t.Error("call failed:", err)
   242  	}
   243  }