github.com/grafana/pyroscope@v1.18.0/cmd/profilecli/client_test.go (about)

     1  package main
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func Test_AcceptHeader(t *testing.T) {
    11  	tests := []struct {
    12  		Name               string
    13  		Header             http.Header
    14  		ClientCapabilities []string
    15  		Want               []string
    16  	}{
    17  		{
    18  			Name:   "empty header adds capability",
    19  			Header: http.Header{},
    20  			ClientCapabilities: []string{
    21  				"allow-utf8-labelnames=true",
    22  			},
    23  			Want: []string{"*/*;allow-utf8-labelnames=true"},
    24  		},
    25  		{
    26  			Name: "existing header appends capability",
    27  			Header: http.Header{
    28  				"Accept": []string{"application/json"},
    29  			},
    30  			ClientCapabilities: []string{
    31  				"allow-utf8-labelnames=true",
    32  			},
    33  			Want: []string{"application/json", "*/*;allow-utf8-labelnames=true"},
    34  		},
    35  		{
    36  			Name: "multiple existing values appends capability",
    37  			Header: http.Header{
    38  				"Accept": []string{"application/json", "text/plain"},
    39  			},
    40  			ClientCapabilities: []string{
    41  				"allow-utf8-labelnames=true",
    42  			},
    43  			Want: []string{"application/json", "text/plain", "*/*;allow-utf8-labelnames=true"},
    44  		},
    45  		{
    46  			Name: "existing capability is not duplicated",
    47  			Header: http.Header{
    48  				"Accept": []string{"*/*;allow-utf8-labelnames=true"},
    49  			},
    50  			ClientCapabilities: []string{
    51  				"allow-utf8-labelnames=true",
    52  			},
    53  			Want: []string{"*/*;allow-utf8-labelnames=true"},
    54  		},
    55  		{
    56  			Name: "multiple client capabilities appends capability",
    57  			Header: http.Header{
    58  				"Accept": []string{"*/*;allow-utf8-labelnames=true"},
    59  			},
    60  			ClientCapabilities: []string{
    61  				"allow-utf8-labelnames=true",
    62  				"capability2=false",
    63  			},
    64  			Want: []string{"*/*;allow-utf8-labelnames=true", "*/*;capability2=false"},
    65  		},
    66  	}
    67  
    68  	for _, tt := range tests {
    69  		t.Run(tt.Name, func(t *testing.T) {
    70  			t.Parallel()
    71  			req, _ := http.NewRequest("GET", "example.com", nil)
    72  			req.Header = tt.Header
    73  			clientCapabilities := tt.ClientCapabilities
    74  
    75  			addClientCapabilitiesHeader(req, acceptHeaderMimeType, clientCapabilities)
    76  			require.Equal(t, tt.Want, req.Header.Values("Accept"))
    77  		})
    78  	}
    79  }