Created
October 15, 2024 19:26
-
-
Save tangzero/684bdc3f7feda5b78ed6a61ab0d6230b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package mocks | |
import ( | |
"encoding/json" | |
"io" | |
"net/http" | |
"net/http/httptest" | |
"strings" | |
"testing" | |
"github.com/stretchr/testify/assert" | |
"github.com/stretchr/testify/require" | |
"github.com/stripe/stripe-go/v79" | |
) | |
func TestCustomersURLs(t *testing.T) { | |
for name, tc := range map[string]struct { | |
request *http.Request | |
statusCode int | |
assert func(*testing.T, []byte) | |
}{ | |
"POST /v1/customers": { | |
statusCode: http.StatusOK, | |
assert: func(t *testing.T, body []byte) { | |
var customer stripe.Customer | |
assert.NoError(t, json.Unmarshal(body, &customer)) | |
assert.NotEmpty(t, customer.ID) | |
}, | |
}, | |
"GET /v1/customers/search?query=something": { | |
statusCode: http.StatusOK, | |
assert: func(t *testing.T, body []byte) { | |
var result stripe.CustomerSearchResult | |
assert.NoError(t, json.Unmarshal(body, &result)) | |
assert.Empty(t, result.Data) | |
}, | |
}, | |
"GET /v1/customers/1234": { | |
statusCode: http.StatusOK, | |
assert: func(t *testing.T, body []byte) { | |
var customer stripe.Customer | |
assert.NoError(t, json.Unmarshal(body, &customer)) | |
assert.Equal(t, "1234", customer.ID) | |
}, | |
}, | |
} { | |
t.Run(name, func(t *testing.T) { | |
request := strings.Split(name, " ") | |
rw := httptest.NewRecorder() | |
r := httptest.NewRequest(request[0], request[1], nil) | |
router := Router() | |
router.ServeHTTP(rw, r) | |
assert.Equal(t, tc.statusCode, rw.Code) | |
assert.Equal(t, "application/json", rw.Header().Get("Content-Type")) | |
body, err := io.ReadAll(rw.Body) | |
require.NoError(t, err) | |
tc.assert(t, body) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment