Skip to content

Commit 9803d25

Browse files
apotterridustinbyrne
authored andcommitted
fix: use the correct ClassLoader
When looking for HttpClient classes (e.g. org.apache.http.client.utils.URIBuilder), use the current thread's class loader, rather than the default.
1 parent 65b8166 commit 9803d25

File tree

24 files changed

+577
-70
lines changed

24 files changed

+577
-70
lines changed

agent/bin/test_run

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,21 @@ set -e
33

44
export PATH="$BATS_DIR"/bin:"$BASHUNIT_DIR":$PATH
55

6+
shopt -s extglob
67
set -x
7-
# bats has a mechanism to discover test files recursively, but it doesn't report errors properly.
8-
bats $(find "${@-test}" -name \*.bats)
8+
9+
# Use -r to discover tests. It comes with a couple of caveats:
10+
#
11+
# * it has problems reporting test scripts errors, i.e. problems in the scripts themselves, not
12+
# with commands they run. If you see failures that don't have sensible errors messages, try
13+
# something like
14+
#
15+
# $ bats $(find test/!(http_client) -name \*.bats)
16+
#
17+
# * just doing bats -r test doesn't discover a setup_suite.bash file correctly. http_client uses
18+
# one, so it needs to be run separately
19+
20+
bats -r test/!(http_client)
21+
bats -r test/http_client
922

1023
bashunit 'test/**/*_test.sh'

