package common.ecommerce.hengjian.http; import common.ecommerce.http.HttpResponse; import common.ecommerce.http.JacksonJsonCodec; import common.ecommerce.shared.error.AuthException; import common.ecommerce.shared.error.PlatformBusinessException; import common.ecommerce.shared.error.TransientException; import common.ecommerce.shared.error.ValidationException; import org.junit.jupiter.api.Test; import java.util.Collections; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; /** * 恒健响应信封校验。 * *

把真机观察到的三种形态都固化成用例:正常业务信封、 * HTTP 200 + {@code code=999999}(未授权)、HTTP 500 + 「未知的账户」。 * 后续有人把信封改成严格数字解析、或把 999999 当普通业务失败,这些用例会挂。

*/ class HengjianEnvelopeTest { private static final JacksonJsonCodec JSON = new JacksonJsonCodec(); private static HttpResponse ok(String body) { return HttpResponse.of(200, Collections.emptyMap(), body, 0L); } private static HengjianEnvelope parse(HttpResponse response) { return HengjianEnvelope.of(response, JSON, "warehouse.test", "WH-1", "req-1"); } // ---------------------------------------------------------------- 成功 @Test void acceptsNumericCode() { String body = "{\"statusCode\":200,\"status\":0,\"code\":200,\"message\":\"查询成功\"}"; assertEquals(body, parse(ok(body)).body()); } /** 查询库存等接口页把 code 标成 STRING,必须同样按成功处理。 */ @Test void acceptsStringCode() { parse(ok("{\"code\":\"200\",\"message\":\"ok\"}")); } /** 个别接口页只列 status 不列 code。 */ @Test void fallsBackToStatusWhenCodeMissing() { parse(ok("{\"status\":200,\"message\":\"ok\"}")); } // ---------------------------------------------------------------- 失败 @Test void businessErrorIsNotRetryable() { PlatformBusinessException ex = assertThrows(PlatformBusinessException.class, () -> parse(ok("{\"code\":500,\"message\":\"sku 不存在\"}"))); assertTrue(ex.getMessage().contains("500"), ex.getMessage()); assertTrue(ex.getMessage().contains("sku 不存在"), ex.getMessage()); assertTrue(!ex.retryable()); } /** * 真机:不带授权头时 HTTP 200 但 body 里 code=999999。 * 这必须归认证失败——当普通业务失败宿主不会刷新凭据。 */ @Test void unauthorizedCodeIsAuthFailure() { AuthException ex = assertThrows(AuthException.class, () -> parse(ok( "{\"statusCode\":500,\"status\":1,\"code\":999999," + "\"message\":\"查询失败Unauthorized\"}"))); assertEquals("999999", ex.platformCode()); assertTrue(!ex.retryable()); } /** * 真机:只带一个授权头 / token 无效时 HTTP 500 + Spring 错误页。 * 消息虽是 500,根因是凭据,按认证失败处理,避免宿主无意义重试。 */ @Test void unknownAccountOn500IsAuthFailure() { AuthException ex = assertThrows(AuthException.class, () -> parse( HttpResponse.of(500, Collections.emptyMap(), "{\"timestamp\":\"2026-11-12T07:01:31.760+0000\",\"status\":500," + "\"error\":\"Internal Server Error\"," + "\"message\":\"未知的账户!\",\"path\":\"/v2/warehouse/getWarehouse\"}", 0L))); assertTrue(ex.getMessage().contains("unknown account"), ex.getMessage()); assertTrue(!ex.retryable()); } /** 普通 5xx(网关 / 后端故障)仍按瞬时错误处理。 */ @Test void plainServerErrorIsRetryable() { TransientException ex = assertThrows(TransientException.class, () -> parse( HttpResponse.of(502, Collections.emptyMap(), "bad gateway", 0L))); assertTrue(ex.retryable()); } @Test void http401IsAuthFailure() { assertThrows(AuthException.class, () -> parse( HttpResponse.of(401, Collections.emptyMap(), "", 0L))); } @Test void emptyBodyIsValidationError() { assertThrows(ValidationException.class, () -> parse(ok(""))); } @Test void missingCodeIsValidationError() { ValidationException ex = assertThrows(ValidationException.class, () -> parse(ok("{\"message\":\"no code here\"}"))); assertTrue(ex.getMessage().contains("no business code"), ex.getMessage()); } @Test void nonJsonErrorBodyIsValidationError() { assertThrows(ValidationException.class, () -> parse(ok("proxy error"))); } }