rpcstack_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. func createAndStartServer(t *testing.T, conf httpConfig, ws bool, wsConf wsConfig) *httpServer {
  64. t.Helper()
  65. srv := newHTTPServer(testlog.Logger(t, log.LvlDebug), rpc.DefaultHTTPTimeouts)
  66. assert.NoError(t, srv.enableRPC(nil, conf))
  67. if ws {
  68. assert.NoError(t, srv.enableWS(nil, wsConf))
  69. }
  70. assert.NoError(t, srv.setListenAddr("localhost", 0))
  71. assert.NoError(t, srv.start())
  72. return srv
  73. }
  74. func testRequest(t *testing.T, key, value, host string, srv *httpServer) *http.Response {
  75. t.Helper()
  76. body := bytes.NewReader([]byte(`{"jsonrpc":"2.0","id":1,method":"rpc_modules"}`))
  77. req, _ := http.NewRequest("POST", "http://"+srv.listenAddr(), body)
  78. req.Header.Set("content-type", "application/json")
  79. if key != "" && value != "" {
  80. req.Header.Set(key, value)
  81. }
  82. if host != "" {
  83. req.Host = host
  84. }
  85. client := http.DefaultClient
  86. resp, err := client.Do(req)
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. return resp
  91. }