github.com/blend/go-sdk@v1.20220411.3/webutil/url_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  	"fmt"
    12  	"testing"
    13  
    14  	"github.com/blend/go-sdk/assert"
    15  )
    16  
    17  func TestMustParseURL(t *testing.T) {
    18  	assert := assert.New(t)
    19  
    20  	output := MustParseURL("https://foo.bar.com/example-string?buzz=muzz")
    21  	assert.NotNil(output)
    22  	assert.Equal("https", output.Scheme)
    23  	assert.Equal("foo.bar.com", output.Host)
    24  	assert.Equal("/example-string", output.Path)
    25  	assert.NotEmpty(output.Query())
    26  
    27  	var err error
    28  	func() {
    29  		defer func() {
    30  			if r := recover(); r != nil {
    31  				err = fmt.Errorf("%v", r)
    32  			}
    33  		}()
    34  		output = MustParseURL(":not-a-url-at-all")
    35  	}()
    36  	assert.NotNil(err)
    37  }
    38  
    39  func TestURLWithScheme(t *testing.T) {
    40  	assert := assert.New(t)
    41  
    42  	original := MustParseURL("https://foo.bar.com/example-string?buzz=muzz")
    43  	assert.Equal("http", URLWithScheme(original, "http").Scheme)
    44  }
    45  
    46  func TestURLWithHost(t *testing.T) {
    47  	assert := assert.New(t)
    48  
    49  	original := MustParseURL("https://foo.bar.com/example-string?buzz=muzz")
    50  	assert.Equal("blend.com", URLWithHost(original, "blend.com").Host)
    51  }
    52  
    53  func TestURLWithPort(t *testing.T) {
    54  	assert := assert.New(t)
    55  
    56  	original := MustParseURL("https://foo.bar.com/example-string?buzz=muzz")
    57  	assert.Equal("foo.bar.com:8443", URLWithPort(original, "8443").Host)
    58  	assert.Equal("8443", URLWithPort(original, "8443").Port())
    59  
    60  	originalWithPort := MustParseURL("https://foo.bar.com:5000/example-string?buzz=muzz")
    61  	assert.Equal("foo.bar.com:5001", URLWithPort(originalWithPort, "5001").Host)
    62  	assert.Equal("5001", URLWithPort(originalWithPort, "5001").Port())
    63  }
    64  
    65  func TestURLWithPath(t *testing.T) {
    66  	assert := assert.New(t)
    67  
    68  	original := MustParseURL("https://foo.bar.com/example-string?buzz=muzz")
    69  	assert.Equal("not-example-string", URLWithPath(original, "not-example-string").Path)
    70  }
    71  
    72  func TestURLWithRawQuery(t *testing.T) {
    73  	assert := assert.New(t)
    74  
    75  	original := MustParseURL("https://foo.bar.com/example-string?buzz=muzz")
    76  	assert.Equal("dog=cool", URLWithRawQuery(original, "dog=cool").RawQuery)
    77  }
    78  
    79  func TestURLWithQuery(t *testing.T) {
    80  	assert := assert.New(t)
    81  
    82  	original := MustParseURL("https://foo.bar.com/example-string?buzz=muzz")
    83  	assert.Equal("buzz=muzz", original.RawQuery)
    84  	assert.Equal("buzz=muzz&dog=cool", URLWithQuery(original, "dog", "cool").RawQuery)
    85  }