Skip to content

Commit f3bc388

Browse files
authored
fix: record servlet request redirect (#161)
1 parent 0af26e2 commit f3bc388

File tree

21 files changed

+523
-156
lines changed

21 files changed

+523
-156
lines changed

arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/model/Mocker.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.arex.agent.bootstrap.model;
22

3+
import io.arex.agent.bootstrap.util.StringUtil;
34
import java.io.Serializable;
45
import java.util.HashMap;
56
import java.util.Map;
@@ -90,4 +91,21 @@ public void setType(String type) {
9091
this.type = type;
9192
}
9293
}
94+
95+
default StringBuilder logBuilder() {
96+
StringBuilder builder = new StringBuilder("[arex]");
97+
boolean isReplay = StringUtil.isNotEmpty(getReplayId());
98+
if (isReplay) {
99+
builder.append("replay");
100+
} else {
101+
builder.append("record");
102+
}
103+
builder.append(" category: ").append(getCategoryType().getName());
104+
builder.append(", operation: ").append(getOperationName());
105+
builder.append(", recordId: ").append(getRecordId());
106+
if (isReplay) {
107+
builder.append(", replayId: ").append(getReplayId());
108+
}
109+
return builder;
110+
}
93111
}

arex-agent-core/src/test/java/io/arex/agent/instrumentation/InstrumentationInstallerTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,5 @@ void onTransformation() throws IOException {
119119
DynamicType dynamicType = Mockito.mock(DynamicType.class);
120120
listener.onTransformation(typeDescription, null, null, false, dynamicType);
121121
verify(dynamicType).saveIn(any());
122-
listener.onError(null, null,null, false, null);
123122
}
124-
}
123+
}
Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package io.arex.inst.runtime.context;
22

33
import io.arex.agent.bootstrap.util.StringUtil;
4+
import io.arex.inst.runtime.model.ArexConstants;
45
import java.util.ArrayList;
6+
import java.util.HashMap;
57
import java.util.List;
68
import java.util.Map;
9+
import java.util.Objects;
710
import java.util.Set;
811
import java.util.concurrent.ConcurrentHashMap;
912

