github.com/blend/go-sdk@v1.20220411.3/webutil/get_host_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package webutil
     9  
    10  import (
    11  	"net/http"
    12  	"net/url"
    13  	"testing"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  )
    17  
    18  func TestGetHost(t *testing.T) {
    19  	assert := assert.New(t)
    20  
    21  	r := http.Request{
    22  		Host: "local.test.com",
    23  	}
    24  	assert.Equal("local.test.com", GetHost(&r))
    25  
    26  	r = http.Request{
    27  		Host: "local.test.com:8080",
    28  	}
    29  	assert.Equal("local.test.com", GetHost(&r))
    30  
    31  	r = http.Request{
    32  		URL:  &url.URL{},
    33  		Host: "local.test.com:8080",
    34  	}
    35  	assert.Equal("local.test.com", GetHost(&r))
    36  
    37  	r = http.Request{
    38  		URL:  &url.URL{Host: "local.foo.com"},
    39  		Host: "local.test.com:8080",
    40  	}
    41  	assert.Equal("local.foo.com", GetHost(&r))
    42  
    43  	headers := http.Header{}
    44  	headers.Set("X-Forwarded-Proto", "spdy,https")
    45  	headers.Set("X-Forwarded-Host", "local.bar.com")
    46  	r = http.Request{
    47  		URL:    &url.URL{Host: "local.foo.com"},
    48  		Host:   "local.test.com:8080",
    49  		Header: headers,
    50  	}
    51  	assert.Equal("local.bar.com", GetHost(&r))
    52  }