Skip to content

Commit

Permalink
feat: add new test
Browse files Browse the repository at this point in the history
  • Loading branch information
tonnytg committed Nov 30, 2023
1 parent 66d1125 commit e74332f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
4 changes: 3 additions & 1 deletion webreq.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"io"
"log"
"net/http"
"time"
)
Expand Down Expand Up @@ -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
Expand Down
33 changes: 21 additions & 12 deletions webreq_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")
}
})
}

0 comments on commit e74332f

Please sign in to comment.