T0.8: open-gateway 建仓——Spring Boot 2.7.18/JDK8 骨架 + EnvelopeWriter(裸码/信封两形态,信封与 SDK OpenApiEnvelope 字段对齐)+ CI/Dockerfile/.dockerignore
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package com.yunhai.platform.gateway.envelope;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* EnvelopeWriter 两形态写出契约(T0.8 TDD):
|
||||
* 裸码无信封 JSON;信封 HTTP 200 + code/msg/data 与 SDK OpenApiEnvelope 解析形态逐字段对齐。
|
||||
*/
|
||||
class EnvelopeWriterTest {
|
||||
|
||||
@Test
|
||||
void bare_status_written() throws IOException {
|
||||
MockHttpServletResponse res = new MockHttpServletResponse();
|
||||
EnvelopeWriter.writeBare(res, 401);
|
||||
assertThat(res.getStatus()).isEqualTo(401);
|
||||
assertThat(res.getContentAsString()).as("裸码无信封 JSON").isEmpty();
|
||||
|
||||
MockHttpServletResponse res404 = new MockHttpServletResponse();
|
||||
EnvelopeWriter.writeBare(res404, 404);
|
||||
assertThat(res404.getStatus()).isEqualTo(404);
|
||||
assertThat(res404.getContentAsString()).isEmpty();
|
||||
|
||||
MockHttpServletResponse res502 = new MockHttpServletResponse();
|
||||
EnvelopeWriter.writeBare(res502, 502);
|
||||
assertThat(res502.getStatus()).isEqualTo(502);
|
||||
assertThat(res502.getContentAsString()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void envelope_written_with_code_and_message() throws IOException {
|
||||
MockHttpServletResponse res = new MockHttpServletResponse();
|
||||
EnvelopeWriter.writeEnvelope(res, 7, "rate limited");
|
||||
assertThat(res.getStatus()).as("信封恒走 HTTP 200").isEqualTo(200);
|
||||
assertThat(res.getContentType()).contains("application/json");
|
||||
assertThat(res.getContentAsString())
|
||||
.isEqualTo("{\"code\":7,\"msg\":\"rate limited\",\"data\":null}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void rate_limit_constants_reserved() {
|
||||
assertThat(EnvelopeWriter.RATE_LIMITED).isEqualTo(7);
|
||||
assertThat(EnvelopeWriter.CIRCUIT_OPEN).isEqualTo(8);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user