From a9e14c633e37fc9b6ea9c817eab04bd1ad94dc8e Mon Sep 17 00:00:00 2001 From: Antonio Thomacelli Gomes Date: Fri, 1 Dec 2023 07:56:55 -0300 Subject: [PATCH] feat: add new tests coverage it is 74% --- example/main.go | 5 +-- webreq.go | 2 +- webreq_get_test.go | 87 +++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 85 insertions(+), 9 deletions(-) diff --git a/example/main.go b/example/main.go index 38f384d..4a80e4b 100644 --- a/example/main.go +++ b/example/main.go @@ -14,11 +14,8 @@ type Friend struct { func main() { - headers := webreq.NewHeaders() + headers := webreq.NewHeaders(nil) headers.Add("Content-Type", "application/json") - if len(headers.Headers) != 1 { - fmt.Println("headers is empty") - } f := Friend{ CreatedAt: time.Now(), diff --git a/webreq.go b/webreq.go index 146a6ab..c746c7c 100644 --- a/webreq.go +++ b/webreq.go @@ -100,7 +100,7 @@ func (request *Request) SetData(bodyValue []byte) *Request { func (request *Request) SetMethod(requestMethod string) *Request { if requestMethod == "" { - request.ErrorMessage = "request type is empty" + request.ErrorMessage = "request method is empty" return nil } request.Method = requestMethod diff --git a/webreq_get_test.go b/webreq_get_test.go index 3999db5..44cc343 100644 --- a/webreq_get_test.go +++ b/webreq_get_test.go @@ -1,8 +1,10 @@ package webreq_test import ( + "fmt" "github.com/tonnytg/webreq" "testing" + "time" ) func TestPackageCall(t *testing.T) { @@ -40,12 +42,58 @@ func TestSetURL(t *testing.T) { }) } -func TestSetNewRequest(t *testing.T) { +func TestSetTimeout(t *testing.T) { + request := webreq.NewRequest("GET") + request.SetTimeout(10) + if request.TimeoutDuration != (10 * time.Second) { + fmt.Println(request.TimeoutDuration) + t.Error("request.TimeoutDuration is not 10 * time.Second") + } + return +} - newRequest := webreq.NewRequest("GET") - if newRequest == nil { - t.Error("newRequest is nil") +func TestSetHeaders(t *testing.T) { + + var headersList = make(map[string]string) + headersList["Content-Type"] = "application/json" + + headers := webreq.NewHeaders(headersList) + if headers.Headers["Content-Type"] != "application/json" { + t.Error("headers.Headers[Content-Type] is not application/json") + } + + request := webreq.NewRequest("GET") + request.SetHeaders(headers.Headers) + if request.ErrorMessage != "" { + t.Error("request.ErrorMessage is not empty") } + return +} + +func TestSetHeaders2(t *testing.T) { + + headers := webreq.NewHeaders(nil) + headers.Add("Content-Type", "application/json") + if headers.Headers["Content-Type"] != "application/json" { + t.Error("headers.Headers[Content-Type] is not application/json") + } + + request := webreq.NewRequest("GET") + request.SetHeaders(headers.Headers) + if request.ErrorMessage != "" { + t.Error("request.ErrorMessage is not empty") + } + return +} + +func TestSetData(t *testing.T) { + + request := webreq.NewRequest("GET") + request.SetData([]byte("test")) + if request.ErrorMessage != "" { + t.Error("request.ErrorMessage is not empty") + } + return } func TestSetMethod(t *testing.T) { @@ -58,9 +106,40 @@ func TestSetMethod(t *testing.T) { if request.Method != "GET" { t.Error("request.Method is not GET") } + + request.SetMethod("POST") + if request.Method != "POST" { + t.Error("request.Method is not POST") + } + + request.SetMethod("") + if request.ErrorMessage != "request method is empty" { + t.Error("request.Method is not empty") + } return } +func TestSetStatusCode(t *testing.T) { + + request := webreq.NewRequest("GET") + if request == nil { + t.Error("request is nil") + } + request.SetStatusCode(200) + if request.StatusCode != 200 { + t.Error("request.StatusCode is not 200") + } + return +} + +func TestSetNewRequest(t *testing.T) { + + newRequest := webreq.NewRequest("GET") + if newRequest == nil { + t.Error("newRequest is nil") + } +} + func TestEndToEnd(t *testing.T) { headers := webreq.NewHeaders(nil)