github.com/kaydxh/golang@v0.0.131/go/net/http/http_client_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package http_test
    23  
    24  import (
    25  	"testing"
    26  	"time"
    27  
    28  	http_ "github.com/kaydxh/golang/go/net/http"
    29  	"golang.org/x/net/context"
    30  	"gotest.tools/v3/assert"
    31  )
    32  
    33  func TestHttpClientGet(t *testing.T) {
    34  	ctx := context.Background()
    35  	client, err := http_.NewClient(http_.WithTimeout(5 * time.Second))
    36  	if err != nil {
    37  		t.Fatalf("expect nil, got %v", err)
    38  	}
    39  
    40  	testCases := []struct {
    41  		url      string
    42  		expected bool
    43  	}{
    44  		{
    45  			url:      "http://127.0.0.1",
    46  			expected: true,
    47  		},
    48  		{
    49  			url:      "http://127.0.0.2",
    50  			expected: false,
    51  		},
    52  	}
    53  
    54  	for _, test := range testCases {
    55  		data, err := client.Get(ctx, test.url)
    56  		if test.expected {
    57  			assert.NilError(t, err)
    58  		} else {
    59  			assert.Assert(t, err != nil) // NotNil
    60  			t.Logf("got %v", err)
    61  		}
    62  
    63  		t.Logf("response data: %v", string(data))
    64  	}
    65  }
    66  
    67  func TestHttpClientGetWithProxy(t *testing.T) {
    68  	ctx := context.Background()
    69  	client, err := http_.NewClient(http_.WithTimeout(5*time.Second), http_.WithTargetHost("dns:///ai-media-1256936300.cos.ap-guangzhou.myqcloud.com"))
    70  	if err != nil {
    71  		t.Fatalf("expect nil, got %v", err)
    72  	}
    73  
    74  	testCases := []struct {
    75  		url      string
    76  		expected bool
    77  	}{
    78  		{
    79  			//url:      "https://ai-media-1256936300.cos.ap-guangzhou.myqcloud.com/find.sh?q-sign-algorithm=sha1&q-ak=AKIDCDyve81SJuISPkMq0IukLg7tupWyoqCg&q-sign-time=1659955959;8640000001659870000&q-key-time=1659955959;8640000001659870000&q-header-list=&q-url-param-list=&q-signature=5695f17ee30c3cd2d37197c773a445eda8a70c8c",
    80  			url:      "http://127.0.0.1/find.sh?q-sign-algorithm=sha1&q-ak=AKIDCDyve81SJuISPkMq0IukLg7tupWyoqCg&q-sign-time=1659955959;8640000001659870000&q-key-time=1659955959;8640000001659870000&q-header-list=&q-url-param-list=&q-signature=5695f17ee30c3cd2d37197c773a445eda8a70c8c",
    81  			expected: true,
    82  		},
    83  	}
    84  
    85  	for _, test := range testCases {
    86  		data, err := client.Get(ctx, test.url)
    87  		if test.expected {
    88  			assert.NilError(t, err)
    89  		} else {
    90  			assert.Assert(t, err != nil) // NotNil
    91  			t.Logf("got %v", err)
    92  		}
    93  
    94  		t.Logf("response data size: %v", len(data))
    95  	}
    96  }
    97  
    98  func TestHttpClientPost(t *testing.T) {
    99  	ctx := context.Background()
   100  	client, err := http_.NewClient(http_.WithTimeout(5 * time.Second))
   101  	if err != nil {
   102  		t.Fatalf("expect nil, got %v", err)
   103  	}
   104  
   105  	testCases := []struct {
   106  		url      string
   107  		data     []byte
   108  		expected bool
   109  	}{
   110  		{
   111  			url:      "http://127.0.0.1",
   112  			data:     []byte("hello world test1"),
   113  			expected: true,
   114  		},
   115  		{
   116  			url:      "http://127.0.0.2",
   117  			data:     []byte("hello world test2"),
   118  			expected: false,
   119  		},
   120  	}
   121  
   122  	for _, test := range testCases {
   123  		data, err := client.Post(ctx, test.url, "application/text", nil, test.data)
   124  		if test.expected {
   125  			assert.NilError(t, err)
   126  		} else {
   127  			assert.Assert(t, err != nil) // NotNil
   128  			t.Logf("got %v", err)
   129  		}
   130  
   131  		t.Logf("response data: %v", string(data))
   132  	}
   133  }