github.com/angryronald/go-kit@v0.0.0-20240505173814-ff2bd9c79dbf/generic/http/client/clienttest/client.utils_test.go (about)

     1  package clienttest
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/angryronald/go-kit/generic/http/client"
     8  )
     9  
    10  type TestParams struct {
    11  	IntField    int     `json:"IntField"`
    12  	StringField string  `json:"StringField"`
    13  	SliceField  []int   `json:"SliceField"`
    14  	PtrField    *string `json:"PtrField"`
    15  }
    16  
    17  func TestParseQueryParams(t *testing.T) {
    18  	strPtr := StringPtr("pointer")
    19  	tests := []struct {
    20  		name     string
    21  		path     string
    22  		params   interface{}
    23  		expected string
    24  	}{
    25  		{
    26  			name:     "EmptyParams",
    27  			path:     "https://example.com",
    28  			params:   TestParams{},
    29  			expected: "https://example.com?IntField=0",
    30  		},
    31  		{
    32  			name:     "IntegerParam",
    33  			path:     "https://example.com",
    34  			params:   TestParams{IntField: 42},
    35  			expected: "https://example.com?IntField=42",
    36  		},
    37  		{
    38  			name:     "StringParam",
    39  			path:     "https://example.com",
    40  			params:   TestParams{StringField: "hello"},
    41  			expected: "https://example.com?IntField=0&StringField=hello",
    42  		},
    43  		{
    44  			name:     "SliceParam",
    45  			path:     "https://example.com",
    46  			params:   TestParams{SliceField: []int{1, 2, 3}},
    47  			expected: "https://example.com?IntField=0&SliceField=1&SliceField=2&SliceField=3",
    48  		},
    49  		{
    50  			name:     "NilPointerParam",
    51  			path:     "https://example.com",
    52  			params:   TestParams{PtrField: nil}, // Should not include PtrField in the query
    53  			expected: "https://example.com?IntField=0",
    54  		},
    55  		{
    56  			name:     "ValidPointerParam",
    57  			path:     "https://example.com",
    58  			params:   TestParams{PtrField: strPtr},
    59  			expected: fmt.Sprintf("https://example.com?IntField=0&PtrField=%v", strPtr),
    60  		},
    61  	}
    62  
    63  	for _, tt := range tests {
    64  		t.Run(tt.name, func(t *testing.T) {
    65  			result := client.ParseQueryParams(tt.path, tt.params)
    66  			if result != tt.expected {
    67  				t.Errorf("Expected: %s, but got: %s", tt.expected, result)
    68  			}
    69  		})
    70  	}
    71  }
    72  
    73  func StringPtr(s string) *string {
    74  	return &s
    75  }