@@ -390,7 +390,7 @@ func TestLifecycleTerminationGuarantee(t *testing.T) {
390
390
}
391
391
392
392
// Tests whether a handler can be successfully mounted on the canonical HTTP server
393
- // on the given path
393
+ // on the given prefix
394
394
func TestRegisterHandler_Successful (t * testing.T ) {
395
395
node := createNode (t , 7878 , 7979 )
396
396
@@ -483,7 +483,112 @@ func TestWebsocketHTTPOnSeparatePort_WSRequest(t *testing.T) {
483
483
if ! checkRPC (node .HTTPEndpoint ()) {
484
484
t .Fatalf ("http request failed" )
485
485
}
486
+ }
487
+
488
+ type rpcPrefixTest struct {
489
+ httpPrefix , wsPrefix string
490
+ // These lists paths on which JSON-RPC should be served / not served.
491
+ wantHTTP []string
492
+ wantNoHTTP []string
493
+ wantWS []string
494
+ wantNoWS []string
495
+ }
496
+
497
+ func TestNodeRPCPrefix (t * testing.T ) {
498
+ t .Parallel ()
499
+
500
+ tests := []rpcPrefixTest {
501
+ // both off
502
+ {
503
+ httpPrefix : "" , wsPrefix : "" ,
504
+ wantHTTP : []string {"/" , "/?p=1" },
505
+ wantNoHTTP : []string {"/test" , "/test?p=1" },
506
+ wantWS : []string {"/" , "/?p=1" },
507
+ wantNoWS : []string {"/test" , "/test?p=1" },
508
+ },
509
+ // only http prefix
510
+ {
511
+ httpPrefix : "/testprefix" , wsPrefix : "" ,
512
+ wantHTTP : []string {"/testprefix" , "/testprefix?p=1" , "/testprefix/x" , "/testprefix/x?p=1" },
513
+ wantNoHTTP : []string {"/" , "/?p=1" , "/test" , "/test?p=1" },
514
+ wantWS : []string {"/" , "/?p=1" },
515
+ wantNoWS : []string {"/testprefix" , "/testprefix?p=1" , "/test" , "/test?p=1" },
516
+ },
517
+ // only ws prefix
518
+ {
519
+ httpPrefix : "" , wsPrefix : "/testprefix" ,
520
+ wantHTTP : []string {"/" , "/?p=1" },
521
+ wantNoHTTP : []string {"/testprefix" , "/testprefix?p=1" , "/test" , "/test?p=1" },
522
+ wantWS : []string {"/testprefix" , "/testprefix?p=1" , "/testprefix/x" , "/testprefix/x?p=1" },
523
+ wantNoWS : []string {"/" , "/?p=1" , "/test" , "/test?p=1" },
524
+ },
525
+ // both set
526
+ {
527
+ httpPrefix : "/testprefix" , wsPrefix : "/testprefix" ,
528
+ wantHTTP : []string {"/testprefix" , "/testprefix?p=1" , "/testprefix/x" , "/testprefix/x?p=1" },
529
+ wantNoHTTP : []string {"/" , "/?p=1" , "/test" , "/test?p=1" },
530
+ wantWS : []string {"/testprefix" , "/testprefix?p=1" , "/testprefix/x" , "/testprefix/x?p=1" },
531
+ wantNoWS : []string {"/" , "/?p=1" , "/test" , "/test?p=1" },
532
+ },
533
+ }
534
+
535
+ for _ , test := range tests {
536
+ test := test
537
+ name := fmt .Sprintf ("http=%s ws=%s" , test .httpPrefix , test .wsPrefix )
538
+ t .Run (name , func (t * testing.T ) {
539
+ cfg := & Config {
540
+ HTTPHost : "127.0.0.1" ,
541
+ HTTPPathPrefix : test .httpPrefix ,
542
+ WSHost : "127.0.0.1" ,
543
+ WSPathPrefix : test .wsPrefix ,
544
+ }
545
+ node , err := New (cfg )
546
+ if err != nil {
547
+ t .Fatal ("can't create node:" , err )
548
+ }
549
+ defer node .Close ()
550
+ if err := node .Start (); err != nil {
551
+ t .Fatal ("can't start node:" , err )
552
+ }
553
+ test .check (t , node )
554
+ })
555
+ }
556
+ }
557
+
558
+ func (test rpcPrefixTest ) check (t * testing.T , node * Node ) {
559
+ t .Helper ()
560
+ httpBase := "http://" + node .http .listenAddr ()
561
+ wsBase := "ws://" + node .http .listenAddr ()
562
+
563
+ if node .WSEndpoint () != wsBase + test .wsPrefix {
564
+ t .Errorf ("Error: node has wrong WSEndpoint %q" , node .WSEndpoint ())
565
+ }
566
+
567
+ for _ , path := range test .wantHTTP {
568
+ resp := rpcRequest (t , httpBase + path )
569
+ if resp .StatusCode != 200 {
570
+ t .Errorf ("Error: %s: bad status code %d, want 200" , path , resp .StatusCode )
571
+ }
572
+ }
573
+ for _ , path := range test .wantNoHTTP {
574
+ resp := rpcRequest (t , httpBase + path )
575
+ if resp .StatusCode != 404 {
576
+ t .Errorf ("Error: %s: bad status code %d, want 404" , path , resp .StatusCode )
577
+ }
578
+ }
579
+ for _ , path := range test .wantWS {
580
+ err := wsRequest (t , wsBase + path , "" )
581
+ if err != nil {
582
+ t .Errorf ("Error: %s: WebSocket connection failed: %v" , path , err )
583
+ }
584
+ }
585
+ for _ , path := range test .wantNoWS {
586
+ err := wsRequest (t , wsBase + path , "" )
587
+ if err == nil {
588
+ t .Errorf ("Error: %s: WebSocket connection succeeded for path in wantNoWS" , path )
589
+ }
486
590
591
+ }
487
592
}
488
593
489
594
func createNode (t * testing.T , httpPort , wsPort int ) * Node {
0 commit comments