github.com/saucelabs/saucectl@v0.175.1/internal/http/request_test.go (about)

     1  package http
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net/http"
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func TestNewRequestWithContext(t *testing.T) {
    12  	type args struct {
    13  		ctx    context.Context
    14  		method string
    15  		url    string
    16  		body   io.Reader
    17  	}
    18  	tests := []struct {
    19  		name        string
    20  		args        args
    21  		wantHeaders http.Header
    22  		wantErr     bool
    23  	}{
    24  		{
    25  			name: "expect headers",
    26  			args: args{
    27  				ctx:    context.Background(),
    28  				method: "GET",
    29  				url:    "http://localhost",
    30  				body:   nil,
    31  			},
    32  			wantHeaders: http.Header{"User-Agent": []string{"saucectl/0.0.0+unknown"}},
    33  		},
    34  	}
    35  	for _, tt := range tests {
    36  		t.Run(tt.name, func(t *testing.T) {
    37  			got, err := NewRequestWithContext(tt.args.ctx, tt.args.method, tt.args.url, tt.args.body)
    38  			if (err != nil) != tt.wantErr {
    39  				t.Errorf("NewRequestWithContext() error = %v, wantErr %v", err, tt.wantErr)
    40  				return
    41  			}
    42  			if !reflect.DeepEqual(got.Header, tt.wantHeaders) {
    43  				t.Errorf("Headers got = %v, want %v", got, tt.wantHeaders)
    44  			}
    45  		})
    46  	}
    47  }