Skip to content

Commit

Permalink
Add tests for IsResponseValid
Browse files Browse the repository at this point in the history
Signed-off-by: Alina Buzachis <[email protected]>
  • Loading branch information
alinabuzachis committed Jan 19, 2024
1 parent 70b8552 commit ec55757
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions internal/provider/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package provider

import (
"encoding/json"
"fmt"
"io"
"net/http"
"reflect"
"slices"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

// DeepEqualJSONByte compares the JSON in two byte slices.
Expand Down Expand Up @@ -73,3 +77,51 @@ func (c *MockHTTPClient) doRequest(method string, path string, data io.Reader) (
}
return &http.Response{StatusCode: c.httpCode}, result, nil
}

const (
expectedStatusCode = 200
unexpectedStatusCode = 404
)

func TestIsResponseValid(t *testing.T) {
var testTable = []struct {
resp *http.Response
err error
expected int
}{
{
resp: &http.Response{
StatusCode: expectedStatusCode,
Status: "OK",
},
err: nil,
},
{
resp: nil,
err: fmt.Errorf("sample error message"),
},
{
resp: nil,
err: nil,
},
{
resp: &http.Response{
StatusCode: unexpectedStatusCode,
Status: "Not Found",
},
err: nil,
},
}

for _, test := range testTable {
t.Run(fmt.Sprintf("Status: %d", expectedStatusCode), func(t *testing.T) {
diags := IsResponseValid(test.resp, test.err, expectedStatusCode)

if test.resp != nil && test.err == nil && test.resp.StatusCode == expectedStatusCode {
assert.Empty(t, diags, "No errors expected for a successful response")
} else {
assert.NotEmpty(t, diags, "Error expected")
}
})
}
}

0 comments on commit ec55757

Please sign in to comment.