Found a decent chunk of repeated code in http handler tests

This commit is contained in:
Daniel Krol 2022-06-19 16:57:37 -04:00
parent 05bf9e51ff
commit f472b73a68
3 changed files with 23 additions and 43 deletions

View file

@ -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)
})
}
}

View file

@ -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)
})
}
}

View file

@ -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)
})
}
}