github.com/yandex/pandora@v0.5.32/components/providers/http/util/request_test.go (about)

     1  package util
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func BenchmarkGetKeyValue(b *testing.B) {
    13  	var h = "[hello: world]"
    14  	for i := 0; i < b.N; i++ {
    15  		_, _, _ = DecodeHeader(h)
    16  	}
    17  }
    18  
    19  type GetKeyValueWant struct {
    20  	key, value string
    21  	err        error
    22  }
    23  
    24  func TestGetKeyValue(t *testing.T) {
    25  	var tests = []struct {
    26  		name  string
    27  		input string
    28  		want  GetKeyValueWant
    29  	}{
    30  		{
    31  			name:  "empty input",
    32  			input: "",
    33  			want: GetKeyValueWant{
    34  				"",
    35  				"",
    36  				ErrHeaderFormat,
    37  			},
    38  		},
    39  		{
    40  			name:  "no colon",
    41  			input: "[key value]",
    42  			want: GetKeyValueWant{
    43  				"key value",
    44  				"",
    45  				ErrHeaderFormat,
    46  			},
    47  		},
    48  		{
    49  			name:  "simple input",
    50  			input: "[key: value]",
    51  			want: GetKeyValueWant{
    52  				"key",
    53  				"value",
    54  				nil,
    55  			},
    56  		},
    57  		{
    58  			name:  "empty output exected",
    59  			input: "[:]",
    60  			want: GetKeyValueWant{
    61  				"",
    62  				"",
    63  				ErrEmptyKey,
    64  			},
    65  		},
    66  	}
    67  	var ans GetKeyValueWant
    68  	for _, tt := range tests {
    69  		t.Run(tt.name, func(t *testing.T) {
    70  			assert := assert.New(t)
    71  			ans.key, ans.value, ans.err = DecodeHeader(tt.input)
    72  			assert.EqualValues(tt.want, ans)
    73  		})
    74  	}
    75  }
    76  
    77  type DecodeHTTPConfigHeadersWant struct {
    78  	ans map[string][]string
    79  	err error
    80  }
    81  
    82  func TestDecodeHTTPConfigHeaders(t *testing.T) {
    83  	var tests = []struct {
    84  		name  string
    85  		input []string
    86  		want  DecodeHTTPConfigHeadersWant
    87  	}{
    88  		{
    89  			name: "decode http config headers",
    90  			input: []string{
    91  				"[Host: youhost.tld]",
    92  				"[SomeHeader: somevalue]",
    93  				"[soMeHeAdEr: secondvalue]",
    94  			},
    95  			want: DecodeHTTPConfigHeadersWant{
    96  				map[string][]string{
    97  					"Host":       {"youhost.tld"},
    98  					"Someheader": {"somevalue", "secondvalue"},
    99  				},
   100  				nil,
   101  			},
   102  		},
   103  		{
   104  			name: "parse error",
   105  			input: []string{
   106  				"",
   107  			},
   108  			want: DecodeHTTPConfigHeadersWant{
   109  				map[string][]string{},
   110  				ErrHeaderFormat,
   111  			},
   112  		},
   113  	}
   114  	for _, tt := range tests {
   115  		t.Run(tt.name, func(t *testing.T) {
   116  			assert := assert.New(t)
   117  			ans, err := DecodeHTTPConfigHeaders(tt.input)
   118  			assert.EqualValues(tt.want.ans, ans)
   119  			assert.EqualValues(tt.want.err, err)
   120  		})
   121  	}
   122  }
   123  
   124  type EnrichRequestWithHeadersInput struct {
   125  	req     func() *http.Request
   126  	headers http.Header
   127  }
   128  
   129  type EnrichRequestWithHeadersWant struct {
   130  	Host string
   131  	http.Header
   132  }
   133  
   134  func TestEnrichRequestWithHeaders(t *testing.T) {
   135  	const origHost = "example.com"
   136  	defaultReq, _ := http.NewRequest("GET", origHost, nil)
   137  
   138  	var tests = []struct {
   139  		name  string
   140  		input EnrichRequestWithHeadersInput
   141  		want  EnrichRequestWithHeadersWant
   142  	}{
   143  		{
   144  			name: "add new http headers",
   145  			input: EnrichRequestWithHeadersInput{
   146  				req: func() (r *http.Request) {
   147  					r = defaultReq.Clone(context.Background())
   148  					r.Host = origHost
   149  					r.Header.Set("SomeHeader", "oldvalue")
   150  					return
   151  				},
   152  				headers: http.Header{
   153  					"Host":         {"youhost.tld"},
   154  					"Someheader":   {"newvalue"},
   155  					"Secondheader": {"new_second_value"},
   156  				},
   157  			},
   158  			want: EnrichRequestWithHeadersWant{
   159  				Host: origHost,
   160  				Header: http.Header{
   161  					"Someheader":   []string{"oldvalue"},
   162  					"Secondheader": []string{"new_second_value"},
   163  				},
   164  			},
   165  		},
   166  		{
   167  			name: "add new host",
   168  			input: EnrichRequestWithHeadersInput{
   169  				req: func() (r *http.Request) {
   170  					r = defaultReq.Clone(context.Background())
   171  					return
   172  				},
   173  				headers: http.Header{
   174  					"Host": {"youhost.tld"},
   175  				},
   176  			},
   177  			want: EnrichRequestWithHeadersWant{
   178  				Host:   "youhost.tld",
   179  				Header: http.Header{},
   180  			},
   181  		},
   182  	}
   183  
   184  	for _, tt := range tests {
   185  		t.Run(tt.name, func(t *testing.T) {
   186  			assert := assert.New(t)
   187  			req := tt.input.req()
   188  			fmt.Println(req)
   189  			EnrichRequestWithHeaders(req, tt.input.headers)
   190  			fmt.Println(req, req.Host)
   191  			assert.EqualValues(tt.want.Host, req.Host)
   192  			assert.EqualValues(tt.want.Header, req.Header)
   193  		})
   194  	}
   195  }