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
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