github.com/waldiirawan/apm-agent-go/v2@v2.2.2/internal/apmhttputil/remoteaddr_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 "net/http" 22 "net/url" 23 "testing" 24 25 "github.com/stretchr/testify/assert" 26 "github.com/stretchr/testify/require" 27 28 "github.com/waldiirawan/apm-agent-go/v2/internal/apmhttputil" 29 ) 30 31 func TestRemoteAddr(t *testing.T) { 32 req := &http.Request{ 33 RemoteAddr: "[::1]:1234", 34 Header: make(http.Header), 35 } 36 req.Header.Set("X-Forwarded-For", "client.invalid, proxy.invalid") 37 req.Header.Set("X-Real-IP", "127.1.2.3") 38 assert.Equal(t, "::1", apmhttputil.RemoteAddr(req)) 39 } 40 41 func TestDestinationAddr(t *testing.T) { 42 test := func(u, expectAddr string, expectPort int) { 43 t.Run(u, func(t *testing.T) { 44 url, err := url.Parse(u) 45 require.NoError(t, err) 46 47 addr, port := apmhttputil.DestinationAddr(&http.Request{URL: url}) 48 assert.Equal(t, expectAddr, addr) 49 assert.Equal(t, expectPort, port) 50 }) 51 } 52 test("http://127.0.0.1:80", "127.0.0.1", 80) 53 test("http://127.0.0.1", "127.0.0.1", 80) 54 test("https://127.0.0.1:443", "127.0.0.1", 443) 55 test("https://127.0.0.1", "127.0.0.1", 443) 56 test("https://[::1]", "::1", 443) 57 test("https://[::1]:1234", "::1", 1234) 58 test("gopher://gopher.invalid:70", "gopher.invalid", 70) 59 test("gopher://gopher.invalid", "gopher.invalid", 0) // default unknown 60 }