package com.baiyue.common.audit; import java.time.Instant; import org.junit.jupiter.api.Test; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.test.web.client.MockRestServiceServer; import org.springframework.web.client.RestClient; import static org.springframework.test.web.client.ExpectedCount.once; import static org.springframework.test.web.client.match.MockRestRequestMatchers.header; import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; import static org.springframework.test.web.client.response.MockRestResponseCreators.withAccepted; class HttpAuditSinkTest { private static final String ENDPOINT = "http://audit-service/internal/audit/events"; private static final String TOKEN = "0123456789abcdef0123456789abcdef"; @Test void sendsInternalServiceToken() { RestClient.Builder builder = RestClient.builder(); MockRestServiceServer server = MockRestServiceServer.bindTo(builder).build(); server.expect(once(), requestTo(ENDPOINT)) .andExpect(method(HttpMethod.POST)) .andExpect(header(HttpAuditSink.INTERNAL_SERVICE_TOKEN_HEADER, TOKEN)) .andRespond(withAccepted().contentType(MediaType.APPLICATION_JSON)); new HttpAuditSink(builder, ENDPOINT, TOKEN).publish(sampleEvent()); server.verify(); } private AuditEvent sampleEvent() { return new AuditEvent("audit-1", "ROLE_UPDATE", "ROLE", "1", "update role", "request-1", "trace-1", 1L, 1L, 1L, "admin", "PLATFORM", "WEB", "PUT", "/api/platform/v1/roles/1", "127.0.0.1", 10L, true, null, null, null, null, Instant.parse("2026-08-05T00:00:00Z"), "iam"); } }