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

Update Client.java and add unit tests #2336

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions core/src/main/java/feign/Client.java
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 The Feign Authors
* Copyright 2012-2024 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -188,7 +188,7 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
}
}
// Avoid add "Accept-encoding" twice or more when "compression" option is enabled
if (field.equals(ACCEPT_ENCODING)) {
else if (field.equals(ACCEPT_ENCODING)) {
connection.addRequestProperty(field, String.join(", ", request.headers().get(field)));
break;
} else {
Expand Down
93 changes: 93 additions & 0 deletions core/src/test/java/feign/ClientTest.java
@@ -0,0 +1,93 @@
/*
* Copyright 2012-2024 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package feign;

import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.nio.charset.Charset;
import java.util.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class ClientTest {

@Test
void testClientExecute() throws IOException {
Client client = mock(Client.class);
RequestTemplate requestTemplate = mock(RequestTemplate.class);
Request request =
Request.create(Request.HttpMethod.GET, "http://example.com", Collections.emptyMap(),
null, requestTemplate);
when(client.execute(ArgumentMatchers.any(Request.class),
ArgumentMatchers.any())).thenReturn(Response.builder()
.status(200).request(request)
.body("Hello, World!", Charset.defaultCharset())
.build());
Response response = client.execute(request, null);
String result = Util.toString(response.body().asReader(Charset.defaultCharset()));
assertEquals("Hello, World!", result);
}

@Test
void testConvertAndSendWithAcceptEncoding() throws IOException {
Map<String, Collection<String>> headers = new HashMap<>();
List<String> acceptEncoding = new ArrayList<>();
acceptEncoding.add("gzip");
acceptEncoding.add("deflate");
headers.put(Util.ACCEPT_ENCODING, acceptEncoding);

RequestTemplate requestTemplate = mock(RequestTemplate.class);
Request.Body body = mock(Request.Body.class);
Request.Options options = mock(Request.Options.class);
Client client = mock(Client.class);

Request request = Request.create(Request.HttpMethod.GET, "http://example.com", headers, body,
requestTemplate);
Client.Default defaultClient = new Client.Default(null, null);
HttpURLConnection urlConnection = defaultClient.convertAndSend(request, options);
Map<String, List<String>> requestProperties = urlConnection.getRequestProperties();
// Test Avoid add "Accept-encoding" twice or more when "compression" option is enabled
assertEquals(1, requestProperties.get(Util.ACCEPT_ENCODING).size());
}

@Test
void testConvertAndSendWithContentLength() throws IOException {
Map<String, Collection<String>> headers = new HashMap<>();
List<String> acceptEncoding = new ArrayList<>();
headers.put(Util.CONTENT_LENGTH, Collections.singletonList("100"));

RequestTemplate requestTemplate = mock(RequestTemplate.class);
Request.Body body = mock(Request.Body.class);
Request.Options options = mock(Request.Options.class);
Client client = mock(Client.class);

Request request = Request.create(Request.HttpMethod.GET, "http://example.com", headers, body,
requestTemplate);
Client.Default defaultClient = new Client.Default(null, null);
HttpURLConnection urlConnection = defaultClient.convertAndSend(request, options);
Map<String, List<String>> requestProperties = urlConnection.getRequestProperties();
String requestProperty = urlConnection.getRequestProperty(Util.CONTENT_LENGTH);
/*
* By default, "Content-Length" will not be added because this key is in the
* restrictedHeaderSet of HttpURLConnection.
* Unless set system property "sun.net.http.allowRestrictedHeaders" to "true"
*/
assertNull(requestProperties.get(Util.CONTENT_LENGTH));
}

}