BAEL-5486 Adding Parameters to Java HttpClient Requests (#11996)
* BAEL-5486 | Article Code * BAEL-5486 | Remove comments and format fix * BAEL-5486 | Add code sample * BAEL-5486 | Common code extracted to a method * BAEL-5486 | Use static import for Assertions * BAEL-5486 | Removed external library * BAEL-5486 | Removed BodyPublisher examples * BAEL-5486 | Code examples added * BAEL-5486 | Removed extra Class Co-authored-by: Avin Buricha <avin.buricha@gupshup.io>
This commit is contained in:
parent
23267c5e68
commit
74dbf0d0c9
|
@ -0,0 +1,45 @@
|
|||
package com.baeldung.httpclient.parameters;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class HttpClientParametersLiveTest {
|
||||
|
||||
private static HttpClient client;
|
||||
|
||||
@BeforeAll
|
||||
public static void setUp() {
|
||||
client = HttpClient.newHttpClient();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenQueryParams_whenGetRequest_thenResponseOk() throws IOException, InterruptedException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.version(HttpClient.Version.HTTP_2)
|
||||
.uri(URI.create("https://postman-echo.com/get?param1=value1¶m2=value2"))
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
assertEquals(response.statusCode(), 200);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenQueryParams_whenGetRequestWithDefaultConfiguration_thenResponseOk() throws IOException, InterruptedException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create("https://postman-echo.com/get?param1=value1¶m2=value2"))
|
||||
.build();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
assertEquals(response.statusCode(), 200);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue