github.com/InjectiveLabs/sdk-go@v1.53.0/client/common/network_test.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"google.golang.org/grpc/metadata"
     9  )
    10  
    11  func TestMainnetKubernetesLoadBalancedCookieAssistant(t *testing.T) {
    12  	assistant := MainnetKubernetesCookieAssistant()
    13  	expectedCookie := fmt.Sprintf("GCLB=CMOO2-DdvKWMqQE; path=/; HttpOnly; expires=%s", time.Now().Add(5*time.Hour).Format("Mon, 02-Jan-2006 15:04:05 MST"))
    14  
    15  	localMetadata := metadata.Pairs("set-cookie", expectedCookie)
    16  
    17  	assistant.ProcessResponseMetadata(localMetadata)
    18  	assistantMetadata := assistant.RealMetadata()
    19  	cookieInfo := assistantMetadata.Get("cookie")
    20  
    21  	if len(cookieInfo) > 0 {
    22  		cookie := cookieInfo[0]
    23  		if cookie != expectedCookie {
    24  			t.Fatalf("The parsed cookie is different than the expected cookie")
    25  		}
    26  	} else {
    27  		t.Fatalf("The cookie was not parsed")
    28  	}
    29  }
    30  
    31  func TestMainnetKubernetesLoadBalancedCookieAssistantRemovesExpiredCookie(t *testing.T) {
    32  	assistant := MainnetKubernetesCookieAssistant()
    33  	tt := time.Now()
    34  	closeExpirationTime := tt.Add(-30 * time.Second)
    35  
    36  	expiredCookie := fmt.Sprintf(
    37  		"GCLB=CMOO2-DdvKWMqQE; path=/; HttpOnly; expires=%s",
    38  		closeExpirationTime.Format("Mon, 02-Jan-2006 15:04:05 MST"),
    39  	)
    40  
    41  	localMetadata := metadata.Pairs("set-cookie", expiredCookie)
    42  
    43  	assistant.ProcessResponseMetadata(localMetadata)
    44  	cookieInfo := assistant.RealMetadata()
    45  
    46  	if len(cookieInfo) > 0 {
    47  		t.Fatalf("The expired cookie was not removed")
    48  	}
    49  
    50  }
    51  
    52  func TestTestnetKubernetesLoadBalancedCookieAssistant(t *testing.T) {
    53  	assistant := TestnetKubernetesCookieAssistant()
    54  	expectedCookie := fmt.Sprintf(
    55  		"grpc-cookie=d97c7a00bcb7bc8b69b26fb0303b60d4; Expires=%s; Max-Age=172800; Path=/; Secure; HttpOnly",
    56  		time.Now().Add(5*time.Hour).Format("Mon, 02-Jan-06 15:04:05 MST"),
    57  	)
    58  
    59  	localMetadata := metadata.Pairs("set-cookie", expectedCookie)
    60  
    61  	assistant.ProcessResponseMetadata(localMetadata)
    62  	assistantMetadata := assistant.RealMetadata()
    63  	cookieInfo := assistantMetadata.Get("cookie")
    64  
    65  	if len(cookieInfo) > 0 {
    66  		cookie := cookieInfo[0]
    67  		if cookie != expectedCookie {
    68  			t.Fatalf("The parsed cookie is different than the expected cookie")
    69  		}
    70  	} else {
    71  		t.Fatalf("The cookie was not parsed")
    72  	}
    73  }
    74  
    75  func TestTestnetKubernetesLoadBalancedCookieAssistantRemovesExpiredCookie(t *testing.T) {
    76  	assistant := TestnetKubernetesCookieAssistant()
    77  	tt := time.Now()
    78  	closeExpirationTime := tt.Add(-30 * time.Second)
    79  
    80  	expiredCookie := fmt.Sprintf(
    81  		"grpc-cookie=d97c7a00bcb7bc8b69b26fb0303b60d4; Expires=%s; Max-Age=172800; Path=/; Secure; HttpOnly",
    82  		closeExpirationTime.Format("Mon, 02-Jan-06 15:04:05 MST"),
    83  	)
    84  
    85  	localMetadata := metadata.Pairs("set-cookie", expiredCookie)
    86  
    87  	assistant.ProcessResponseMetadata(localMetadata)
    88  	cookieInfo := assistant.RealMetadata()
    89  
    90  	if len(cookieInfo) > 0 {
    91  		t.Fatalf("The expired cookie was not removed")
    92  	}
    93  
    94  }
    95  
    96  func TestBareMetalLoadBalancedCookieAssistant(t *testing.T) {
    97  	assistant := BareMetalLoadBalancedCookieAssistant{}
    98  	expectedCookie := "lb=706c9476f31159d415afe3e9172972618b8ab99455c2daa799199540e6d31a1a; Path=/"
    99  
   100  	localMetadata := metadata.Pairs("set-cookie", expectedCookie)
   101  
   102  	assistant.ProcessResponseMetadata(localMetadata)
   103  	assistantMetadata := assistant.RealMetadata()
   104  	cookieInfo := assistantMetadata.Get("cookie")
   105  
   106  	if len(cookieInfo) > 0 {
   107  		cookie := cookieInfo[0]
   108  		if cookie != expectedCookie {
   109  			t.Fatalf("The parsed cookie is different than the expected cookie")
   110  		}
   111  	} else {
   112  		t.Fatalf("The cookie was not parsed")
   113  	}
   114  }
   115  
   116  func TestDisabledCookieAssistant(t *testing.T) {
   117  	assistant := DisabledCookieAssistant{}
   118  	providedCookie := "lb=706c9476f31159d415afe3e9172972618b8ab99455c2daa799199540e6d31a1a; Path=/"
   119  
   120  	localMetadata := metadata.Pairs("set-cookie", providedCookie)
   121  
   122  	assistant.ProcessResponseMetadata(localMetadata)
   123  	assistantMetadata := assistant.RealMetadata()
   124  	cookieInfo := assistantMetadata.Get("cookie")
   125  
   126  	if len(cookieInfo) > 0 {
   127  		t.Fatalf("The disabled cookie assistant should not return any cookie")
   128  	}
   129  }