Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: record servlet request full url #84

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,11 @@ private static <TRequest> boolean shouldSkip(ServletAdapter<TRequest, ?> adapter
return true;
}

String requestURI = adapter.getRequestURI(httpServletRequest);

// Filter invalid servlet path suffix
if (HTTP_METHOD_GET.equals(adapter.getMethod(httpServletRequest))) {
String servletPath = adapter.getServletPath(httpServletRequest);
return StringUtil.isEmpty(servletPath) || FILTERED_GET_URL_SUFFIX.stream().anyMatch(servletPath::contains);
return StringUtil.isEmpty(requestURI) || FILTERED_GET_URL_SUFFIX.stream().anyMatch(requestURI::contains);
}

// Filter invalid content-type
Expand All @@ -165,12 +166,10 @@ private static <TRequest> boolean shouldSkip(ServletAdapter<TRequest, ?> adapter
return true;
}

String uri = adapter.getRequestURI(httpServletRequest);

if (IgnoreUtils.ignoreOperation(uri)) {
if (IgnoreUtils.ignoreOperation(requestURI)) {
return true;
}

return !RecordLimiter.acquire(uri);
return !RecordLimiter.acquire(requestURI);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.arex.inst.httpservlet;

import io.arex.agent.bootstrap.model.Mocker;
import io.arex.agent.bootstrap.util.StringUtil;
import io.arex.inst.httpservlet.adapter.ServletAdapter;
import io.arex.inst.runtime.context.ContextManager;
import io.arex.inst.runtime.model.ArexConstants;
Expand Down Expand Up @@ -70,7 +71,7 @@ private void doExecute() {

Map<String, Object> requestAttributes = new HashMap<>();
requestAttributes.put("HttpMethod", adapter.getMethod(httpServletRequest));
requestAttributes.put("RequestPath", adapter.getServletPath(httpServletRequest));
requestAttributes.put("RequestPath", adapter.getFullUrl(httpServletRequest));
requestAttributes.put("Headers", getRequestHeaders());

Map<String, Object> responseAttributes = Collections.singletonMap("Headers", getResponseHeaders());
Expand Down Expand Up @@ -110,7 +111,7 @@ private Map<String, String> getResponseHeaders() {

private String getRequest() {
if ("GET".equals(adapter.getMethod(httpServletRequest))) {
return adapter.getQueryString(httpServletRequest);
return StringUtil.EMPTY;
}
// Compatible with custom message converters that include compression
return Base64.getEncoder().encodeToString(adapter.getRequestBytes(httpServletRequest));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void addListener(ServletAdapter<HttpServletRequest, HttpServletResponse> adapter

String getContentType(HttpServletRequest httpServletRequest);

String getServletPath(HttpServletRequest httpServletRequest);
String getFullUrl(HttpServletRequest httpServletRequest);

String getRequestURI(HttpServletRequest httpServletRequest);

Expand All @@ -54,8 +54,6 @@ void addListener(ServletAdapter<HttpServletRequest, HttpServletResponse> adapter

Collection<String> getResponseHeaderNames(HttpServletResponse httpServletResponse);

String getQueryString(HttpServletRequest httpServletRequest);

byte[] getRequestBytes(HttpServletRequest httpServletRequest);

byte[] getResponseBytes(HttpServletResponse httpServletResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,15 @@ public String getContentType(HttpServletRequest httpServletRequest) {
}

@Override
public String getServletPath(HttpServletRequest httpServletRequest) {
return httpServletRequest.getServletPath();
public String getFullUrl(HttpServletRequest httpServletRequest) {
StringBuilder fullUrl = new StringBuilder(httpServletRequest.getRequestURI());
String queryString = httpServletRequest.getQueryString();

if (queryString == null) {
return fullUrl.toString();
} else {
return fullUrl.append('?').append(queryString).toString();
}
}

@Override
Expand Down Expand Up @@ -127,11 +134,6 @@ public Collection<String> getResponseHeaderNames(HttpServletResponse httpServlet
return httpServletResponse.getHeaderNames();
}

@Override
public String getQueryString(HttpServletRequest httpServletRequest) {
return httpServletRequest.getQueryString();
}

@Override
public byte[] getRequestBytes(HttpServletRequest httpServletRequest) {
return ((CachedBodyRequestWrapperV3) httpServletRequest).getContentAsByteArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,15 @@ public String getContentType(HttpServletRequest httpServletRequest) {
}

@Override
public String getServletPath(HttpServletRequest httpServletRequest) {
return httpServletRequest.getServletPath();
public String getFullUrl(HttpServletRequest httpServletRequest) {
StringBuilder fullUrl = new StringBuilder(httpServletRequest.getRequestURI());
String queryString = httpServletRequest.getQueryString();

if (queryString == null) {
return fullUrl.toString();
} else {
return fullUrl.append('?').append(queryString).toString();
}
}

@Override
Expand Down Expand Up @@ -127,11 +134,6 @@ public Collection<String> getResponseHeaderNames(HttpServletResponse httpServlet
return httpServletResponse.getHeaderNames();
}

@Override
public String getQueryString(HttpServletRequest httpServletRequest) {
return httpServletRequest.getQueryString();
}

@Override
public byte[] getRequestBytes(HttpServletRequest httpServletRequest) {
return ((CachedBodyRequestWrapperV5) httpServletRequest).getContentAsByteArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static Stream<Arguments> onServiceEnterCase() {
Runnable mocker5 = () -> {
Mockito.when(adapter.getRequestHeader(any(), eq(ArexConstants.REPLAY_WARM_UP))).thenReturn("false");
Mockito.when(adapter.getMethod(any())).thenReturn("GET");
Mockito.when(adapter.getServletPath(any())).thenReturn(".png");
Mockito.when(adapter.getFullUrl(any())).thenReturn(".png");
};
Runnable mocker6 = () -> {
Mockito.when(adapter.getMethod(any())).thenReturn("POST");
Expand Down