Simplify expectErrorString
This commit is contained in:
parent
ea4b907293
commit
a6d41df4e4
4 changed files with 34 additions and 24 deletions
|
@ -103,8 +103,10 @@ func TestServerAuthHandlerErrors(t *testing.T) {
|
|||
|
||||
server.getAuthToken(w, req)
|
||||
|
||||
body, _ := ioutil.ReadAll(w.Body)
|
||||
|
||||
expectStatusCode(t, w, tc.expectedStatusCode)
|
||||
expectErrorString(t, w, tc.expectedErrorString)
|
||||
expectErrorString(t, body, tc.expectedErrorString)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -87,8 +87,10 @@ func TestServerRegisterErrors(t *testing.T) {
|
|||
|
||||
server.register(w, req)
|
||||
|
||||
body, _ := ioutil.ReadAll(w.Body)
|
||||
|
||||
expectStatusCode(t, w, tc.expectedStatusCode)
|
||||
expectErrorString(t, w, tc.expectedErrorString)
|
||||
expectErrorString(t, body, tc.expectedErrorString)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -128,8 +128,14 @@ func expectStatusCode(t *testing.T, w *httptest.ResponseRecorder, expectedStatus
|
|||
|
||||
// expectErrorString: A helper to call in functions that test that request
|
||||
// handlers failed with a certain error string. Cuts down on noise.
|
||||
func expectErrorString(t *testing.T, w *httptest.ResponseRecorder, expectedErrorString string) {
|
||||
body, _ := ioutil.ReadAll(w.Body)
|
||||
func expectErrorString(t *testing.T, body []byte, expectedErrorString string) {
|
||||
if len(body) == 0 {
|
||||
// Nothing to decode
|
||||
if expectedErrorString == "" {
|
||||
return // Nothing expected either, we're all good
|
||||
}
|
||||
t.Errorf("Error String: expected %s, got an empty body (no JSON to decode)", expectedErrorString)
|
||||
}
|
||||
|
||||
var result ErrorResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
|
@ -206,10 +212,10 @@ func TestServerHelperCheckAuth(t *testing.T) {
|
|||
if !tc.tokenExpected && (authToken != nil) {
|
||||
t.Errorf("Expected checkAuth not to return a valid AuthToken")
|
||||
}
|
||||
body, _ := ioutil.ReadAll(w.Body)
|
||||
|
||||
expectStatusCode(t, w, tc.expectedStatusCode)
|
||||
if len(tc.expectedErrorString) > 0 {
|
||||
expectErrorString(t, w, tc.expectedErrorString)
|
||||
}
|
||||
expectErrorString(t, body, tc.expectedErrorString)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -283,9 +289,10 @@ func TestServerHelperGetPostDataErrors(t *testing.T) {
|
|||
if success {
|
||||
t.Errorf("getPostData succeeded unexpectedly")
|
||||
}
|
||||
body, _ := ioutil.ReadAll(w.Body)
|
||||
|
||||
expectStatusCode(t, w, tc.expectedStatusCode)
|
||||
expectErrorString(t, w, tc.expectedErrorString)
|
||||
expectErrorString(t, body, tc.expectedErrorString)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -96,16 +96,18 @@ func TestServerGetWallet(t *testing.T) {
|
|||
t.Errorf("testStore.Called.GetToken called with: expected %s, got %s", want, got)
|
||||
}
|
||||
|
||||
expectStatusCode(t, w, tc.expectedStatusCode)
|
||||
body, _ := ioutil.ReadAll(w.Body)
|
||||
|
||||
if len(tc.expectedErrorString) > 0 {
|
||||
// Only do this check if we're expecting an error and only an error,
|
||||
// since it reads the body and would break the ioutil.ReadAll below.
|
||||
expectErrorString(t, w, tc.expectedErrorString)
|
||||
return
|
||||
expectStatusCode(t, w, tc.expectedStatusCode)
|
||||
expectErrorString(t, body, tc.expectedErrorString)
|
||||
|
||||
// In this case, a wallet body is expected iff there is no error string
|
||||
expectWalletBody := len(tc.expectedErrorString) == 0
|
||||
|
||||
if !expectWalletBody {
|
||||
return // The rest of the test does not apply
|
||||
}
|
||||
|
||||
body, _ := ioutil.ReadAll(w.Body)
|
||||
var result WalletResponse
|
||||
err := json.Unmarshal(body, &result)
|
||||
|
||||
|
@ -331,26 +333,23 @@ func TestServerPostWallet(t *testing.T) {
|
|||
t.Errorf("testStore.Called.GetToken called with: expected %s, got %s", want, got)
|
||||
}
|
||||
|
||||
body, _ := ioutil.ReadAll(w.Body)
|
||||
|
||||
expectStatusCode(t, w, tc.expectedStatusCode)
|
||||
expectErrorString(t, body, tc.expectedErrorString)
|
||||
|
||||
if !tc.expectWalletBody {
|
||||
// Only do this check if we're expecting an error and only an error,
|
||||
// since it reads the body and would break the ioutil.ReadAll below.
|
||||
expectErrorString(t, w, tc.expectedErrorString)
|
||||
return
|
||||
return // The rest of the test does not apply
|
||||
}
|
||||
|
||||
body, _ := ioutil.ReadAll(w.Body)
|
||||
var result WalletResponse
|
||||
err := json.Unmarshal(body, &result)
|
||||
|
||||
if err != nil ||
|
||||
result.EncryptedWallet != tc.returnedEncryptedWallet ||
|
||||
result.Hmac != tc.returnedHmac ||
|
||||
result.Sequence != tc.returnedSequence ||
|
||||
result.Error != tc.expectedErrorString {
|
||||
|
||||
t.Errorf("Expected wallet response to have the test wallet values and error string: result: %+v err: %+v", string(body), err)
|
||||
result.Sequence != tc.returnedSequence {
|
||||
t.Errorf("Expected wallet response to have the test wallet values: result: %+v err: %+v", string(body), err)
|
||||
}
|
||||
|
||||
if want, got := (SetWalletCall{tc.newEncryptedWallet, tc.newSequence, tc.newHmac}), testStore.Called.SetWallet; want != got {
|
||||
|
|
Loading…
Reference in a new issue