agent/src/main/java/com/appland/appmap/process/hooks/http/HttpClientRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,16 @@ private static void execute(Event event, String method, URIBuilder builder) {
8080

8181
static private URIBuilder newBuilder(URI uri) {
8282
try {
83-
Class<?> cls = Class.forName("org.apache.http.client.utils.URIBuilder");
83+
Class<?> cls = Class.forName("org.apache.http.client.utils.URIBuilder", true,
84+
Thread.currentThread().getContextClassLoader());
8485
Constructor<?> ctor = cls.getConstructor(java.net.URI.class);
8586
return new URIBuilder(ctor.newInstance(uri));
8687
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException
8788
| IllegalAccessException
8889
| IllegalArgumentException | InvocationTargetException e) {
8990
logger.error(e, "failed creating a URIBuilder");
9091
// This shouldn't ever happen: we've hooked a method in
91-
// org.apachage.http.client, so URIBuilder should always be available.
92+
// org.apache.http.client, so URIBuilder should always be available.
9293
throw new InternalError(e);
9394
}
9495
}

agent/test/http_client/appmap.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
name: HTTP client test
22
packages:
3-
- path: http_client
3+
- path: httpclient
Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1 @@
1-
#!/usr/bin/env bats
2-
3-
load '../../build/bats/bats-support/load'
4-
load '../../build/bats/bats-assert/load'
5-
load '../helper'
6-
load '../petclinic-shared/shared-setup.bash'
7-
8-
9-
setup_file() {
10-
export FIXTURE_DIR="build/fixtures/spring-petclinic"
11-
_shared_setup
12-
start_petclinic >&3
13-
14-
cd test/http_client
15-
}
16-
17-
teardown_file() {
18-
stop_ws
19-
}
20-
21-
@test "request without query" {
22-
run ./gradlew -q -PmainClass=http_client.HttpClientTest run ${DEBUG} --args "${WS_URL}/vets"
23-
24-
assert_json_eq '.events[1].http_client_request.request_method' "GET"
25-
assert_json_eq '.events[1].http_client_request.url' "${WS_URL}/vets"
26-
27-
# Neither message nor parameters should set set
28-
assert_json_eq '.events[1].message' ''
29-
assert_json_eq '.events[1].parameters' ''
30-
}
31-
32-
@test "request with query" {
33-
run ./gradlew -q -PmainClass=http_client.HttpClientTest run ${DEBUG} --args "${WS_URL}/owners?lastName=davis"
34-
35-
assert_json_eq '.events[1].http_client_request.url' "${WS_URL}/owners"
36-
assert_json_eq '.events[1].message | length' 1
37-
assert_json_eq '.events[1].message[0] | "\(.name) \(.value)"' "lastName davis"
38-
39-
assert_json_eq '.events[2].http_client_response.status' "200"
40-
}
41-
42-
@test "request without Content-Type" {
43-
run ./gradlew -q -PmainClass=http_client.HttpClientTest run ${DEBUG} --args "${WS_URL}/no-content"
44-
45-
assert_json_eq '.events[1].http_client_request.url' "${WS_URL}/no-content"
46-
assert_json_eq '.events[2].http_client_response.status' "200"
47-
}
48-
49-
@test "request with HttpHost" {
50-
run ./gradlew -q -PmainClass=http_client.HttpHostTest run ${DEBUG} --args "${WS_HOST} ${WS_PORT} /owners?lastName=davis"
51-
52-
assert_json_eq '.events[1].http_client_request.url' "${WS_URL}/owners"
53-
assert_json_eq '.events[1].message | length' 1
54-
assert_json_eq '.events[1].message[0] | "\(.name) \(.value)"' "lastName davis"
55-
56-
assert_json_eq '.events[2].http_client_response.status' "200"
57-
}
1+
# intentionally empty, bats requires a test file to enable detection of setup_suite.bash

agent/test/http_client/build.gradle renamed to agent/test/http_client/httpclient/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
plugins {
23
id 'application'
34
id 'java'
@@ -16,13 +17,13 @@ sourceSets {
1617
}
1718

1819
// TODO: look at consolidating this with the gradle config in httpcore
19-
def appmapJar = fileTree('../../build/libs').matching {
20+
def appmapJar = fileTree('../../../build/libs').matching {
2021
include {
2122
it.file.name ==~ /.*appmap-[0-9.]*(-SNAPSHOT)*.jar$/
2223
}
2324
}.getFiles()[0]
2425

25-
def annotationJar = fileTree('../../../annotation/build/libs').matching {
26+
def annotationJar = fileTree('../../../../annotation/build/libs').matching {
2627
include {
2728
it.file.name ==~ /.*annotation-[0-9.]*(-SNAPSHOT)*.jar$/
2829
}
@@ -47,7 +48,6 @@ application {
4748
System.env.JAVA_OUTPUT_OPTIONS,
4849
"-javaagent:${appmapJar}",
4950
"-Djava.util.logging.config.file=${System.env.JUL_CONFIG}",
50-
"-Dappmap.config.file=appmap.yml",
5151
//"-Dappmap.debug=true",
5252
//"-Dappmap.debug.file=../../build/log/httpclient-appmap.log"
5353
]
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
4+
networkTimeout=10000
5+
zipStoreBase=GRADLE_USER_HOME
6+
zipStorePath=wrapper/dists
File renamed without changes.
File renamed without changes.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env bats
2+
3+
load '../../../build/bats/bats-support/load'
4+
load '../../../build/bats/bats-assert/load'
5+
load '../../helper'
6+
load '../../petclinic-shared/shared-setup.bash'
7+
8+
9+
setup_file() {
10+
cd test/http_client/httpclient
11+
}
12+
13+
@test "request without query" {
14+
run ./gradlew -q -PmainClass=httpclient.HttpClientTest run ${DEBUG} --args "${WS_URL}/vets"
15+
16+
assert_json_eq '.events[1].http_client_request.request_method' "GET"
17+
assert_json_eq '.events[1].http_client_request.url' "${WS_URL}/vets"
18+
19+
# Neither message nor parameters should be set
20+
assert_json_eq '.events[1].message' ''
21+
assert_json_eq '.events[1].parameters' ''
22+
}
23+
24+
@test "request with query" {
25+
run ./gradlew -q -PmainClass=httpclient.HttpClientTest run ${DEBUG} --args "${WS_URL}/owners?lastName=davis"
26+
27+
assert_json_eq '.events[1].http_client_request.url' "${WS_URL}/owners"
28+
assert_json_eq '.events[1].message | length' 1
29+
assert_json_eq '.events[1].message[0] | "\(.name) \(.value)"' "lastName davis"
30+
31+
assert_json_eq '.events[2].http_client_response.status' "200"
32+
}
33+
34+
@test "request without Content-Type" {
35+
run ./gradlew -q -PmainClass=httpclient.HttpClientTest run ${DEBUG} --args "${WS_URL}/no-content"
36+
37+
assert_json_eq '.events[1].http_client_request.url' "${WS_URL}/no-content"
38+
assert_json_eq '.events[2].http_client_response.status' "200"
39+
}
40+
41+
@test "request with HttpHost" {
42+
run ./gradlew -q -PmainClass=httpclient.HttpHostTest run ${DEBUG} --args "${WS_HOST} ${WS_PORT} /owners?lastName=davis"
43+
44+
assert_json_eq '.events[1].http_client_request.url' "${WS_URL}/owners"
45+
assert_json_eq '.events[1].message | length' 1
46+
assert_json_eq '.events[1].message[0] | "\(.name) \(.value)"' "lastName davis"
47+
48+
assert_json_eq '.events[2].http_client_response.status' "200"
49+
}

0 commit comments

Comments
 (0)