Skip to content

Commit

Permalink
feat: add new tests coverage it is 74%
Browse files Browse the repository at this point in the history
  • Loading branch information
tonnytg committed Dec 1, 2023
1 parent 3ff8844 commit a9e14c6
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
5 changes: 1 addition & 4 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion webreq.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
87 changes: 83 additions & 4 deletions webreq_get_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package webreq_test

import (
"fmt"
"github.com/tonnytg/webreq"
"testing"
"time"
)

func TestPackageCall(t *testing.T) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down

0 comments on commit a9e14c6

Please sign in to comment.