github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/requests/client_test.go (about)

     1  /*
     2   * Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package requests
    19  
    20  import (
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"sync"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestClientDoRequest(t *testing.T) {
    31  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    32  		assert.Equal(t, getUserAgent(), r.Header.Get("User-Agent"))
    33  		w.WriteHeader(http.StatusOK)
    34  	}))
    35  	defer server.Close()
    36  
    37  	httpClient := NewHTTPClient("0.0.0.0", DefaultTimeout)
    38  
    39  	req, err := NewGetRequest(server.URL, "/", nil)
    40  	assert.NoError(t, err)
    41  
    42  	res, err := httpClient.Do(req)
    43  	assert.NoError(t, err)
    44  
    45  	assert.Equal(t, http.StatusOK, res.StatusCode)
    46  }
    47  
    48  func TestClientDoRequestAndParseResponse(t *testing.T) {
    49  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    50  		w.WriteHeader(http.StatusOK)
    51  		_, _ = w.Write([]byte(`{"Test": "OK"}`))
    52  	}))
    53  	defer server.Close()
    54  
    55  	httpClient := NewHTTPClient("0.0.0.0", DefaultTimeout)
    56  
    57  	req, err := NewGetRequest(server.URL, "/", nil)
    58  	assert.NoError(t, err)
    59  
    60  	var res struct {
    61  		Test string
    62  	}
    63  	err = httpClient.DoRequestAndParseResponse(req, &res)
    64  	assert.NoError(t, err)
    65  
    66  	assert.Equal(t, "OK", res.Test)
    67  }
    68  
    69  func TestHTTPClientRecreateUnderlyingHTTPClientInstance(t *testing.T) {
    70  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    71  		time.Sleep(100 * time.Millisecond)
    72  		w.Write([]byte("Timeout"))
    73  	}))
    74  	defer server.Close()
    75  
    76  	httpTransport := NewTransport(NewDialer("0.0.0.0").DialContext)
    77  	httpClient := NewHTTPClientWithTransport(httpTransport, 50*time.Millisecond)
    78  
    79  	var wg sync.WaitGroup
    80  	wg.Add(3)
    81  	for i := 0; i < 3; i++ {
    82  		go func() {
    83  			defer wg.Done()
    84  			req, err := NewGetRequest(server.URL, "/", nil)
    85  			assert.NoError(t, err)
    86  			res, err := httpClient.Do(req)
    87  			assert.Error(t, err)
    88  			assert.Nil(t, res)
    89  		}()
    90  
    91  		go func() {
    92  			httpTransport.CloseIdleConnections()
    93  		}()
    94  	}
    95  
    96  	wg.Wait()
    97  }