|
| 1 | +// Copyright (c) 2026 ByteDance Ltd. and/or its affiliates. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package com.volcengine.ark.runtime.interceptor; |
| 5 | + |
| 6 | +import static org.junit.Assert.assertEquals; |
| 7 | +import static org.junit.Assert.assertNotNull; |
| 8 | +import static org.junit.Assert.assertTrue; |
| 9 | + |
| 10 | +import com.volcengine.ark.runtime.Const; |
| 11 | +import java.io.File; |
| 12 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 13 | +import javax.xml.xpath.XPathFactory; |
| 14 | +import okhttp3.OkHttpClient; |
| 15 | +import okhttp3.Protocol; |
| 16 | +import okhttp3.Request; |
| 17 | +import okhttp3.Response; |
| 18 | +import okhttp3.ResponseBody; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +public class RequestIdInterceptorTest { |
| 22 | + @Test |
| 23 | + public void outgoingRequestUsesProjectVersionAndPreservesRuntimeSuffix() throws Exception { |
| 24 | + Request request = new Request.Builder() |
| 25 | + .url("https://example.com/api/v3/chat/completions") |
| 26 | + .header("User-Agent", "custom-agent") |
| 27 | + .header(Const.CLIENT_REQUEST_HEADER, "provided-request-id") |
| 28 | + .build(); |
| 29 | + Request outgoing = intercept(request); |
| 30 | + String version = XPathFactory.newInstance().newXPath().evaluate("/project/version", |
| 31 | + DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("pom.xml"))); |
| 32 | + assertTrue(!version.isEmpty()); |
| 33 | + assertEquals("ark-runtime-java/" + version + "/(java-" + System.getProperty("java.version") |
| 34 | + + ";" + System.getProperty("os.name") + "-" + System.getProperty("os.version") |
| 35 | + + ";" + System.getProperty("os.arch") + ")", outgoing.header("User-Agent")); |
| 36 | + assertEquals(1, outgoing.headers("User-Agent").size()); |
| 37 | + assertEquals("provided-request-id", outgoing.header(Const.CLIENT_REQUEST_HEADER)); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void outgoingRequestStillGeneratesClientRequestId() throws Exception { |
| 42 | + Request outgoing = intercept(new Request.Builder().url("https://example.com").build()); |
| 43 | + assertNotNull(outgoing.header(Const.CLIENT_REQUEST_HEADER)); |
| 44 | + assertEquals(34, outgoing.header(Const.CLIENT_REQUEST_HEADER).length()); |
| 45 | + assertTrue(outgoing.header("User-Agent").startsWith("ark-runtime-java/")); |
| 46 | + } |
| 47 | + |
| 48 | + private Request intercept(Request request) throws Exception { |
| 49 | + OkHttpClient client = new OkHttpClient.Builder() |
| 50 | + .addInterceptor(new RequestIdInterceptor()) |
| 51 | + .addInterceptor(chain -> new Response.Builder() |
| 52 | + .request(chain.request()) |
| 53 | + .protocol(Protocol.HTTP_1_1) |
| 54 | + .code(200) |
| 55 | + .message("OK") |
| 56 | + .body(ResponseBody.create(null, "{}")) |
| 57 | + .build()) |
| 58 | + .build(); |
| 59 | + try (Response response = client.newCall(request).execute()) { |
| 60 | + return response.request(); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments