번호 자동 부여 대응 및 API 공통 처리 보강

This commit is contained in:
JiWoong Sul
2025-10-23 14:02:31 +09:00
parent 09c31b2503
commit 7e933a2dda
55 changed files with 948 additions and 586 deletions

View File

@@ -93,5 +93,62 @@ void main() {
verify(() => mapper.map(dioError)).called(1);
});
group('unwrapAsMap', () {
test('data 키가 있으면 내부 맵을 반환한다', () {
final response = Response<dynamic>(
requestOptions: RequestOptions(path: '/auth/login'),
statusCode: 200,
data: {
'data': {'token': 'abc'},
},
);
expect(client.unwrapAsMap(response), {'token': 'abc'});
});
test('data 키가 없으면 원본 맵을 반환한다', () {
final response = Response<dynamic>(
requestOptions: RequestOptions(path: '/menus'),
statusCode: 200,
data: {'items': []},
);
expect(client.unwrapAsMap(response), {'items': []});
});
test('본문이 null이면 빈 맵을 반환한다', () {
final response = Response<dynamic>(
requestOptions: RequestOptions(path: '/menus'),
statusCode: 200,
);
expect(client.unwrapAsMap(response), isEmpty);
});
});
group('unwrap', () {
test('data 키가 있는 리스트를 반환한다', () {
final response = Response<dynamic>(
requestOptions: RequestOptions(path: '/reports'),
statusCode: 200,
data: {
'data': [1, 2, 3],
},
);
expect(client.unwrap(response), [1, 2, 3]);
});
test('data 키가 없으면 원본을 그대로 반환한다', () {
final response = Response<dynamic>(
requestOptions: RequestOptions(path: '/health'),
statusCode: 200,
data: {'status': 'ok'},
);
expect(client.unwrap(response), {'status': 'ok'});
});
});
});
}

View File

@@ -80,4 +80,60 @@ void main() {
expect(asIterable(action), contains('approve'));
expect(asIterable(resource), contains('stock-transactions'));
});
group('401 응답 메시지 매핑', () {
test('invalid credentials 메시지를 한글로 변환한다', () {
final mapper = ApiErrorMapper();
final requestOptions = RequestOptions(path: '/auth/login');
final dioException = DioException(
requestOptions: requestOptions,
type: DioExceptionType.badResponse,
response: Response<dynamic>(
requestOptions: requestOptions,
statusCode: 401,
data: {
'error': {
'message': 'invalid credentials',
},
},
),
);
final exception = mapper.map(dioException);
expect(exception.code, ApiErrorCode.unauthorized);
expect(exception.message, '아이디 또는 비밀번호가 올바르지 않습니다.');
});
test('token expired 메시지를 세션 만료 문구로 변환하고 상세를 보존한다', () {
final mapper = ApiErrorMapper();
final requestOptions = RequestOptions(path: '/auth/refresh');
final dioException = DioException(
requestOptions: requestOptions,
type: DioExceptionType.badResponse,
response: Response<dynamic>(
requestOptions: requestOptions,
statusCode: 401,
data: {
'error': {
'message': 'token expired',
'details': [
{'field': 'token', 'message': '만료되었습니다.'},
],
},
},
),
);
final exception = mapper.map(dioException);
expect(exception.message, '세션이 만료되었습니다. 다시 로그인해 주세요.');
final tokenDetails = exception.details?['token'];
if (tokenDetails is Iterable) {
expect(tokenDetails, contains('만료되었습니다.'));
} else {
expect(tokenDetails, '만료되었습니다.');
}
});
});
}