github.com/blend/go-sdk@v1.20220411.3/vault/mock_http_client_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 vault
     9  
    10  import (
    11  	"bytes"
    12  	"net/http"
    13  	"net/url"
    14  	"testing"
    15  
    16  	"github.com/blend/go-sdk/assert"
    17  )
    18  
    19  func TestMockHTTPClientDo(t *testing.T) {
    20  	assert := assert.New(t)
    21  
    22  	// mocked request and response
    23  	address := "www.blend.com"
    24  	url, err := url.Parse(address)
    25  	assert.Nil(err)
    26  
    27  	client := NewMockHTTPClient()
    28  
    29  	// Test: Do returns the OK response matching the route specified
    30  	happyReq, err := http.NewRequest(http.MethodGet, address, bytes.NewReader([]byte{}))
    31  	happyResp := &http.Response{StatusCode: http.StatusOK}
    32  	assert.Nil(err)
    33  	r, err := client.With(http.MethodGet, url, happyResp).Do(happyReq)
    34  	assert.Nil(err)
    35  	assert.Equal(happyResp, r)
    36  
    37  	// Test: Do returns an error response for unknown routes
    38  	badReq, err := http.NewRequest(http.MethodDelete, address, bytes.NewReader([]byte{}))
    39  	assert.Nil(err)
    40  	r, err = client.Do(badReq)
    41  	assert.NotNil(err)
    42  	assert.Nil(r)
    43  }