Skip to content

Commit

Permalink
Make ClientRequestBase mock friendly (helidon-io#9281)
Browse files Browse the repository at this point in the history
Make ClientRequestBase mock friendly
  • Loading branch information
jbescos authored Sep 27, 2024
1 parent d8dad61 commit 7edf00b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public R request() {
}

@Override
public final R submit(Object entity) {
public R submit(Object entity) {
if (!(entity instanceof byte[] bytes && bytes.length == 0)) {
rejectHeadWithEntity();
}
Expand All @@ -271,7 +271,7 @@ public final R submit(Object entity) {
}

@Override
public final R outputStream(OutputStreamHandler outputStreamConsumer) {
public R outputStream(OutputStreamHandler outputStreamConsumer) {
rejectHeadWithEntity();
additionalHeaders();
return doOutputStream(outputStreamConsumer);
Expand Down Expand Up @@ -481,7 +481,7 @@ private String resolvePathParams(String path) {
}

private void rejectHeadWithEntity() {
if (this.method.equals(Method.HEAD)) {
if (Method.HEAD.equals(this.method)) {
throw new IllegalArgumentException("Payload in method '" + Method.HEAD + "' has no defined semantics");
}
}
Expand Down
5 changes: 5 additions & 0 deletions webclient/tests/webclient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,10 @@
<artifactId>helidon-logging-jul</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* 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 io.helidon.webclient.tests;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import io.helidon.http.HeaderName;
import io.helidon.http.HeaderNames;
import io.helidon.http.Status;
import io.helidon.webclient.api.HttpClientRequest;
import io.helidon.webclient.api.HttpClientResponse;
import io.helidon.webclient.api.WebClient;

import org.junit.jupiter.api.Test;

/**
* Tests WebClientMockTest.
*/
class WebClientMockTest {

@Test
public void useMock() {
WebClient webClient = mockWebClient(Status.OK_200);
HttpClientResponse response = webClient.post("http://test.com")
.header(HeaderNames.AUTHORIZATION, "Bearer asdsadsad")
.submit(new Object());
assertEquals(Status.OK_200, response.status());
}

private WebClient mockWebClient(Status status) {
WebClient webClient = mock(WebClient.class);
HttpClientRequest request = mock(HttpClientRequest.class);
HttpClientResponse response = mock(HttpClientResponse.class);
doReturn(request).when(webClient).post(anyString());
doReturn(request).when(request).header(any(HeaderName.class), anyString());
doReturn(response).when(request).submit(any(Object.class));
when(response.status()).thenReturn(status);
return webClient;
}

}

0 comments on commit 7edf00b

Please sign in to comment.