github.com/waldiirawan/apm-agent-go/v2@v2.2.2/internal/apmhttputil/forwarded_test.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package apmhttputil_test
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  
    25  	"github.com/waldiirawan/apm-agent-go/v2/internal/apmhttputil"
    26  )
    27  
    28  func TestParseForwarded(t *testing.T) {
    29  	type test struct {
    30  		name   string
    31  		header string
    32  		expect apmhttputil.ForwardedHeader
    33  	}
    34  
    35  	tests := []test{{
    36  		name:   "Forwarded",
    37  		header: "by=127.0.0.1; for=127.1.1.1; Host=\"forwarded.invalid:443\"; proto=HTTPS",
    38  		expect: apmhttputil.ForwardedHeader{
    39  			For:   "127.1.1.1",
    40  			Host:  "forwarded.invalid:443",
    41  			Proto: "HTTPS",
    42  		},
    43  	}, {
    44  		name:   "Forwarded-Multi",
    45  		header: "host=first.invalid, host=second.invalid",
    46  		expect: apmhttputil.ForwardedHeader{
    47  			Host: "first.invalid",
    48  		},
    49  	}, {
    50  		name:   "Forwarded-Malformed-Fields-Ignored",
    51  		header: "what; nonsense=\"; host=first.invalid",
    52  		expect: apmhttputil.ForwardedHeader{
    53  			Host: "first.invalid",
    54  		},
    55  	}, {
    56  		name:   "Forwarded-Trailing-Separators",
    57  		header: "host=first.invalid;,",
    58  		expect: apmhttputil.ForwardedHeader{
    59  			Host: "first.invalid",
    60  		},
    61  	}}
    62  
    63  	for _, test := range tests {
    64  		t.Run(test.name, func(t *testing.T) {
    65  			parsed := apmhttputil.ParseForwarded(test.header)
    66  			assert.Equal(t, test.expect, parsed)
    67  		})
    68  	}
    69  }