View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.client5.http.config;
29  
30  import java.util.Collections;
31  import java.util.concurrent.TimeUnit;
32  
33  import org.apache.hc.client5.http.auth.StandardAuthScheme;
34  import org.apache.hc.client5.http.cookie.StandardCookieSpec;
35  import org.apache.hc.core5.http.HttpHost;
36  import org.apache.hc.core5.util.TimeValue;
37  import org.apache.hc.core5.util.Timeout;
38  import org.junit.Assert;
39  import org.junit.Test;
40  
41  public class TestRequestConfig {
42  
43      @Test
44      public void testBasics() {
45          final RequestConfig config = RequestConfig.custom().build();
46          config.toString();
47      }
48  
49      @Test
50      public void testDefaults() {
51          final RequestConfig config = RequestConfig.DEFAULT;
52          Assert.assertEquals(Timeout.ofMinutes(3), config.getConnectTimeout());
53          Assert.assertEquals(Timeout.ofMinutes(3), config.getConnectionRequestTimeout());
54          Assert.assertEquals(false, config.isExpectContinueEnabled());
55          Assert.assertEquals(true, config.isAuthenticationEnabled());
56          Assert.assertEquals(true, config.isRedirectsEnabled());
57          Assert.assertEquals(false, config.isCircularRedirectsAllowed());
58          Assert.assertEquals(50, config.getMaxRedirects());
59          Assert.assertEquals(null, config.getCookieSpec());
60          Assert.assertEquals(null, config.getProxy());
61          Assert.assertEquals(null, config.getTargetPreferredAuthSchemes());
62          Assert.assertEquals(null, config.getProxyPreferredAuthSchemes());
63          Assert.assertEquals(true, config.isContentCompressionEnabled());
64      }
65  
66      @Test
67      public void testBuildAndCopy() throws Exception {
68          final RequestConfig config0 = RequestConfig.custom()
69                  .setConnectTimeout(33, TimeUnit.MILLISECONDS)
70                  .setConnectionRequestTimeout(44, TimeUnit.MILLISECONDS)
71                  .setExpectContinueEnabled(true)
72                  .setAuthenticationEnabled(false)
73                  .setRedirectsEnabled(false)
74                  .setCircularRedirectsAllowed(true)
75                  .setMaxRedirects(100)
76                  .setCookieSpec(StandardCookieSpec.STRICT)
77                  .setProxy(new HttpHost("someproxy"))
78                  .setTargetPreferredAuthSchemes(Collections.singletonList(StandardAuthScheme.NTLM))
79                  .setProxyPreferredAuthSchemes(Collections.singletonList(StandardAuthScheme.DIGEST))
80                  .setContentCompressionEnabled(false)
81                  .build();
82          final RequestConfig config = RequestConfig.copy(config0).build();
83          Assert.assertEquals(TimeValue.ofMilliseconds(33), config.getConnectTimeout());
84          Assert.assertEquals(TimeValue.ofMilliseconds(44), config.getConnectionRequestTimeout());
85          Assert.assertEquals(true, config.isExpectContinueEnabled());
86          Assert.assertEquals(false, config.isAuthenticationEnabled());
87          Assert.assertEquals(false, config.isRedirectsEnabled());
88          Assert.assertEquals(true, config.isCircularRedirectsAllowed());
89          Assert.assertEquals(100, config.getMaxRedirects());
90          Assert.assertEquals(StandardCookieSpec.STRICT, config.getCookieSpec());
91          Assert.assertEquals(new HttpHost("someproxy"), config.getProxy());
92          Assert.assertEquals(Collections.singletonList(StandardAuthScheme.NTLM), config.getTargetPreferredAuthSchemes());
93          Assert.assertEquals(Collections.singletonList(StandardAuthScheme.DIGEST), config.getProxyPreferredAuthSchemes());
94          Assert.assertEquals(false, config.isContentCompressionEnabled());
95      }
96  
97  }