Skip to content

Commit

Permalink
Refactored checkResponseForTestPlaybackOrder
Browse files Browse the repository at this point in the history
- less brittle
- no boolean in signature
  • Loading branch information
seborama committed Feb 17, 2017
1 parent a270db7 commit 5a8b412
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions govcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func TestPlaybackOrder(t *testing.T) {
resp, _ := client.Get(ts.URL)

// check outcome of the request
checkResponseForTestPlaybackOrder(t, resp, i, false)
expectedBody := fmt.Sprintf("Hello, client %d", i)
checkResponseForTestPlaybackOrder(t, resp, expectedBody)

if !govcr.CassetteExistsAndValid(cassetteName, "") {
t.Fatalf("CassetteExists: expected true, got false")
Expand All @@ -63,7 +64,8 @@ func TestPlaybackOrder(t *testing.T) {
resp, _ := client.Get(ts.URL)

// check outcome of the request
checkResponseForTestPlaybackOrder(t, resp, i, false)
expectedBody := fmt.Sprintf("Hello, client %d", i)
checkResponseForTestPlaybackOrder(t, resp, expectedBody)

if !govcr.CassetteExistsAndValid(cassetteName, "") {
t.Fatalf("CassetteExists: expected true, got false")
Expand Down Expand Up @@ -104,7 +106,8 @@ func TestNonUtf8EncodableBinaryBody(t *testing.T) {
resp, _ := client.Get(ts.URL)

// check outcome of the request
checkResponseForTestPlaybackOrder(t, resp, i, true)
expectedBody := generateBinaryBody(i)
checkResponseForTestPlaybackOrder(t, resp, expectedBody)

if !govcr.CassetteExistsAndValid(cassetteName, "") {
t.Fatalf("CassetteExists: expected true, got false")
Expand All @@ -125,7 +128,8 @@ func TestNonUtf8EncodableBinaryBody(t *testing.T) {
resp, _ := client.Get(ts.URL)

// check outcome of the request
checkResponseForTestPlaybackOrder(t, resp, i, true)
expectedBody := generateBinaryBody(i)
checkResponseForTestPlaybackOrder(t, resp, expectedBody)

if !govcr.CassetteExistsAndValid(cassetteName, "") {
t.Fatalf("CassetteExists: expected true, got false")
Expand All @@ -149,7 +153,7 @@ func createVCR(cassetteName string, wipeCassette bool) *govcr.VCRControlPanel {
})
}

func checkResponseForTestPlaybackOrder(t *testing.T, resp *http.Response, i int, binary bool) {
func checkResponseForTestPlaybackOrder(t *testing.T, resp *http.Response, expectedBody interface{}) {
if resp.StatusCode != http.StatusOK {
t.Fatalf("resp.StatusCode: Expected %d, got %d", http.StatusOK, resp.StatusCode)
}
Expand All @@ -164,16 +168,28 @@ func checkResponseForTestPlaybackOrder(t *testing.T, resp *http.Response, i int,
}
resp.Body.Close()

if binary {
expectedBody := generateBinaryBody(i)
if bytes.Compare(bodyData, expectedBody) != 0 {
t.Fatalf("Body: expected '%v', got '%v'", expectedBody, bodyData)
var expectedBodyBytes []byte
switch expectedBody.(type) {
case []byte:
ok := false
expectedBodyBytes, ok = expectedBody.([]byte)
if !ok {
t.Fatalf("expectedBody: cannot assert to type '[]byte'")
}
} else {
expectedBody := fmt.Sprintf("Hello, client %d", i)
if string(bodyData) != expectedBody {
t.Fatalf("Body: expected '%s', got '%s'", expectedBody, string(bodyData))

case string:
expectedBodyString, ok := expectedBody.(string)
if !ok {
t.Fatalf("expectedBody: cannot assert to type 'string'")
}
expectedBodyBytes = []byte(expectedBodyString)

default:
t.Fatalf("Unexpected type for 'expectedBody' variable")
}

if bytes.Compare(bodyData, expectedBodyBytes) != 0 {
t.Fatalf("Body: expected '%v', got '%v'", expectedBody, bodyData)
}
}

Expand Down

0 comments on commit 5a8b412

Please sign in to comment.