insert all the logic into a type switch

This commit is contained in:
CicadaCinema 2023-07-05 21:05:22 +00:00
parent 2b5fbea6db
commit 00e43c60e5

View file

@ -540,34 +540,31 @@ func Test_register(t *testing.T) {
resp = Register(req, userAPI, &cfg.ClientAPI) resp = Register(req, userAPI, &cfg.ClientAPI)
switch resp.JSON.(type) { switch rr := resp.JSON.(type) {
case spec.InternalServerError, spec.MatrixError, util.JSONResponse: case spec.InternalServerError, spec.MatrixError, util.JSONResponse:
if !reflect.DeepEqual(tc.wantResponse, resp) { if !reflect.DeepEqual(tc.wantResponse, resp) {
t.Fatalf("unexpected response: %+v, want: %+v", resp, tc.wantResponse) t.Fatalf("unexpected response: %+v, want: %+v", resp, tc.wantResponse)
} }
return return
} case registerResponse:
// validate the response
rr, ok := resp.JSON.(registerResponse) if tc.forceEmpty {
if !ok { // when not supplying a username, one will be generated. Given this _SHOULD_ be
t.Fatalf("expected a registerresponse, got %T", resp.JSON) // the second user, set the username accordingly
} reg.Username = "2"
}
// validate the response wantUserID := strings.ToLower(fmt.Sprintf("@%s:%s", reg.Username, "test"))
if tc.forceEmpty { if wantUserID != rr.UserID {
// when not supplying a username, one will be generated. Given this _SHOULD_ be t.Fatalf("unexpected userID: %s, want %s", rr.UserID, wantUserID)
// the second user, set the username accordingly }
reg.Username = "2" if rr.DeviceID != *reg.DeviceID {
} t.Fatalf("unexpected deviceID: %s, want %s", rr.DeviceID, *reg.DeviceID)
wantUserID := strings.ToLower(fmt.Sprintf("@%s:%s", reg.Username, "test")) }
if wantUserID != rr.UserID { if rr.AccessToken == "" {
t.Fatalf("unexpected userID: %s, want %s", rr.UserID, wantUserID) t.Fatalf("missing accessToken in response")
} }
if rr.DeviceID != *reg.DeviceID { default:
t.Fatalf("unexpected deviceID: %s, want %s", rr.DeviceID, *reg.DeviceID) t.Fatalf("expected one of internalservererror, matrixerror, jsonresponse, registerresponse, got %T", resp.JSON)
}
if rr.AccessToken == "" {
t.Fatalf("missing accessToken in response")
} }
}) })
} }