-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Establish testing for httpassert testing.T helpers. - Allow response header setting in mocked requests.
- Loading branch information
1 parent
aaf7ec9
commit fb55abf
Showing
11 changed files
with
222 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package httpassert | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
func TestPrintJSON(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("fails if the input cannot be marshalled to JSON", func(t *testing.T) { | ||
grp := errgroup.Group{} | ||
stubTest := &testing.T{} | ||
grp.Go(func() error { | ||
PrintJSON(stubTest, func() {}) | ||
return nil | ||
}) | ||
require.NoError(t, grp.Wait()) | ||
require.True(t, stubTest.Failed()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package httpassert | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestResponseEqual(t *testing.T) { | ||
type args struct { | ||
t *testing.T | ||
actual *http.Response | ||
expected *http.Response | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want assert.BoolAssertionFunc | ||
}{ | ||
{ | ||
name: "status code does not match", | ||
args: args{ | ||
t: &testing.T{}, | ||
actual: &http.Response{ | ||
StatusCode: http.StatusBadRequest, | ||
}, | ||
expected: &http.Response{ | ||
StatusCode: http.StatusOK, | ||
}, | ||
}, | ||
want: assert.True, | ||
}, | ||
{ | ||
name: "status code matches", | ||
args: args{ | ||
t: &testing.T{}, | ||
actual: &http.Response{ | ||
StatusCode: http.StatusBadRequest, | ||
}, | ||
expected: &http.Response{ | ||
StatusCode: http.StatusBadRequest, | ||
}, | ||
}, | ||
want: assert.False, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ResponseEqual(tt.args.t, tt.args.actual, tt.args.expected) | ||
tt.want(t, tt.args.t.Failed()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package httpclient | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDeserializeJSON(t *testing.T) { | ||
type args struct { | ||
resp *http.Response | ||
target any | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErrMessage string | ||
want map[string]any | ||
}{ | ||
{ | ||
name: "returns error from JSON marshalling", | ||
args: args{ | ||
resp: &http.Response{ | ||
Body: io.NopCloser(strings.NewReader("{")), | ||
}, | ||
target: &map[string]any{}, | ||
}, | ||
wantErrMessage: "unexpected end of JSON input", | ||
}, | ||
{ | ||
name: "returns error when not passing a pointer", | ||
args: args{ | ||
resp: &http.Response{ | ||
Body: io.NopCloser(strings.NewReader("{}")), | ||
}, | ||
target: map[string]any{}, | ||
}, | ||
wantErrMessage: "pointer required, got map[string]interface {}", | ||
}, | ||
{ | ||
name: "unmarshals the JSON payload to the passed pointer", | ||
args: args{ | ||
resp: &http.Response{ | ||
Body: io.NopCloser(strings.NewReader(`{"hello": "world"}`)), | ||
}, | ||
target: &map[string]any{}, | ||
}, | ||
want: map[string]any{"hello": "world"}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := DeserializeJSON(tt.args.resp, tt.args.target) | ||
if tt.wantErrMessage != "" { | ||
assert.ErrorContains(t, err, tt.wantErrMessage) | ||
} else { | ||
assert.Equal(t, tt.want, *tt.args.target.(*map[string]any)) | ||
} | ||
}) | ||
} | ||
} |