@@ -363,7 +363,7 @@ func TestLifecycleTerminationGuarantee(t *testing.T) {
363363}
364364
365365// Tests whether a handler can be successfully mounted on the canonical HTTP server
366- // on the given path
366+ // on the given prefix
367367func TestRegisterHandler_Successful (t * testing.T ) {
368368 node := createNode (t , 7878 , 7979 )
369369
@@ -427,6 +427,112 @@ func TestWebsocketHTTPOnSamePort_WebsocketRequest(t *testing.T) {
427427 }
428428}
429429
430+ type rpcPrefixTest struct {
431+ httpPrefix , wsPrefix string
432+ // These lists paths on which JSON-RPC should be served / not served.
433+ wantHTTP []string
434+ wantNoHTTP []string
435+ wantWS []string
436+ wantNoWS []string
437+ }
438+
439+ func TestNodeRPCPrefix (t * testing.T ) {
440+ t .Parallel ()
441+
442+ tests := []rpcPrefixTest {
443+ // both off
444+ {
445+ httpPrefix : "" , wsPrefix : "" ,
446+ wantHTTP : []string {"/" , "/?p=1" },
447+ wantNoHTTP : []string {"/test" , "/test?p=1" },
448+ wantWS : []string {"/" , "/?p=1" },
449+ wantNoWS : []string {"/test" , "/test?p=1" },
450+ },
451+ // only http prefix
452+ {
453+ httpPrefix : "/testprefix" , wsPrefix : "" ,
454+ wantHTTP : []string {"/testprefix" , "/testprefix?p=1" , "/testprefix/x" , "/testprefix/x?p=1" },
455+ wantNoHTTP : []string {"/" , "/?p=1" , "/test" , "/test?p=1" },
456+ wantWS : []string {"/" , "/?p=1" },
457+ wantNoWS : []string {"/testprefix" , "/testprefix?p=1" , "/test" , "/test?p=1" },
458+ },
459+ // only ws prefix
460+ {
461+ httpPrefix : "" , wsPrefix : "/testprefix" ,
462+ wantHTTP : []string {"/" , "/?p=1" },
463+ wantNoHTTP : []string {"/testprefix" , "/testprefix?p=1" , "/test" , "/test?p=1" },
464+ wantWS : []string {"/testprefix" , "/testprefix?p=1" , "/testprefix/x" , "/testprefix/x?p=1" },
465+ wantNoWS : []string {"/" , "/?p=1" , "/test" , "/test?p=1" },
466+ },
467+ // both set
468+ {
469+ httpPrefix : "/testprefix" , wsPrefix : "/testprefix" ,
470+ wantHTTP : []string {"/testprefix" , "/testprefix?p=1" , "/testprefix/x" , "/testprefix/x?p=1" },
471+ wantNoHTTP : []string {"/" , "/?p=1" , "/test" , "/test?p=1" },
472+ wantWS : []string {"/testprefix" , "/testprefix?p=1" , "/testprefix/x" , "/testprefix/x?p=1" },
473+ wantNoWS : []string {"/" , "/?p=1" , "/test" , "/test?p=1" },
474+ },
475+ }
476+
477+ for _ , test := range tests {
478+ test := test
479+ name := fmt .Sprintf ("http=%s ws=%s" , test .httpPrefix , test .wsPrefix )
480+ t .Run (name , func (t * testing.T ) {
481+ cfg := & Config {
482+ HTTPHost : "127.0.0.1" ,
483+ HTTPPathPrefix : test .httpPrefix ,
484+ WSHost : "127.0.0.1" ,
485+ WSPathPrefix : test .wsPrefix ,
486+ }
487+ node , err := New (cfg )
488+ if err != nil {
489+ t .Fatal ("can't create node:" , err )
490+ }
491+ defer node .Close ()
492+ if err := node .Start (); err != nil {
493+ t .Fatal ("can't start node:" , err )
494+ }
495+ test .check (t , node )
496+ })
497+ }
498+ }
499+
500+ func (test rpcPrefixTest ) check (t * testing.T , node * Node ) {
501+ t .Helper ()
502+ httpBase := "http://" + node .http .listenAddr ()
503+ wsBase := "ws://" + node .http .listenAddr ()
504+
505+ if node .WSEndpoint () != wsBase + test .wsPrefix {
506+ t .Errorf ("Error: node has wrong WSEndpoint %q" , node .WSEndpoint ())
507+ }
508+
509+ for _ , path := range test .wantHTTP {
510+ resp := rpcRequest (t , httpBase + path )
511+ if resp .StatusCode != 200 {
512+ t .Errorf ("Error: %s: bad status code %d, want 200" , path , resp .StatusCode )
513+ }
514+ }
515+ for _ , path := range test .wantNoHTTP {
516+ resp := rpcRequest (t , httpBase + path )
517+ if resp .StatusCode != 404 {
518+ t .Errorf ("Error: %s: bad status code %d, want 404" , path , resp .StatusCode )
519+ }
520+ }
521+ for _ , path := range test .wantWS {
522+ err := wsRequest (t , wsBase + path , "" )
523+ if err != nil {
524+ t .Errorf ("Error: %s: WebSocket connection failed: %v" , path , err )
525+ }
526+ }
527+ for _ , path := range test .wantNoWS {
528+ err := wsRequest (t , wsBase + path , "" )
529+ if err == nil {
530+ t .Errorf ("Error: %s: WebSocket connection succeeded for path in wantNoWS" , path )
531+ }
532+
533+ }
534+ }
535+
430536func TestWebsocketHTTPOnSeparatePort_WSRequest (t * testing.T ) {
431537 // try and get a free port
432538 listener , err := net .Listen ("tcp" , "127.0.0.1:0" )
0 commit comments