github.com/stakater/IngressMonitorController@v1.0.103/pkg/http/httpClient_test.go (about)

     1  package http
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  )
     7  
     8  func TestCreateHttpClient(t *testing.T) {
     9  	url := "https://google.com"
    10  	client := CreateHttpClient(url)
    11  
    12  	if client.url != url {
    13  		t.Error("Client URL should match the assigned url")
    14  	}
    15  }
    16  func TestAddHeaders(t *testing.T) {
    17  	url := "https://google.com"
    18  	client := CreateHttpClient(url)
    19  
    20  	request := &http.Request{}
    21  	request.Header = http.Header{}
    22  
    23  	headers := make(map[string]string)
    24  	headers["Accepts"] = "application/json"
    25  
    26  	client.addHeaders(request, headers)
    27  
    28  	if value, ok := request.Header["Accepts"]; ok {
    29  		if value[0] != "application/json" {
    30  			t.Error("Accepts Header should have the value application/json")
    31  		}
    32  	} else {
    33  		t.Error("Request should have the header Accepts")
    34  	}
    35  }
    36  
    37  func TestGetUrlShouldReturn200Status(t *testing.T) {
    38  	url := "https://google.com"
    39  	client := CreateHttpClient(url)
    40  
    41  	response := client.GetUrl(make(map[string]string), []byte(""))
    42  
    43  	if response.StatusCode != http.StatusOK {
    44  		t.Error("Status code mismatch")
    45  	}
    46  }
    47  
    48  func TestPostUrlShouldReturn405Status(t *testing.T) {
    49  	url := "https://google.com"
    50  	client := CreateHttpClient(url)
    51  
    52  	response := client.PostUrl(make(map[string]string), []byte(""))
    53  
    54  	if response.StatusCode != http.StatusMethodNotAllowed {
    55  		t.Error("Status code mismatch")
    56  	}
    57  }
    58  
    59  func TestDeleteUrlShouldReturn405Status(t *testing.T) {
    60  	url := "https://google.com"
    61  	client := CreateHttpClient(url)
    62  
    63  	response := client.DeleteUrl(make(map[string]string), []byte(""))
    64  
    65  	if response.StatusCode != http.StatusMethodNotAllowed {
    66  		t.Error("Status code mismatch")
    67  	}
    68  }