Added test case for HttpClient and fixed a small newline issue with the payload
This commit is contained in:
parent
28bef3935e
commit
66e7ed4aaf
2 changed files with 137 additions and 22 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* The MIT License (MIT)
|
* The MIT License (MIT)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2020 Ziver Koc
|
* Copyright (c) 2020-2024 Ziver Koc
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -143,30 +143,38 @@ public class HttpClient implements AutoCloseable {
|
||||||
this.content = content;
|
this.content = content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Socket getSocket(int port) throws IOException {
|
||||||
|
Socket socket = null;
|
||||||
|
|
||||||
|
if ("https".equals(url.getProtocol())) {
|
||||||
|
socket = SSLSocketFactory.getDefault().createSocket(url.getHost(), port);
|
||||||
|
((SSLSocket)socket).startHandshake();
|
||||||
|
} else {
|
||||||
|
socket = new Socket(url.getHost(), port);
|
||||||
|
}
|
||||||
|
|
||||||
|
return socket;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will send a HTTP request to the target host.
|
* Will send a HTTP request to the target host.
|
||||||
* NOTE: any previous request connections will be closed
|
* NOTE: any previous request connections will be closed
|
||||||
*/
|
*/
|
||||||
public HttpHeader send() throws IOException {
|
public HttpHeader send() throws IOException {
|
||||||
int port = 80;
|
int port = 80;
|
||||||
if (url == null)
|
if (url == null) {
|
||||||
throw new IllegalArgumentException("No URL defined for request.");
|
throw new IllegalArgumentException("No URL defined for request.");
|
||||||
else if (url.getPort() > 0)
|
} else if (url.getPort() > 0) {
|
||||||
port = url.getPort();
|
port = url.getPort();
|
||||||
else if ("https".equals(url.getProtocol()))
|
} else if ("https".equals(url.getProtocol())) {
|
||||||
port = 443;
|
port = 443;
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------
|
// ---------------------------------
|
||||||
// Create Socket
|
// Create Socket
|
||||||
// ---------------------------------
|
// ---------------------------------
|
||||||
|
|
||||||
Socket conn;
|
Socket conn = getSocket(port);
|
||||||
if ("https".equals(url.getProtocol())) {
|
|
||||||
conn = SSLSocketFactory.getDefault().createSocket(url.getHost(), port);
|
|
||||||
((SSLSocket)conn).startHandshake();
|
|
||||||
} else {
|
|
||||||
conn = new Socket(url.getHost(), port);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------
|
// ---------------------------------
|
||||||
// Request
|
// Request
|
||||||
|
|
@ -185,17 +193,21 @@ public class HttpClient implements AutoCloseable {
|
||||||
|
|
||||||
// send payload
|
// send payload
|
||||||
|
|
||||||
if (HttpRequestType.POST.toString().equals(type)) {
|
String payload = null;
|
||||||
String postData;
|
|
||||||
if (content != null)
|
if (content != null) {
|
||||||
postData = content;
|
payload = content;
|
||||||
else
|
} else if (HttpRequestType.POST.toString().equals(type)) {
|
||||||
postData = url.getParameterString();
|
payload = url.getParameterString();
|
||||||
request.setHeader(HttpHeader.HEADER_CONTENT_LENGTH, "" + postData.length());
|
}
|
||||||
request.println();
|
|
||||||
request.print(postData);
|
if (payload != null) {
|
||||||
} else
|
request.setHeader(HttpHeader.HEADER_CONTENT_LENGTH, "" + payload.length());
|
||||||
request.println();
|
request.print(payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finish off by sending all buffered data
|
||||||
|
|
||||||
request.flush();
|
request.flush();
|
||||||
|
|
||||||
// ---------------------------------
|
// ---------------------------------
|
||||||
|
|
|
||||||
103
test/zutil/net/http/HttpClientTest.java
Normal file
103
test/zutil/net/http/HttpClientTest.java
Normal file
|
|
@ -0,0 +1,103 @@
|
||||||
|
/*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2024 Ziver Koc
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package zutil.net.http;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import zutil.io.StringInputStream;
|
||||||
|
import zutil.io.StringOutputStream;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
|
||||||
|
public class HttpClientTest {
|
||||||
|
|
||||||
|
private class HttpClientMock extends HttpClient {
|
||||||
|
StringOutputStream outputStream = new StringOutputStream();
|
||||||
|
StringInputStream inputStream = new StringInputStream();
|
||||||
|
|
||||||
|
public HttpClientMock(HttpRequestType httpRequestType) {
|
||||||
|
super(httpRequestType);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Socket getSocket(int port) throws IOException {
|
||||||
|
return new Socket() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public OutputStream getOutputStream() {
|
||||||
|
return outputStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream getInputStream(){
|
||||||
|
return inputStream;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sendPostJson() throws IOException {
|
||||||
|
HttpClientMock client = new HttpClientMock(HttpClient.HttpRequestType.POST);
|
||||||
|
client.setURL("https://example.com/v1-beta/gql");
|
||||||
|
client.setHeader("Authorization", "Bearer ABCDEF");
|
||||||
|
client.setHeader("Content-Type", "application/json");
|
||||||
|
String requestQuery =
|
||||||
|
"{\"query\": \"{" +
|
||||||
|
"viewer {" +
|
||||||
|
"homes {" +
|
||||||
|
"consumption(resolution: HOURLY, last: 1) {" +
|
||||||
|
"nodes {" +
|
||||||
|
"from " +
|
||||||
|
"to " +
|
||||||
|
"cost " + // Total cost
|
||||||
|
"unitPrice " + // Cost per Kwh
|
||||||
|
"unitPriceVAT " + // Cost per Kwh with taxes
|
||||||
|
"consumption " + // Total consumption
|
||||||
|
"consumptionUnit " + // Should be Kwh
|
||||||
|
"}" +
|
||||||
|
"}" +
|
||||||
|
"}" +
|
||||||
|
"}" +
|
||||||
|
"}\"}";
|
||||||
|
client.setContent(requestQuery);
|
||||||
|
client.send();
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
"POST /v1-beta/gql HTTP/1.0" + System.lineSeparator() +
|
||||||
|
"Authorization: Bearer ABCDEF" + System.lineSeparator() +
|
||||||
|
"Content-Length: " + requestQuery.length() + System.lineSeparator() +
|
||||||
|
"Content-Type: application/json" + System.lineSeparator() +
|
||||||
|
"Host: example.com:443" + System.lineSeparator() +
|
||||||
|
System.lineSeparator() +
|
||||||
|
requestQuery,
|
||||||
|
client.outputStream.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue