Skip to content

Commit 799e702

Browse files
author
eugenp
committed
logging work and http client usage
1 parent ccb9c4a commit 799e702

3 files changed

Lines changed: 58 additions & 19 deletions

File tree

httpclient/pom.xml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,32 @@
3030
<artifactId>httpclient</artifactId>
3131
<version>${httpclient.version}</version>
3232
</dependency>
33-
33+
34+
<!-- logging -->
35+
36+
<dependency>
37+
<groupId>org.slf4j</groupId>
38+
<artifactId>slf4j-api</artifactId>
39+
<version>${org.slf4j.version}</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>ch.qos.logback</groupId>
43+
<artifactId>logback-classic</artifactId>
44+
<version>${logback.version}</version>
45+
<!-- <scope>runtime</scope> -->
46+
</dependency>
47+
<dependency>
48+
<groupId>org.slf4j</groupId>
49+
<artifactId>jcl-over-slf4j</artifactId>
50+
<version>${org.slf4j.version}</version>
51+
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
52+
</dependency>
53+
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
54+
<groupId>org.slf4j</groupId>
55+
<artifactId>log4j-over-slf4j</artifactId>
56+
<version>${org.slf4j.version}</version>
57+
</dependency>
58+
3459
<!-- test scoped -->
3560

3661
<dependency>
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
<configuration>
22

3-
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4-
<encoder>
5-
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
6-
</pattern>
7-
</encoder>
8-
</appender>
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
6+
</pattern>
7+
</encoder>
8+
</appender>
99

10-
<logger name="org.springframework" level="WARN" />
11-
<logger name="org.springframework.transaction" level="WARN" />
10+
<logger name="org.apache.http" level="DEBUG" />
11+
<logger name="org.apache.http.wire" level="INFO" />
1212

13-
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
14-
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
15-
16-
<root level="INFO">
17-
<appender-ref ref="STDOUT" />
18-
</root>
13+
<root level="INFO">
14+
<appender-ref ref="STDOUT" />
15+
</root>
1916

2017
</configuration>

httpclient/src/test/java/org/baeldung/httpclient/HttpClientLiveTest.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
import org.apache.http.impl.client.CloseableHttpClient;
2323
import org.apache.http.impl.client.DefaultHttpClient;
2424
import org.apache.http.impl.client.HttpClientBuilder;
25+
import org.apache.http.impl.client.HttpClients;
2526
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
27+
import org.apache.http.params.CoreProtocolPNames;
28+
import org.apache.http.params.HttpProtocolParams;
2629
import org.apache.http.util.EntityUtils;
2730
import org.junit.After;
2831
import org.junit.Before;
@@ -129,16 +132,30 @@ public final void givenRequestWasSet_whenAnalyzingTheHeadersOfTheResponse_thenCo
129132

130133
// tests - headers
131134

135+
@SuppressWarnings("deprecation")
136+
@Test
137+
public final void givenDeprecatedApi_whenClientUsesCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
138+
final DefaultHttpClient client = new DefaultHttpClient();
139+
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 Firefox/26.0");
140+
HttpProtocolParams.setUserAgent(client.getParams(), "Mozilla/5.0 Firefox/26.0");
141+
142+
final HttpGet request = new HttpGet(SAMPLE_URL);
143+
response = client.execute(request);
144+
}
145+
132146
@SuppressWarnings("deprecation")
133147
@Test
134148
public final void givenDeprecatedApi_whenRequestHasCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
135-
instance = new DefaultHttpClient();
149+
final DefaultHttpClient client = new DefaultHttpClient();
136150
final HttpGet request = new HttpGet(SAMPLE_URL);
137151
request.setHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 Firefox/26.0");
152+
response = client.execute(request);
153+
}
138154

139-
response = instance.execute(request);
140-
141-
System.out.println(response);
155+
@Test
156+
public final void whenRequestHasCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
157+
instance = HttpClients.custom().setUserAgent("Mozilla/5.0 Firefox/26.0").build();
158+
response = instance.execute(new HttpGet(SAMPLE_URL));
142159
}
143160

144161
}

0 commit comments

Comments
 (0)