package com.eactive.eai.kjb.errors;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.junit.jupiter.api.Test;

class BearerTokenFilterErrorTest {

	@Test
	void testCase001_잘못된Bearer토큰_401에러() throws Exception {
		// given
		String url = TestConfig.getBaseUrl("/api/auth/case009?transactionId=1234567890");
		String invalidToken = "INVALID_TOKEN_12345";

		// when
		try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
			HttpPost httpPost = new HttpPost(url);
			httpPost.setHeader("Authorization", "Bearer " + invalidToken);
			httpPost.setHeader("Content-Type", "application/json");
			httpPost.setEntity(new StringEntity("{}", ContentType.APPLICATION_JSON));

			String responseBody = httpClient.execute(httpPost, response -> {
				String body = EntityUtils.toString(response.getEntity());
				System.out.println("Response Body: " + body);

				// then
				assertEquals(401, response.getCode());
				return body;
			});

			// MessageUtil.makeJsonErrorMessage 기본 포맷 검증
			ErrorResponseValidator.validateErrorFormat(responseBody);
		}
	}
}
