github.com/core-coin/go-core/v2@v2.1.9/rpc/http_test.go (about) 1 // Copyright 2017 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package rpc 18 19 import ( 20 "net/http" 21 "net/http/httptest" 22 "strings" 23 "testing" 24 ) 25 26 func confirmStatusCode(t *testing.T, got, want int) { 27 t.Helper() 28 if got == want { 29 return 30 } 31 if gotName := http.StatusText(got); len(gotName) > 0 { 32 if wantName := http.StatusText(want); len(wantName) > 0 { 33 t.Fatalf("response status code: got %d (%s), want %d (%s)", got, gotName, want, wantName) 34 } 35 } 36 t.Fatalf("response status code: got %d, want %d", got, want) 37 } 38 39 func confirmRequestValidationCode(t *testing.T, method, contentType, body string, expectedStatusCode int) { 40 t.Helper() 41 request := httptest.NewRequest(method, "http://url.com", strings.NewReader(body)) 42 if len(contentType) > 0 { 43 request.Header.Set("Content-Type", contentType) 44 } 45 code, err := validateRequest(request) 46 if code == 0 { 47 if err != nil { 48 t.Errorf("validation: got error %v, expected nil", err) 49 } 50 } else if err == nil { 51 t.Errorf("validation: code %d: got nil, expected error", code) 52 } 53 confirmStatusCode(t, code, expectedStatusCode) 54 } 55 56 func TestHTTPErrorResponseWithDelete(t *testing.T) { 57 confirmRequestValidationCode(t, http.MethodDelete, contentType, "", http.StatusMethodNotAllowed) 58 } 59 60 func TestHTTPErrorResponseWithPut(t *testing.T) { 61 confirmRequestValidationCode(t, http.MethodPut, contentType, "", http.StatusMethodNotAllowed) 62 } 63 64 func TestHTTPErrorResponseWithMaxContentLength(t *testing.T) { 65 body := make([]rune, maxRequestContentLength+1) 66 confirmRequestValidationCode(t, 67 http.MethodPost, contentType, string(body), http.StatusRequestEntityTooLarge) 68 } 69 70 func TestHTTPErrorResponseWithEmptyContentType(t *testing.T) { 71 confirmRequestValidationCode(t, http.MethodPost, "", "", http.StatusUnsupportedMediaType) 72 } 73 74 func TestHTTPErrorResponseWithValidRequest(t *testing.T) { 75 confirmRequestValidationCode(t, http.MethodPost, contentType, "", 0) 76 } 77 78 func confirmHTTPRequestYieldsStatusCode(t *testing.T, method, contentType, body string, expectedStatusCode int) { 79 t.Helper() 80 s := Server{} 81 ts := httptest.NewServer(&s) 82 defer ts.Close() 83 84 request, err := http.NewRequest(method, ts.URL, strings.NewReader(body)) 85 if err != nil { 86 t.Fatalf("failed to create a valid HTTP request: %v", err) 87 } 88 if len(contentType) > 0 { 89 request.Header.Set("Content-Type", contentType) 90 } 91 resp, err := http.DefaultClient.Do(request) 92 if err != nil { 93 t.Fatalf("request failed: %v", err) 94 } 95 confirmStatusCode(t, resp.StatusCode, expectedStatusCode) 96 } 97 98 func TestHTTPResponseWithEmptyGet(t *testing.T) { 99 confirmHTTPRequestYieldsStatusCode(t, http.MethodGet, "", "", http.StatusOK) 100 } 101 102 func TestHTTPPeerInfo(t *testing.T) { 103 s := newTestServer() 104 defer s.Stop() 105 ts := httptest.NewServer(s) 106 defer ts.Close() 107 108 c, err := Dial(ts.URL) 109 if err != nil { 110 t.Fatal(err) 111 } 112 c.SetHeader("user-agent", "ua-testing") 113 c.SetHeader("origin", "origin.example.com") 114 115 // Request peer information. 116 var info PeerInfo 117 if err := c.Call(&info, "test_peerInfo"); err != nil { 118 t.Fatal(err) 119 } 120 121 if info.RemoteAddr == "" { 122 t.Error("RemoteAddr not set") 123 } 124 if info.Transport != "http" { 125 t.Errorf("wrong Transport %q", info.Transport) 126 } 127 if info.HTTP.Version != "HTTP/1.1" { 128 t.Errorf("wrong HTTP.Version %q", info.HTTP.Version) 129 } 130 if info.HTTP.UserAgent != "ua-testing" { 131 t.Errorf("wrong HTTP.UserAgent %q", info.HTTP.UserAgent) 132 } 133 if info.HTTP.Origin != "origin.example.com" { 134 t.Errorf("wrong HTTP.Origin %q", info.HTTP.UserAgent) 135 } 136 }