github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/cross_process_http_test.go (about)

     1  package internal
     2  
     3  import (
     4  	"net/http"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/lulzWill/go-agent/internal/cat"
     9  )
    10  
    11  func TestTxnCrossProcessInitFromHTTPRequest(t *testing.T) {
    12  	txp := &TxnCrossProcess{}
    13  	if err := txp.InitFromHTTPRequest(true, replyAccountOne, nil); err != nil {
    14  		t.Errorf("got error while initialising without a request: %v", err)
    15  	}
    16  	if txp.IsInbound() {
    17  		t.Error("inbound CAT enabled even though there was no request")
    18  	}
    19  
    20  	txp = &TxnCrossProcess{}
    21  	req, err := http.NewRequest("GET", "http://foo.bar/", nil)
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	if err := txp.InitFromHTTPRequest(true, replyAccountOne, req); err != nil {
    26  		t.Errorf("got error while initialising with an empty request: %v", err)
    27  	}
    28  	if txp.IsInbound() {
    29  		t.Error("inbound CAT enabled even though there was no metadata in the request")
    30  	}
    31  
    32  	txp = &TxnCrossProcess{}
    33  	req, err = http.NewRequest("GET", "http://foo.bar/", nil)
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	req.Header.Add(cat.NewRelicIDName, mustObfuscate(`1#1`, "foo"))
    38  	req.Header.Add(cat.NewRelicTxnName, mustObfuscate(`["abcdefgh",false,"12345678","b95be233"]`, "foo"))
    39  	if err := txp.InitFromHTTPRequest(true, replyAccountOne, req); err != nil {
    40  		t.Errorf("got error while initialising with an inbound CAT request: %v", err)
    41  	}
    42  	if !txp.IsInbound() {
    43  		t.Error("inbound CAT disabled even though there was metadata in the request")
    44  	}
    45  	if txp.ClientID != "1#1" {
    46  		t.Errorf("incorrect ClientID: %s", txp.ClientID)
    47  	}
    48  	if txp.ReferringTxnGUID != "abcdefgh" {
    49  		t.Errorf("incorrect ReferringTxnGUID: %s", txp.ReferringTxnGUID)
    50  	}
    51  	if txp.TripID != "12345678" {
    52  		t.Errorf("incorrect TripID: %s", txp.TripID)
    53  	}
    54  	if txp.ReferringPathHash != "b95be233" {
    55  		t.Errorf("incorrect ReferringPathHash: %s", txp.ReferringPathHash)
    56  	}
    57  }
    58  
    59  func TestAppDataToHTTPHeader(t *testing.T) {
    60  	header := AppDataToHTTPHeader("")
    61  	if len(header) != 0 {
    62  		t.Errorf("unexpected number of header elements: %d", len(header))
    63  	}
    64  
    65  	header = AppDataToHTTPHeader("foo")
    66  	if len(header) != 1 {
    67  		t.Errorf("unexpected number of header elements: %d", len(header))
    68  	}
    69  	if actual := header.Get(cat.NewRelicAppDataName); actual != "foo" {
    70  		t.Errorf("unexpected header value: %s", actual)
    71  	}
    72  }
    73  
    74  func TestHTTPHeaderToAppData(t *testing.T) {
    75  	if appData := HTTPHeaderToAppData(nil); appData != "" {
    76  		t.Errorf("unexpected app data: %s", appData)
    77  	}
    78  
    79  	header := http.Header{}
    80  	if appData := HTTPHeaderToAppData(header); appData != "" {
    81  		t.Errorf("unexpected app data: %s", appData)
    82  	}
    83  
    84  	header.Add("X-Foo", "bar")
    85  	if appData := HTTPHeaderToAppData(header); appData != "" {
    86  		t.Errorf("unexpected app data: %s", appData)
    87  	}
    88  
    89  	header.Add(cat.NewRelicAppDataName, "foo")
    90  	if appData := HTTPHeaderToAppData(header); appData != "foo" {
    91  		t.Errorf("unexpected app data: %s", appData)
    92  	}
    93  }
    94  
    95  func TestHTTPHeaderToMetadata(t *testing.T) {
    96  	if metadata := HTTPHeaderToMetadata(nil); !reflect.DeepEqual(metadata, CrossProcessMetadata{}) {
    97  		t.Errorf("unexpected metadata: %v", metadata)
    98  	}
    99  
   100  	header := http.Header{}
   101  	if metadata := HTTPHeaderToMetadata(header); !reflect.DeepEqual(metadata, CrossProcessMetadata{}) {
   102  		t.Errorf("unexpected metadata: %v", metadata)
   103  	}
   104  
   105  	header.Add("X-Foo", "bar")
   106  	if metadata := HTTPHeaderToMetadata(header); !reflect.DeepEqual(metadata, CrossProcessMetadata{}) {
   107  		t.Errorf("unexpected metadata: %v", metadata)
   108  	}
   109  
   110  	header.Add(cat.NewRelicIDName, "id")
   111  	if metadata := HTTPHeaderToMetadata(header); !reflect.DeepEqual(metadata, CrossProcessMetadata{
   112  		ID: "id",
   113  	}) {
   114  		t.Errorf("unexpected metadata: %v", metadata)
   115  	}
   116  
   117  	header.Add(cat.NewRelicTxnName, "txn")
   118  	if metadata := HTTPHeaderToMetadata(header); !reflect.DeepEqual(metadata, CrossProcessMetadata{
   119  		ID:      "id",
   120  		TxnData: "txn",
   121  	}) {
   122  		t.Errorf("unexpected metadata: %v", metadata)
   123  	}
   124  
   125  	header.Add(cat.NewRelicSyntheticsName, "synth")
   126  	if metadata := HTTPHeaderToMetadata(header); !reflect.DeepEqual(metadata, CrossProcessMetadata{
   127  		ID:         "id",
   128  		TxnData:    "txn",
   129  		Synthetics: "synth",
   130  	}) {
   131  		t.Errorf("unexpected metadata: %v", metadata)
   132  	}
   133  }
   134  
   135  func TestMetadataToHTTPHeader(t *testing.T) {
   136  	metadata := CrossProcessMetadata{}
   137  
   138  	header := MetadataToHTTPHeader(metadata)
   139  	if len(header) != 0 {
   140  		t.Errorf("unexpected number of header elements: %d", len(header))
   141  	}
   142  
   143  	metadata.ID = "id"
   144  	header = MetadataToHTTPHeader(metadata)
   145  	if len(header) != 1 {
   146  		t.Errorf("unexpected number of header elements: %d", len(header))
   147  	}
   148  	if actual := header.Get(cat.NewRelicIDName); actual != "id" {
   149  		t.Errorf("unexpected header value: %s", actual)
   150  	}
   151  
   152  	metadata.TxnData = "txn"
   153  	header = MetadataToHTTPHeader(metadata)
   154  	if len(header) != 2 {
   155  		t.Errorf("unexpected number of header elements: %d", len(header))
   156  	}
   157  	if actual := header.Get(cat.NewRelicIDName); actual != "id" {
   158  		t.Errorf("unexpected header value: %s", actual)
   159  	}
   160  	if actual := header.Get(cat.NewRelicTxnName); actual != "txn" {
   161  		t.Errorf("unexpected header value: %s", actual)
   162  	}
   163  
   164  	metadata.Synthetics = "synth"
   165  	header = MetadataToHTTPHeader(metadata)
   166  	if len(header) != 3 {
   167  		t.Errorf("unexpected number of header elements: %d", len(header))
   168  	}
   169  	if actual := header.Get(cat.NewRelicIDName); actual != "id" {
   170  		t.Errorf("unexpected header value: %s", actual)
   171  	}
   172  	if actual := header.Get(cat.NewRelicTxnName); actual != "txn" {
   173  		t.Errorf("unexpected header value: %s", actual)
   174  	}
   175  	if actual := header.Get(cat.NewRelicSyntheticsName); actual != "synth" {
   176  		t.Errorf("unexpected header value: %s", actual)
   177  	}
   178  }