aboutsummaryrefslogtreecommitdiffstats
path: root/jttp_test.go
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2026-06-13 11:38:20 -0400
committerSam Anthony <sam@samanthony.xyz>2026-06-13 11:38:20 -0400
commit21958dacc6b34b447228114c82ddb88567e44bcf (patch)
treefb24df0553eade4ea5c6e496f46b0ca0cd7e1b5e /jttp_test.go
parent056cff27d3c003e32e098dc59bb53a8a93efa88a (diff)
downloadjttp-master.zip
refactor testsHEADmaster
Diffstat (limited to 'jttp_test.go')
-rw-r--r--jttp_test.go69
1 files changed, 29 insertions, 40 deletions
diff --git a/jttp_test.go b/jttp_test.go
index 66256c0..26b0b8d 100644
--- a/jttp_test.go
+++ b/jttp_test.go
@@ -17,67 +17,56 @@ type Person struct {
Age int
}
-func TestDo(t *testing.T) {
- srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- fmt.Fprint(w, `{"name": "Alice", "age": 123}`)
- }))
+func do(t *testing.T, handler http.HandlerFunc, method string, wantStatus int) (Person, error) {
+ t.Helper()
+
+ srv := httptest.NewServer(handler)
defer srv.Close()
- clnt := jttp.NewClient(srv.Client())
url, err := url.Parse(srv.URL)
require.NoError(t, err)
- req := &http.Request{Method: http.MethodGet, URL: url}
- var alice Person
- err = clnt.Do(req, http.StatusOK, &alice)
+
+ clnt := jttp.NewClient(srv.Client())
+ req := &http.Request{Method: method, URL: url}
+ var person Person
+ err = clnt.Do(req, wantStatus, &person)
+ return person, err
+}
+
+func TestDo(t *testing.T) {
+ handler := func(w http.ResponseWriter, r *http.Request) {
+ fmt.Fprint(w, `{"name": "Alice", "age": 123}`)
+ }
+ alice, err := do(t, handler, http.MethodGet, http.StatusOK)
require.NoError(t, err)
require.Equal(t, Person{"Alice", 123}, alice)
}
func TestDoReqFail(t *testing.T) {
- srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- http.Error(w, "", http.StatusInternalServerError)
- }))
- defer srv.Close()
-
- clnt := jttp.NewClient(srv.Client())
- url, err := url.Parse(srv.URL)
- require.NoError(t, err)
- req := &http.Request{Method: http.MethodGet, URL: url}
+ clnt := jttp.NewClient(&http.Client{})
+ req := &http.Request{}
var alice Person
- err = clnt.Do(req, http.StatusOK, &alice)
+ err := clnt.Do(req, http.StatusOK, &alice)
require.Error(t, err)
- require.Equal(t, fmt.Sprintf("jttp: GET %s: 500 Internal Server Error (expected 200)", srv.URL), err.Error())
+ require.Contains(t, err.Error(), "http: nil Request.URL")
}
func TestDoBadStatus(t *testing.T) {
- srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ handler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
fmt.Fprint(w, `{"name": "Alice", "age": 123}`)
- }))
- defer srv.Close()
-
- clnt := jttp.NewClient(srv.Client())
- url, err := url.Parse(srv.URL)
- require.NoError(t, err)
- req := &http.Request{Method: http.MethodGet, URL: url}
- var alice Person
- err = clnt.Do(req, 200, &alice)
+ }
+ _, err := do(t, handler, http.MethodGet, 200)
require.Error(t, err)
- require.Equal(t, fmt.Sprintf("jttp: GET %s: 201 Created (expected 200)", srv.URL), err.Error())
+ require.Contains(t, err.Error(), "jttp")
+ require.Contains(t, err.Error(), "201 Created (expected 200)")
}
func TestDoDecodeFail(t *testing.T) {
- srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ handler := func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"name": "Alice", "age": "notanumber"}`)
- }))
- defer srv.Close()
-
- clnt := jttp.NewClient(srv.Client())
- url, err := url.Parse(srv.URL)
- require.NoError(t, err)
- req := &http.Request{Method: http.MethodGet, URL: url}
- var alice Person
- err = clnt.Do(req, http.StatusOK, &alice)
+ }
+ _, err := do(t, handler, http.MethodGet, http.StatusOK)
require.Error(t, err)
require.Contains(t, err.Error(), "json")
require.Contains(t, err.Error(), "Person.Age")