github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/agent_test.go (about)

     1  // (c) Copyright IBM Corp. 2022
     2  
     3  package instana
     4  
     5  import (
     6  	"bytes"
     7  	"encoding/json"
     8  	"io/ioutil"
     9  	"net/http"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/instana/testify/assert"
    14  )
    15  
    16  func Test_agentS_SendSpans(t *testing.T) {
    17  	tests := []struct {
    18  		name  string
    19  		spans []Span
    20  	}{
    21  		{
    22  			name: "big span",
    23  			spans: []Span{
    24  				{
    25  					Data: HTTPSpanData{
    26  						Tags: HTTPSpanTags{
    27  							URL: strings.Repeat("1", maxContentLength),
    28  						},
    29  					},
    30  				},
    31  			},
    32  		},
    33  		{
    34  			name: "multiple big span",
    35  			spans: []Span{
    36  				{Data: HTTPSpanData{Tags: HTTPSpanTags{URL: strings.Repeat("1", maxContentLength)}}},
    37  				{Data: HTTPSpanData{Tags: HTTPSpanTags{URL: strings.Repeat("1", maxContentLength)}}},
    38  				{Data: HTTPSpanData{Tags: HTTPSpanTags{URL: strings.Repeat("1", maxContentLength)}}},
    39  				{Data: HTTPSpanData{Tags: HTTPSpanTags{URL: strings.Repeat("1", maxContentLength)}}},
    40  				{Data: HTTPSpanData{Tags: HTTPSpanTags{URL: strings.Repeat("1", maxContentLength)}}},
    41  				{Data: HTTPSpanData{Tags: HTTPSpanTags{URL: strings.Repeat("1", maxContentLength)}}},
    42  			},
    43  		},
    44  		{
    45  			name: "not really a big span",
    46  			spans: []Span{
    47  				{
    48  					Data: HTTPSpanData{
    49  						Tags: HTTPSpanTags{
    50  							URL: strings.Repeat("1", maxContentLength/2),
    51  						},
    52  					},
    53  				},
    54  			},
    55  		},
    56  	}
    57  
    58  	for _, tt := range tests {
    59  		t.Run(tt.name, func(t *testing.T) {
    60  			agent := &agentS{host: "", from: &fromS{}, logger: defaultLogger, client: &httpClientMock{
    61  				resp: &http.Response{
    62  					StatusCode: 200,
    63  					Body:       ioutil.NopCloser(bytes.NewReader([]byte("{}"))),
    64  				},
    65  			}}
    66  			err := agent.SendSpans(tt.spans)
    67  
    68  			assert.NoError(t, err)
    69  		})
    70  	}
    71  }
    72  
    73  type httpClientMock struct {
    74  	resp *http.Response
    75  	err  error
    76  }
    77  
    78  func (h httpClientMock) Do(req *http.Request) (*http.Response, error) {
    79  	return h.resp, h.err
    80  }
    81  
    82  func Test_agentResponse_getExtraHTTPHeaders(t *testing.T) {
    83  
    84  	tests := []struct {
    85  		name         string
    86  		originalJSON string
    87  		want         []string
    88  	}{
    89  		{
    90  			name:         "old config",
    91  			originalJSON: `{"pid":37808,"agentUuid":"88:66:5a:ff:fe:05:a5:f0","extraHeaders":["expected-value"],"secrets":{"matcher":"contains-ignore-case","list":["key","pass","secret"]}}`,
    92  			want:         []string{"expected-value"},
    93  		},
    94  		{
    95  			name:         "new config",
    96  			originalJSON: `{"pid":38381,"agentUuid":"88:66:5a:ff:fe:05:a5:f0","tracing":{"extra-http-headers":["expected-value"]},"extraHeaders":["non-expected-value"],"secrets":{"matcher":"contains-ignore-case","list":["key","pass","secret"]}}`,
    97  			want:         []string{"expected-value"},
    98  		},
    99  	}
   100  
   101  	for _, tt := range tests {
   102  		t.Run(tt.name, func(t *testing.T) {
   103  			r := &agentResponse{}
   104  			json.Unmarshal([]byte(tt.originalJSON), r)
   105  			assert.Equalf(t, tt.want, r.getExtraHTTPHeaders(), "getExtraHTTPHeaders()")
   106  		})
   107  	}
   108  }
   109  
   110  func Test_agentApplyHostSettings(t *testing.T) {
   111  	agent := &agentS{}
   112  	response := agentResponse{
   113  		Pid:    37892,
   114  		HostID: "myhost",
   115  		Tracing: struct {
   116  			ExtraHTTPHeaders []string `json:"extra-http-headers"`
   117  		}{
   118  			ExtraHTTPHeaders: []string{"my-unwanted-custom-headers"},
   119  		},
   120  	}
   121  
   122  	agent.applyHostAgentSettings(response)
   123  
   124  	assert.NotContains(t, sensor.options.Tracer.CollectableHTTPHeaders, "my-unwanted-custom-headers")
   125  }