rpcstack_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2020 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 node
  17. import (
  18. "bytes"
  19. "net/http"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/internal/testlog"
  22. "github.com/ethereum/go-ethereum/log"
  23. "github.com/ethereum/go-ethereum/rpc"
  24. "github.com/gorilla/websocket"
  25. "github.com/stretchr/testify/assert"
  26. )
  27. // TestCorsHandler makes sure CORS are properly handled on the http server.
  28. func TestCorsHandler(t *testing.T) {
  29. srv := createAndStartServer(t, httpConfig{CorsAllowedOrigins: []string{"test", "test.com"}}, false, wsConfig{})
  30. defer srv.stop()
  31. resp := testRequest(t, "origin", "test.com", "", srv)
  32. assert.Equal(t, "test.com", resp.Header.Get("Access-Control-Allow-Origin"))
  33. resp2 := testRequest(t, "origin", "bad", "", srv)
  34. assert.Equal(t, "", resp2.Header.Get("Access-Control-Allow-Origin"))
  35. }
  36. // TestVhosts makes sure vhosts are properly handled on the http server.
  37. func TestVhosts(t *testing.T) {
  38. srv := createAndStartServer(t, httpConfig{Vhosts: []string{"test"}}, false, wsConfig{})
  39. defer srv.stop()
  40. resp := testRequest(t, "", "", "test", srv)
  41. assert.Equal(t, resp.StatusCode, http.StatusOK)
  42. resp2 := testRequest(t, "", "", "bad", srv)
  43. assert.Equal(t, resp2.StatusCode, http.StatusForbidden)
  44. }
  45. // TestWebsocketOrigins makes sure the websocket origins are properly handled on the websocket server.
  46. func TestWebsocketOrigins(t *testing.T) {
  47. srv := createAndStartServer(t, httpConfig{}, true, wsConfig{Origins: []string{"test"}})
  48. defer srv.stop()
  49. dialer := websocket.DefaultDialer
  50. _, _, err := dialer.Dial("ws://"+srv.listenAddr(), http.Header{
  51. "Content-type": []string{"application/json"},
  52. "Sec-WebSocket-Version": []string{"13"},
  53. "Origin": []string{"test"},
  54. })
  55. assert.NoError(t, err)
  56. _, _, err = dialer.Dial("ws://"+srv.listenAddr(), http.Header{
  57. "Content-type": []string{"application/json"},
  58. "Sec-WebSocket-Version": []string{"13"},
  59. "Origin": []string{"bad"},
  60. })
  61. assert.Error(t, err)
  62. }
  63. // TestIsWebsocket tests if an incoming websocket upgrade request is handled properly.
  64. func TestIsWebsocket(t *testing.T) {
  65. r, _ := http.NewRequest("GET", "/", nil)
  66. assert.False(t, isWebsocket(r))
  67. r.Header.Set("upgrade", "websocket")
  68. assert.False(t, isWebsocket(r))
  69. r.Header.Set("connection", "upgrade")
  70. assert.True(t, isWebsocket(r))
  71. r.Header.Set("connection", "upgrade,keep-alive")
  72. assert.True(t, isWebsocket(r))
  73. r.Header.Set("connection", " UPGRADE,keep-alive")
  74. assert.True(t, isWebsocket(r))
  75. }
  76. func createAndStartServer(t *testing.T, conf httpConfig, ws bool, wsConf wsConfig) *httpServer {
  77. t.Helper()
  78. srv := newHTTPServer(testlog.Logger(t, log.LvlDebug), rpc.DefaultHTTPTimeouts)
  79. assert.NoError(t, srv.enableRPC(nil, conf))
  80. if ws {
  81. assert.NoError(t, srv.enableWS(nil, wsConf))
  82. }
  83. assert.NoError(t, srv.setListenAddr("localhost", 0))
  84. assert.NoError(t, srv.start())
  85. return srv
  86. }
  87. func testRequest(t *testing.T, key, value, host string, srv *httpServer) *http.Response {
  88. t.Helper()
  89. body := bytes.NewReader([]byte(`{"jsonrpc":"2.0","id":1,method":"rpc_modules"}`))
  90. req, _ := http.NewRequest("POST", "http://"+srv.listenAddr(), body)
  91. req.Header.Set("content-type", "application/json")
  92. if key != "" && value != "" {
  93. req.Header.Set(key, value)
  94. }
  95. if host != "" {
  96. req.Host = host
  97. }
  98. client := http.DefaultClient
  99. resp, err := client.Do(req)
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. return resp
  104. }