@@ -17,16 +20,16 @@ public class ArexContext {
1720
private final Map<String, Object> cachedReplayResultMap = new ConcurrentHashMap<>();
1821
private Map<String, Set<String>> excludeMockTemplate;
1922

20-
public String getCaseId() {
21-
return this.caseId;
22-
}
23+
private Map<String, Object> attachments = null;
2324

24-
public String getReplayId() {
25-
return this.replayId;
25+
private boolean isRedirectRequest;
26+
27+
public static ArexContext of(String caseId) {
28+
return of(caseId, null);
2629
}
2730

28-
public long getCreateTime() {
29-
return createTime;
31+
public static ArexContext of(String caseId, String replayId) {
32+
return new ArexContext(caseId, replayId);
3033
}
3134

3235
private ArexContext(String caseId, String replayId) {
@@ -36,32 +39,28 @@ private ArexContext(String caseId, String replayId) {
3639
this.replayId = replayId;
3740
}
3841

39-
public boolean isReplay() {
40-
return StringUtil.isNotEmpty(this.replayId);
41-
}
42-
43-
public void add(String key, String value) {
44-
42+
public String getCaseId() {
43+
return this.caseId;
4544
}
4645

47-
public String get(String key) {
48-
return null;
46+
public String getReplayId() {
47+
return this.replayId;
4948
}
5049

51-
public int calculateSequence(String target) {
52-
return StringUtil.isEmpty(target) ? 0 : sequence.get(target);
50+
public long getCreateTime() {
51+
return createTime;
5352
}
5453

55-
public int calculateSequence() {
56-
return 0;
54+
public boolean isRecord() {
55+
return !isReplay();
5756
}
5857

59-
public static ArexContext of(String caseId) {
60-
return of(caseId, null);
58+
public boolean isReplay() {
59+
return StringUtil.isNotEmpty(this.replayId);
6160
}
6261

63-
public static ArexContext of(String caseId, String replayId) {
64-
return new ArexContext(caseId, replayId);
62+
public int calculateSequence(String target) {
63+
return StringUtil.isEmpty(target) ? 0 : sequence.get(target);
6564
}
6665

6766
public List<Integer> getMethodSignatureHashList() {
@@ -78,12 +77,61 @@ public Map<String, Set<String>> getExcludeMockTemplate() {
7877
public void setExcludeMockTemplate(Map<String, Set<String>> excludeMockTemplate) {
7978
this.excludeMockTemplate = excludeMockTemplate;
8079
}
80+
81+
public void setAttachment(String key, Object value) {
82+
if (attachments == null) {
83+
attachments = new HashMap<>();
84+
}
85+
86+
attachments.put(key, value);
87+
}
88+
89+
public Object getAttachment(String key) {
90+
if (attachments == null) {
91+
return null;
92+
}
93+
94+
return attachments.get(key);
95+
}
96+
97+
public boolean isRedirectRequest() {
98+
return isRedirectRequest;
99+
}
100+
101+
public void setRedirectRequest(boolean redirectRequest) {
102+
isRedirectRequest = redirectRequest;
103+
}
104+
105+
public boolean isRedirectRequest(String referer) {
106+
if (attachments == null) {
107+
isRedirectRequest = false;
108+
return false;
109+
}
110+
111+
if (StringUtil.isEmpty(referer)) {
112+
isRedirectRequest = false;
113+
return false;
114+
}
115+
116+
Object location = attachments.get(ArexConstants.REDIRECT_REFERER);
117+
118+
if (location == null) {
119+
isRedirectRequest = false;
120+
return false;
121+
}
122+
isRedirectRequest = Objects.equals(location, referer);
123+
return isRedirectRequest;
124+
}
125+
81126
public void clear() {
82127
methodSignatureHashList.clear();
83128
cachedReplayResultMap.clear();
84129
sequence.clear();
85130
if (excludeMockTemplate != null) {
86131
excludeMockTemplate.clear();
87132
}
133+
if (attachments != null) {
134+
attachments.clear();
135+
}
88136
}
89137
}

arex-instrumentation-api/src/main/java/io/arex/inst/runtime/context/ContextManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public static ArexContext currentContext(boolean createIfAbsent, String caseId)
5151
return RECORD_MAP.get(traceId);
5252
}
5353

54+
public static ArexContext getRecordContext(String recordId) {
55+
return RECORD_MAP.get(recordId);
56+
}
57+
5458
public static boolean needRecord() {
5559
ArexContext context = currentContext();
5660
return context != null && !context.isReplay();

arex-instrumentation-api/src/main/java/io/arex/inst/runtime/model/ArexConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ private ArexConstants() {}
77
public static final String REPLAY_ID = "arex-replay-id";
88
public static final String REPLAY_WARM_UP = "arex-replay-warm-up";
99
public static final String FORCE_RECORD = "arex-force-record";
10+
public static final String REDIRECT_REQUEST_METHOD = "arex-redirect-request-method";
11+
public static final String REDIRECT_REFERER = "arex-redirect-referer";
12+
public static final String REDIRECT_PATTERN = "arex-redirect-pattern";
1013
/**
1114
* mock template
1215
*/

arex-instrumentation-api/src/main/java/io/arex/inst/runtime/util/MockUtils.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
public final class MockUtils {
1919

20+
private static final String EMPTY_JSON = "{}";
21+
2022
private MockUtils() {
2123
}
2224

@@ -87,6 +89,11 @@ public static ArexMocker create(MockCategoryType categoryType, String operationN
8789

8890
public static void recordMocker(Mocker requestMocker) {
8991
String postJson = Serializer.serialize(requestMocker);
92+
93+
if (Config.get().isEnableDebug()) {
94+
LOGGER.info("{}\nrequest: {}", requestMocker.logBuilder(), postJson);
95+
}
96+
9097
DataService.INSTANCE.save(postJson);
9198
}
9299

@@ -96,8 +103,14 @@ public static Mocker replayMocker(Mocker requestMocker) {
96103

97104
public static Mocker replayMocker(Mocker requestMocker, MockStrategyEnum mockStrategy) {
98105
String postJson = Serializer.serialize(requestMocker);
106+
99107
String data = DataService.INSTANCE.query(postJson, mockStrategy);
100-
if (StringUtil.isEmpty(data) || "{}".equals(data)) {
108+
109+
if (Config.get().isEnableDebug()) {
110+
LOGGER.info("{}\nrequest: {}\nresponse: {}", requestMocker.logBuilder(), postJson, data);
111+
}
112+
113+
if (StringUtil.isEmpty(data) || EMPTY_JSON.equals(data)) {
101114
LOGGER.warn("[arex] response body is null. request: {}", postJson);
102115
return null;
103116
}
@@ -142,4 +155,4 @@ public static boolean checkResponseMocker(Mocker responseMocker) {
142155

143156
return true;
144157
}
145-
}
158+
}

arex-instrumentation-foundation/src/main/java/io/arex/foundation/services/DataCollectorService.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,6 @@ static void doSleep(long millis) {
106106
}
107107

108108
void saveData(DataEntity entity) {
109-
if (ConfigManager.INSTANCE.isEnableDebug()) {
110-
LOGGER.info("[arex] save mocker: {}", entity.getPostData());
111-
}
112109
AsyncHttpClientUtil.executeAsync(saveApiUrl, entity.getPostData()).whenComplete(saveMockDataConsumer(entity));
113110
}
114111

0 commit comments

Comments
 (0)