|
7 | 7 | import net.servicestack.client.JsonServiceClient;
|
8 | 8 |
|
9 | 9 | import net.servicestack.client.WebServiceException;
|
| 10 | +import net.servicestack.client.tests.TestDtos; |
10 | 11 | import test.dtos.*;
|
11 | 12 |
|
12 | 13 | import java.net.HttpURLConnection;
|
| 14 | +import java.nio.charset.StandardCharsets; |
13 | 15 | import java.util.Calendar;
|
14 | 16 | import java.util.Date;
|
| 17 | +import java.util.List; |
| 18 | +import java.util.Optional; |
15 | 19 |
|
16 | 20 | public class JsonServiceClientTests extends TestCase {
|
17 | 21 |
|
@@ -93,4 +97,99 @@ public void test_can_change_basePath() {
|
93 | 97 | client.setBasePath("/api/");
|
94 | 98 | assertEquals("https://test.servicestack.net/api/", client.getReplyUrl());
|
95 | 99 | }
|
| 100 | + |
| 101 | + public void test_Can_Post_file_with_Request() { |
| 102 | + try { |
| 103 | + TestDtos.SpeechToText request = new TestDtos.SpeechToText(); |
| 104 | + request.setTag("ztag"); |
| 105 | + request.setRefId("zid"); |
| 106 | + |
| 107 | + byte[] fileBytes = "Hello World".getBytes(StandardCharsets.UTF_8); |
| 108 | + FileUpload[] files = new FileUpload[]{ |
| 109 | + new FileUpload( |
| 110 | + "audio", // fieldName |
| 111 | + "test.txt", // fileName |
| 112 | + "text/plain", // contentType |
| 113 | + fileBytes // data |
| 114 | + ) |
| 115 | + }; |
| 116 | + |
| 117 | + TestDtos.GenerationResponse response = client.postFilesWithRequest( |
| 118 | + "/api/SpeechToText", |
| 119 | + request, |
| 120 | + files, |
| 121 | + TestDtos.GenerationResponse.class |
| 122 | + ); |
| 123 | + |
| 124 | + List<TestDtos.TextOutput> outputs = response.getTextOutputs(); |
| 125 | + |
| 126 | + assertNotNull("Response should not be null", response); |
| 127 | + assertNotNull("Text outputs should not be null", outputs); |
| 128 | + assertEquals("Should match expected output", "audio, Audio 11, test.txt, text/plain", outputs.get(0).getText()); |
| 129 | + assertEquals("Should match expected tag", "Tag ztag", outputs.get(1).getText()); |
| 130 | + assertEquals("Should match expected refId", "RefId zid", outputs.get(2).getText()); |
| 131 | + |
| 132 | + } catch (Exception e) { |
| 133 | + fail("Error during test: " + e.getMessage()); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public void test_Can_Post_Multiple_files_with_Request() { |
| 138 | + try { |
| 139 | + TestDtos.TestFileUploads request = new TestDtos.TestFileUploads(); |
| 140 | + request.setId(1); |
| 141 | + request.setRefId("zid"); |
| 142 | + |
| 143 | + byte[] textFileBytes = "Hello World".getBytes(StandardCharsets.UTF_8); |
| 144 | + byte[] markdownFileBytes = "## Heading".getBytes(StandardCharsets.UTF_8); |
| 145 | + |
| 146 | + FileUpload[] files = new FileUpload[]{ |
| 147 | + new FileUpload( |
| 148 | + "audio", // fieldName |
| 149 | + "test.txt", // fileName |
| 150 | + "text/plain", // contentType |
| 151 | + textFileBytes // data |
| 152 | + ), |
| 153 | + new FileUpload( |
| 154 | + "content", // fieldName |
| 155 | + "test.md", // fileName |
| 156 | + "text/markdown", // contentType |
| 157 | + markdownFileBytes // data |
| 158 | + ) |
| 159 | + }; |
| 160 | + |
| 161 | + TestDtos.TestFileUploadsResponse response = client.postFilesWithRequest( |
| 162 | + "/api/TestFileUploads", |
| 163 | + request, |
| 164 | + files, |
| 165 | + TestDtos.TestFileUploadsResponse.class |
| 166 | + ); |
| 167 | + |
| 168 | + assertNotNull("Response should not be null", response); |
| 169 | + assertEquals("Id should match", Optional.of(1), response.getId()); |
| 170 | + assertEquals("RefId should match", "zid", response.getRefId()); |
| 171 | + assertEquals("Should have correct number of files", 2, response.getFiles().size()); |
| 172 | + |
| 173 | + // Verify first file |
| 174 | + TestDtos.UploadInfo file1 = response.getFiles().get(0); |
| 175 | + assertEquals("First file name should match", "audio", file1.getName()); |
| 176 | + assertEquals("First filename should match", "test.txt", file1.getFileName()); |
| 177 | + assertEquals("First file content length should match", Optional.of("Hello World".length()), file1.getContentLength()); |
| 178 | + assertEquals("First file content type should match", "text/plain", file1.getContentType()); |
| 179 | + |
| 180 | + // Verify second file |
| 181 | + TestDtos.UploadInfo file2 = response.getFiles().get(1); |
| 182 | + assertEquals("Second file name should match", "content", file2.getName()); |
| 183 | + assertEquals("Second filename should match", "test.md", file2.getFileName()); |
| 184 | + assertEquals("Second file content length should match", Optional.of("## Heading".length()), file2.getContentLength()); |
| 185 | + assertEquals("Second file content type should match", "text/markdown", file2.getContentType()); |
| 186 | + |
| 187 | + } catch (Exception e) { |
| 188 | + fail("Error during test: " + e.getMessage()); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | +// Async versions would not be needed in Java as the API is already based on blocking calls |
| 193 | +// If async behavior is needed, it would typically be handled by the calling code using |
| 194 | +// CompletableFuture or other async patterns external to these tests |
96 | 195 | }
|
0 commit comments