/*
* Copyright 2017 LINE Corporation
*
* LINE Corporation licenses this file to you 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 com.linecorp.armeria.client.http;
import static com.linecorp.armeria.client.http.DefaultHttpClient.concatPaths;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.net.URI;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import com.linecorp.armeria.client.Client;
import com.linecorp.armeria.client.ClientBuilderParams;
import com.linecorp.armeria.client.ClientOptions;
import com.linecorp.armeria.client.ClientRequestContext;
import com.linecorp.armeria.client.DefaultClientBuilderParams;
import com.linecorp.armeria.client.Endpoint;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.common.http.HttpHeaders;
import com.linecorp.armeria.common.http.HttpMethod;
import com.linecorp.armeria.common.http.HttpRequest;
import com.linecorp.armeria.common.http.HttpResponse;
public class DefaultHttpClientTest {
@SuppressWarnings("unchecked")
@Test
public void testConcatenateRequestPath() throws Exception {
String clientUriPath = "http://127.0.0.1/hello";
String requestPath = "world/test?q1=foo";
Client<HttpRequest, HttpResponse> mockClientDelegate = mock(Client.class);
ClientBuilderParams clientBuilderParams = new DefaultClientBuilderParams(new HttpClientFactory(),
new URI(clientUriPath),
HttpClient.class,
ClientOptions.DEFAULT);
DefaultHttpClient defaultHttpClient = new DefaultHttpClient(clientBuilderParams,
mockClientDelegate,
SessionProtocol.of("http"),
Endpoint.of("127.0.0.1"));
defaultHttpClient.execute(HttpRequest.of(HttpHeaders.of(HttpMethod.GET, requestPath)));
ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class);
verify(mockClientDelegate).execute(any(ClientRequestContext.class),
httpRequestArgumentCaptor.capture());
String concatPath = httpRequestArgumentCaptor.getValue().path();
assertThat(concatPath).isEqualTo("/hello/world/test?q1=foo");
}
@Test
public void testConcatPaths() throws Exception {
assertThat(concatPaths(null, "a")).isEqualTo("/a");
assertThat(concatPaths(null, "/a")).isEqualTo("/a");
assertThat(concatPaths("", "a")).isEqualTo("/a");
assertThat(concatPaths("", "/a")).isEqualTo("/a");
assertThat(concatPaths("/", "a")).isEqualTo("/a");
assertThat(concatPaths("/", "/a")).isEqualTo("/a");
assertThat(concatPaths("/a", "b")).isEqualTo("/a/b");
assertThat(concatPaths("/a", "/b")).isEqualTo("/a/b");
assertThat(concatPaths("/a/", "/b")).isEqualTo("/a/b");
}
}