github.com/MontFerret/ferret@v0.18.0/pkg/drivers/http/driver_test.go (about)

     1  package http
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"crypto/tls"
     7  	"io"
     8  	"net/http"
     9  	"reflect"
    10  	"testing"
    11  	"unsafe"
    12  
    13  	"github.com/jarcoal/httpmock"
    14  	. "github.com/smartystreets/goconvey/convey"
    15  	"golang.org/x/text/encoding/charmap"
    16  
    17  	"github.com/MontFerret/ferret/pkg/drivers"
    18  )
    19  
    20  func Test_newHTTPClientWithTransport(t *testing.T) {
    21  	httpTransport := (http.DefaultTransport).(*http.Transport)
    22  	httpTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
    23  
    24  	type args struct {
    25  		options *Options
    26  	}
    27  	tests := []struct {
    28  		name string
    29  		args args
    30  		want bool
    31  	}{
    32  		{
    33  			name: "check transport exist with pester.New()",
    34  			args: args{options: &Options{
    35  				Options: &drivers.Options{
    36  					Proxy: "http://0.0.0.|",
    37  				},
    38  				HTTPTransport: httpTransport,
    39  			}},
    40  		},
    41  	}
    42  	for _, tt := range tests {
    43  		t.Run(tt.name, func(t *testing.T) {
    44  			Convey(tt.name, t, func() {
    45  				var (
    46  					transport *http.Transport
    47  					client    = newHTTPClient(tt.args.options)
    48  					rValue    = reflect.ValueOf(client).Elem()
    49  					rField    = rValue.Field(0)
    50  				)
    51  
    52  				rField = reflect.NewAt(rField.Type(), unsafe.Pointer(rField.UnsafeAddr())).Elem()
    53  				hc := rField.Interface().(*http.Client)
    54  
    55  				if hc != nil {
    56  					transport = hc.Transport.(*http.Transport)
    57  				} else {
    58  					transport = client.Transport.(*http.Transport)
    59  				}
    60  
    61  				verify := transport.TLSClientConfig.InsecureSkipVerify
    62  
    63  				So(verify, ShouldBeTrue)
    64  			})
    65  		})
    66  	}
    67  }
    68  
    69  func Test_newHTTPClient(t *testing.T) {
    70  
    71  	Convey("pester.New()", t, func() {
    72  		var (
    73  			client = newHTTPClient(&Options{
    74  				Options: &drivers.Options{
    75  					Proxy: "http://0.0.0.|",
    76  				},
    77  			})
    78  
    79  			rValue = reflect.ValueOf(client).Elem()
    80  			rField = rValue.Field(0)
    81  		)
    82  
    83  		rField = reflect.NewAt(rField.Type(), unsafe.Pointer(rField.UnsafeAddr())).Elem()
    84  		hc := rField.Interface().(*http.Client)
    85  
    86  		So(hc, ShouldBeNil)
    87  	})
    88  }
    89  
    90  func TestDriver_convertToUTF8(t *testing.T) {
    91  	type args struct {
    92  		inputData  string
    93  		srcCharset string
    94  	}
    95  	tests := []struct {
    96  		name     string
    97  		args     args
    98  		wantData io.Reader
    99  		expected string
   100  		wantErr  bool
   101  	}{
   102  		{
   103  			name: "should convert to expected state",
   104  			args: args{
   105  				inputData:  `<!DOCTYPE html><html><head><meta charset="windows-1251"/></head><body>феррет</body></html>`,
   106  				srcCharset: "windows-1251",
   107  			},
   108  			wantErr:  false,
   109  			expected: `<!DOCTYPE html><html><head><meta charset="windows-1251"/></head><body>феррет</body></html>`,
   110  		},
   111  	}
   112  	for _, tt := range tests {
   113  		t.Run(tt.name, func(t *testing.T) {
   114  			drv := &Driver{}
   115  
   116  			Convey(tt.name, t, func() {
   117  
   118  				data, err := io.ReadAll(bytes.NewBufferString(tt.args.inputData))
   119  				if err != nil {
   120  					panic(err)
   121  				}
   122  
   123  				encodedData := make([]byte, len(data)*2)
   124  
   125  				dec := charmap.Windows1251.NewEncoder()
   126  				nDst, _, err := dec.Transform(encodedData, data, false)
   127  				if err != nil {
   128  					panic(err)
   129  				}
   130  
   131  				encodedData = encodedData[:nDst]
   132  
   133  				gotData, err := drv.convertToUTF8(bytes.NewReader(encodedData), tt.args.srcCharset)
   134  				So(err, ShouldBeNil)
   135  
   136  				outData, err := io.ReadAll(gotData)
   137  				So(err, ShouldBeNil)
   138  
   139  				So(string(outData), ShouldEqual, tt.expected)
   140  
   141  			})
   142  
   143  		})
   144  	}
   145  }
   146  
   147  func TestDriver_Concurrency(t *testing.T) {
   148  	Convey("Should make only 1 request", t, func() {
   149  		httpmock.Activate()
   150  		defer httpmock.DeactivateAndReset()
   151  
   152  		httpmock.RegisterResponder("GET", "http://localhost:1111",
   153  			httpmock.NewStringResponder(200, `<!DOCTYPE html><html><head></head><body></body></html>`))
   154  
   155  		drv := NewDriver()
   156  
   157  		page, err := drv.Open(context.Background(), drivers.Params{
   158  			URL: "http://localhost:1111",
   159  		})
   160  
   161  		So(err, ShouldBeNil)
   162  		So(page, ShouldNotBeNil)
   163  		So(httpmock.GetTotalCallCount(), ShouldEqual, 1)
   164  	})
   165  }