Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions java/com/gurock/testrail/APIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* Copyright Gurock Software GmbH. See license.md for details.
*/

package com.gurock.testrail;

import java.net.URL;
Expand Down Expand Up @@ -91,7 +91,7 @@ public void setPassword(String password)
* either be an instance of JSONObject or JSONArray (depending on the
* API method). In most cases, this returns a JSONObject instance which
* is basically the same as java.util.Map.
*
*
* If 'get_attachment/:attachment_id', returns a String
*/
public Object sendGet(String uri, String data)
Expand Down Expand Up @@ -163,7 +163,7 @@ private Object sendRequest(String method, String uri, Object data)
BufferedWriter bodyWriter = new BufferedWriter(new OutputStreamWriter(ostreamBody));

bodyWriter.write("\n\n--" + boundary + "\r\n");
bodyWriter.write("Content-Disposition: form-data; name=\"attachment\"; filename=\""
bodyWriter.write("Content-Disposition: form-data; name=\"attachment\"; filename=\""
+ uploadFile.getName() + "\"");
bodyWriter.write("\r\n\r\n");
bodyWriter.flush();
Expand Down Expand Up @@ -218,31 +218,32 @@ private Object sendRequest(String method, String uri, Object data)
if (istream == null)
{
throw new APIException(
"TestRail API return HTTP " + status +
"TestRail API return HTTP " + status +
" (No additional error message received)"
);
}
}
else
else
{
istream = conn.getInputStream();
}

// If 'get_attachment' (not 'get_attachments') returned valid status code, save the file
if ((istream != null)
if ((istream != null)
&& (uri.startsWith("get_attachment/")))
{
FileOutputStream outputStream = new FileOutputStream((String)data);

int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = istream.read(buffer)) > 0)
while ((bytesRead = istream.read(buffer)) > 0)
{
outputStream.write(buffer, 0, bytesRead);
}

outputStream.close();
istream.close();
conn.disconnect();
return (String) data;
}

Expand All @@ -266,14 +267,15 @@ private Object sendRequest(String method, String uri, Object data)
}

reader.close();
conn.disconnect();
}

Object result;
if (!text.equals(""))
{
result = JSONValue.parse(text);
}
else
else
{
result = new JSONObject();
}
Expand Down Expand Up @@ -304,7 +306,7 @@ private Object sendRequest(String method, String uri, Object data)

private static String getAuthorization(String user, String password)
{
try
try
{
return new String(Base64.getEncoder().encode((user + ":" + password).getBytes()));
}
Expand Down