dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/clients/http/utils/common_test.go (about)

     1  //
     2  // Copyright (C) 2023 IOTech Ltd
     3  //
     4  // SPDX-License-Identifier: Apache-2.0
     5  
     6  package utils
     7  
     8  import (
     9  	"context"
    10  	"net/http"
    11  	"os"
    12  	"testing"
    13  
    14  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models"
    15  
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func TestCreateRequest(t *testing.T) {
    20  	baseUrl := "http://localhost:59990"
    21  	requestPath := "test-path"
    22  	requestPathWithSlash := "/test-path"
    23  	tests := []struct {
    24  		name        string
    25  		requestPath string
    26  	}{
    27  		{"create request", requestPath},
    28  		{"create request and the request path starting with slash", requestPathWithSlash},
    29  	}
    30  	for _, testCase := range tests {
    31  		t.Run(testCase.name, func(t *testing.T) {
    32  			_, err := createRequest(context.Background(), http.MethodGet, baseUrl, requestPath, nil)
    33  			assert.NoError(t, err)
    34  		})
    35  	}
    36  }
    37  
    38  func TestCreateRequestWithRawData(t *testing.T) {
    39  	baseUrl := "http://localhost:59990"
    40  	requestPath := "test-path"
    41  	requestPathWithSlash := "/test-path"
    42  	tests := []struct {
    43  		name        string
    44  		requestPath string
    45  	}{
    46  		{"create request", requestPath},
    47  		{"create request and the request path starting with slash", requestPathWithSlash},
    48  	}
    49  	for _, testCase := range tests {
    50  		t.Run(testCase.name, func(t *testing.T) {
    51  			_, err := createRequestWithRawData(context.Background(), http.MethodGet, baseUrl, requestPath, nil, models.Event{})
    52  			assert.NoError(t, err)
    53  		})
    54  	}
    55  }
    56  
    57  func TestCreateRequestWithRawDataAndParams(t *testing.T) {
    58  	baseUrl := "http://localhost:59990"
    59  	requestPath := "test-path"
    60  	requestPathWithSlash := "/test-path"
    61  	tests := []struct {
    62  		name        string
    63  		requestPath string
    64  	}{
    65  		{"create request", requestPath},
    66  		{"create request and the request path starting with slash", requestPathWithSlash},
    67  	}
    68  	for _, testCase := range tests {
    69  		t.Run(testCase.name, func(t *testing.T) {
    70  			_, err := createRequestWithRawDataAndParams(context.Background(), http.MethodGet, baseUrl, requestPath, nil, models.Event{})
    71  			assert.NoError(t, err)
    72  		})
    73  	}
    74  }
    75  
    76  func TestCreateRequestWithEncodedData(t *testing.T) {
    77  	baseUrl := "http://localhost:59990"
    78  	requestPath := "test-path"
    79  	requestPathWithSlash := "/test-path"
    80  	tests := []struct {
    81  		name        string
    82  		requestPath string
    83  	}{
    84  		{"create request", requestPath},
    85  		{"create request and the request path starting with slash", requestPathWithSlash},
    86  	}
    87  	for _, testCase := range tests {
    88  		t.Run(testCase.name, func(t *testing.T) {
    89  			_, err := createRequestWithEncodedData(context.Background(), http.MethodGet, baseUrl, requestPath, nil, "")
    90  			assert.NoError(t, err)
    91  		})
    92  	}
    93  }
    94  
    95  func TestCreateRequestFromFilePath(t *testing.T) {
    96  	baseUrl := "http://localhost:59990"
    97  	requestPath := "test-path"
    98  	requestPathWithSlash := "/test-path"
    99  	f, err := os.CreateTemp("", "sample")
   100  	assert.NoError(t, err)
   101  	defer os.Remove(f.Name())
   102  
   103  	tests := []struct {
   104  		name        string
   105  		requestPath string
   106  	}{
   107  		{"create request", requestPath},
   108  		{"create request and the request path starting with slash", requestPathWithSlash},
   109  	}
   110  	for _, testCase := range tests {
   111  		t.Run(testCase.name, func(t *testing.T) {
   112  			_, err := createRequestFromFilePath(context.Background(), http.MethodGet, baseUrl, requestPath, f.Name())
   113  			assert.NoError(t, err)
   114  		})
   115  	}
   116  }