github.com/newrelic/go-agent@v3.26.0+incompatible/browser_header_test.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package newrelic
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/newrelic/go-agent/internal"
    11  )
    12  
    13  func TestNilBrowserTimingHeader(t *testing.T) {
    14  	var h *BrowserTimingHeader
    15  
    16  	// The methods on a nil BrowserTimingHeader pointer should not panic.
    17  
    18  	if out := h.WithTags(); out != nil {
    19  		t.Errorf("unexpected WithTags output for a disabled header: expected a blank string; got %s", out)
    20  	}
    21  
    22  	if out := h.WithoutTags(); out != nil {
    23  		t.Errorf("unexpected WithoutTags output for a disabled header: expected a blank string; got %s", out)
    24  	}
    25  }
    26  
    27  func TestEnabled(t *testing.T) {
    28  	// We're not trying to test Go's JSON marshalling here; we just want to
    29  	// ensure that we get the right fields out the other side.
    30  	expectInfo := internal.CompactJSONString(`
    31      {
    32        "beacon": "brecon",
    33        "licenseKey": "12345",
    34        "applicationID": "app",
    35        "transactionName": "txn",
    36        "queueTime": 1,
    37        "applicationTime": 2,
    38        "atts": "attrs",
    39        "errorBeacon": "blah",
    40        "agent": "bond"
    41      }
    42    `)
    43  
    44  	h := &BrowserTimingHeader{
    45  		agentLoader: "loader();",
    46  		info: browserInfo{
    47  			Beacon:                "brecon",
    48  			LicenseKey:            "12345",
    49  			ApplicationID:         "app",
    50  			TransactionName:       "txn",
    51  			QueueTimeMillis:       1,
    52  			ApplicationTimeMillis: 2,
    53  			ObfuscatedAttributes:  "attrs",
    54  			ErrorBeacon:           "blah",
    55  			Agent:                 "bond",
    56  		},
    57  	}
    58  
    59  	expected := fmt.Sprintf("%s%s%s%s%s", browserStartTag, h.agentLoader, browserInfoPrefix, expectInfo, browserEndTag)
    60  	if actual := h.WithTags(); string(actual) != expected {
    61  		t.Errorf("unexpected WithTags output: expected %s; got %s", expected, string(actual))
    62  	}
    63  
    64  	expected = fmt.Sprintf("%s%s%s", h.agentLoader, browserInfoPrefix, expectInfo)
    65  	if actual := h.WithoutTags(); string(actual) != expected {
    66  		t.Errorf("unexpected WithoutTags output: expected %s; got %s", expected, string(actual))
    67  	}
    68  }