From e74332f0f7719813bb5da2213294e61748747b67 Mon Sep 17 00:00:00 2001 From: Antonio Thomacelli Gomes Date: Wed, 29 Nov 2023 23:59:13 -0300 Subject: [PATCH] feat: add new test --- webreq.go | 4 +++- webreq_get_test.go | 33 +++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/webreq.go b/webreq.go index f68ce77..ee97bc9 100644 --- a/webreq.go +++ b/webreq.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "io" + "log" "net/http" "time" ) @@ -47,7 +48,8 @@ func NewRequest(method string) Request { func (request *Request) SetURL(urlValue string) *Request { if urlValue == "" { - panic("URL cannot be empty") + log.Println("URL cannot be empty") + return nil } request.URL = urlValue return request diff --git a/webreq_get_test.go b/webreq_get_test.go index 6f08627..ac5f332 100644 --- a/webreq_get_test.go +++ b/webreq_get_test.go @@ -5,6 +5,16 @@ import ( "testing" ) +// fakeWriter é uma implementação simples de io.Writer para capturar a saída do log +type fakeWriter struct { + target *string +} + +func (fw *fakeWriter) Write(p []byte) (n int, err error) { + *fw.target = string(p) + return len(p), nil +} + func TestPackageCall(t *testing.T) { headers := webreq.NewHeaders() @@ -26,16 +36,15 @@ func TestPackageCall(t *testing.T) { } -func TestWrongCall(t *testing.T) { - - request := webreq.NewRequest("GET") - - body, err := request.Execute() - if err == nil { - t.Error("Expected an error but didn't get one") - } - bodyString := string(body) - if bodyString != "" { - t.Error("body is not empty") - } +func TestSetURL(t *testing.T) { + // Teste para verificar se a URL é definida corretamente quando não está vazia + t.Run("Non-empty URL", func(t *testing.T) { + request := webreq.NewRequest("GET") + request.SetURL("https://example.com") + + // Verifique se a URL é definida corretamente + if request.URL != "https://example.com" { + t.Errorf("URL not set correctly") + } + }) }