Found a decent chunk of repeated code in http handler tests
This commit is contained in:
parent
05bf9e51ff
commit
f472b73a68
3 changed files with 23 additions and 43 deletions
|
@ -92,20 +92,7 @@ func TestServerAuthHandlerErrors(t *testing.T) {
|
|||
|
||||
server.getAuthToken(w, req)
|
||||
|
||||
if want, got := tc.expectedStatusCode, w.Result().StatusCode; want != got {
|
||||
t.Errorf("StatusCode: expected %d, got %d", want, got)
|
||||
}
|
||||
|
||||
body, _ := ioutil.ReadAll(w.Body)
|
||||
|
||||
var result ErrorResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
t.Fatalf("Error decoding error message %s: `%s`", err, body)
|
||||
}
|
||||
|
||||
if want, got := tc.expectedErrorString, result.Error; want != got {
|
||||
t.Errorf("Error String: expected %s, got %s", want, got)
|
||||
}
|
||||
expectErrorResponse(t, w, tc.expectedStatusCode, tc.expectedErrorString)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package server
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
@ -78,20 +77,7 @@ func TestServerRegisterErrors(t *testing.T) {
|
|||
|
||||
server.register(w, req)
|
||||
|
||||
if want, got := tc.expectedStatusCode, w.Result().StatusCode; want != got {
|
||||
t.Errorf("StatusCode: expected %d, got %d", want, got)
|
||||
}
|
||||
|
||||
body, _ := ioutil.ReadAll(w.Body)
|
||||
|
||||
var result ErrorResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
t.Fatalf("Error decoding error message %s: `%s`", err, body)
|
||||
}
|
||||
|
||||
if want, got := tc.expectedErrorString, result.Error; want != got {
|
||||
t.Errorf("Error String: expected %s, got %s", want, got)
|
||||
}
|
||||
expectErrorResponse(t, w, tc.expectedStatusCode, tc.expectedErrorString)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -91,6 +91,26 @@ func (s *TestStore) GetWallet(userId auth.UserId) (encryptedWallet wallet.Encryp
|
|||
return
|
||||
}
|
||||
|
||||
// expectErrorResponse: A helper to call in functions that test that request
|
||||
// handlers fail with a certain status code and error string. Cuts down on
|
||||
// noise.
|
||||
func expectErrorResponse(t *testing.T, w *httptest.ResponseRecorder, expectedStatusCode int, expectedErrorString string) {
|
||||
if want, got := expectedStatusCode, w.Result().StatusCode; want != got {
|
||||
t.Errorf("StatusCode: expected %d, got %d", want, got)
|
||||
}
|
||||
|
||||
body, _ := ioutil.ReadAll(w.Body)
|
||||
|
||||
var result ErrorResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
t.Fatalf("Error decoding error message %s: `%s`", err, body)
|
||||
}
|
||||
|
||||
if want, got := expectedErrorString, result.Error; want != got {
|
||||
t.Errorf("Error String: expected %s, got %s", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerHelperCheckAuthSuccess(t *testing.T) {
|
||||
t.Fatalf("Test me: checkAuth success")
|
||||
}
|
||||
|
@ -168,20 +188,7 @@ func TestServerHelperGetPostDataErrors(t *testing.T) {
|
|||
t.Errorf("getPostData succeeded unexpectedly")
|
||||
}
|
||||
|
||||
if want, got := tc.expectedStatusCode, w.Result().StatusCode; want != got {
|
||||
t.Errorf("StatusCode: expected %d, got %d", want, got)
|
||||
}
|
||||
|
||||
body, _ := ioutil.ReadAll(w.Body)
|
||||
|
||||
var result ErrorResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
t.Fatalf("Error decoding error message %s: `%s`", err, body)
|
||||
}
|
||||
|
||||
if want, got := tc.expectedErrorString, result.Error; want != got {
|
||||
t.Errorf("Error String: expected %s, got %s", want, got)
|
||||
}
|
||||
expectErrorResponse(t, w, tc.expectedStatusCode, tc.expectedErrorString)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue