Skip to content

Commit

Permalink
feat: 所有 Forest 请求默认带上 User-Agent: forest/version 的请求头
Browse files Browse the repository at this point in the history
  • Loading branch information
mySingleLive committed Apr 25, 2023
1 parent 0e4c831 commit 10661bc
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
3 changes: 0 additions & 3 deletions forest-core/src/main/java/com/dtflys/forest/Forest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.dtflys.forest;

import com.dtflys.forest.config.ForestConfiguration;
import com.dtflys.forest.exceptions.ForestRuntimeException;
import com.dtflys.forest.http.ForestFuture;
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.http.ForestResponse;
import com.dtflys.forest.utils.ManifestUtil;
import com.dtflys.forest.utils.VersionUtil;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;
import java.util.stream.Collectors;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ protected OkHttpClient getClient(ForestRequest request, LifeCycleHandler lifeCyc
}

protected void prepareHeaders(Request.Builder builder) {
ForestJsonConverter jsonConverter = request.getConfiguration().getJsonConverter();
List<RequestNameValue> headerList = request.getHeaderNameValueList();
String contentType = request.getContentType();
String contentEncoding = request.getContentEncoding();
final ForestJsonConverter jsonConverter = request.getConfiguration().getJsonConverter();
final List<RequestNameValue> headerList = request.getHeaderNameValueList();
final String contentType = request.getContentType();
final String contentEncoding = request.getContentEncoding();
String contentTypeHeaderName = ForestHeader.CONTENT_TYPE;
String contentEncodingHeaderName = ForestHeader.CONTENT_ENCODING;
if (headerList != null && !headerList.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

package com.dtflys.forest.http;

import com.dtflys.forest.Forest;

/**
* Forest请求头接口
* <p>通过该接口可获取单个Forest请求头的信息</p>
Expand Down Expand Up @@ -199,6 +201,11 @@ public interface ForestHeader<SELF extends ForestHeader, VALUE> {
*/
String LOCATION = "Location";

/**
* 默认 Forest 请求的 User-Agent 值
*/
String DEFAULT_USER_AGENT_VALUE = "forest/" + Forest.VERSION;

/**
* 获取请求头的名称
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3150,11 +3150,23 @@ public List<RequestNameValue> getDataNameValueList() {


public List<RequestNameValue> getHeaderNameValueList() {
List<RequestNameValue> nameValueList = new ArrayList<RequestNameValue>();
final List<RequestNameValue> nameValueList = new LinkedList<>();
boolean hasUserAgent = false;
for (Iterator<ForestHeader> iterator = headers.headerIterator(); iterator.hasNext(); ) {
ForestHeader header = iterator.next();
RequestNameValue nameValue = new RequestNameValue(header.getName(), header.getValue(), TARGET_HEADER);
final ForestHeader header = iterator.next();
final String name = header.getName();
final String value = header.getValue();
RequestNameValue nameValue = new RequestNameValue(name, value, TARGET_HEADER);
nameValueList.add(nameValue);
if (!hasUserAgent && ForestHeader.USER_AGENT.equalsIgnoreCase(name)) {
hasUserAgent = true;
}
}
if (!hasUserAgent) {
nameValueList.add(0, new RequestNameValue(
ForestHeader.USER_AGENT,
ForestHeader.DEFAULT_USER_AGENT_VALUE,
TARGET_HEADER));
}
return nameValueList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.alibaba.fastjson.JSONObject;
import com.dtflys.forest.backend.HttpBackend;
import com.dtflys.forest.config.ForestConfiguration;
import com.dtflys.forest.http.ForestHeader;
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.http.ForestResponse;
import com.dtflys.forest.logging.ForestLogHandler;
Expand Down Expand Up @@ -121,6 +122,7 @@ public void testSimplePostWithProxy() {
"\t[Proxy]: host: 127.0.0.1, port: " + server.getPort() + "\n" +
"\tPOST http://localhost:" + server.getPort() + "/hello HTTP\n" +
"\tHeaders: \n" +
"\t\tUser-Agent: " + ForestHeader.DEFAULT_USER_AGENT_VALUE + "\n" +
"\t\tAccept: text/plain\n" +
"\t\tContent-Type: application/json\n" +
"\tBody: username=foo&password=123456");
Expand Down Expand Up @@ -633,6 +635,7 @@ public void testPostJsonWithLog() {
Mockito.verify(logger).info("[Forest] Request (" + configuration.getBackend().getName() + "): \n" +
"\tPOST http://localhost:" + server.getPort() + "/json HTTP\n" +
"\tHeaders: \n" +
"\t\tUser-Agent: " + ForestHeader.DEFAULT_USER_AGENT_VALUE + "\n" +
"\t\tAccept: application/json\n" +
"\t\tAuthorization: 1111111111111\n" +
"\t\tContent-Type: application/json\n" +
Expand All @@ -652,6 +655,7 @@ public void testPostJsonMapWithLog() {
Mockito.verify(logger).info("[Forest] Request (" + configuration.getBackend().getName() + "): \n" +
"\tPOST http://localhost:" + server.getPort() + "/json HTTP\n" +
"\tHeaders: \n" +
"\t\tUser-Agent: " + ForestHeader.DEFAULT_USER_AGENT_VALUE + "\n" +
"\t\tContent-Type: application/json\n" +
"\tBody: {\"username\":\"foo\"}");
}
Expand Down Expand Up @@ -703,6 +707,7 @@ public void testPostJsonObjectWithLog_content_noStatus() {
Mockito.verify(logger).info("[Forest] Request (" + configuration.getBackend().getName() + "): \n" +
"\tPOST http://localhost:" + server.getPort() + "/json HTTP\n" +
"\tHeaders: \n" +
"\t\tUser-Agent: " + ForestHeader.DEFAULT_USER_AGENT_VALUE + "\n" +
"\t\tAccept-Encoding: UTF-8\n" +
"\t\tContent-Type: application/json\n" +
"\tBody: {\"username\":\"foo\"}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dtflys.forest.Forest;
import com.dtflys.forest.backend.HttpBackend;
import com.dtflys.forest.http.ForestHeader;
import com.dtflys.forest.http.ForestResponse;
import com.dtflys.forest.logging.ForestLogger;
import com.dtflys.test.http.BaseClientTest;
Expand Down Expand Up @@ -71,6 +72,7 @@ public void testSimpleInterceptor() {
"\t[Type Change]: POST -> GET\n" +
"\tGET http://localhost:" + server.getPort() + "/hello/user?username=foo&username=foo HTTP\n" +
"\tHeaders: \n" +
"\t\tUser-Agent: " + ForestHeader.DEFAULT_USER_AGENT_VALUE + "\n" +
"\t\tAccept: text/plain");
}

Expand Down

0 comments on commit 10661bc

Please sign in to comment.