|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.ai.openai.implementation; |
| 5 | + |
| 6 | +import com.azure.core.http.rest.RequestOptions; |
| 7 | +import com.azure.core.util.BinaryData; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.*; |
| 14 | + |
| 15 | +/** |
| 16 | + * Unit tests for {@link MultipartFormDataHelper} |
| 17 | + */ |
| 18 | +public class MultipartFormDataHelperTest { |
| 19 | + @Test |
| 20 | + public void testSerializeJsonField() { |
| 21 | + // Arrange |
| 22 | + RequestOptions multipartRequestOptions = new RequestOptions(); |
| 23 | + |
| 24 | + // Act |
| 25 | + MultipartFormDataHelper multipartRequest |
| 26 | + = new MultipartFormDataHelper(multipartRequestOptions).serializeJsonField("weather", "sunny") |
| 27 | + .serializeJsonField("temperatureCelsius", 24); |
| 28 | + |
| 29 | + BinaryData requestBody = multipartRequest.end().getRequestBody(); |
| 30 | + |
| 31 | + // Assert |
| 32 | + assertTrue(requestBody.toString().contains("Content-Disposition: form-data; name=\"weather\"")); |
| 33 | + assertTrue(requestBody.toString().contains("Content-Disposition: form-data; name=\"temperatureCelsius\"")); |
| 34 | + assertTrue(requestBody.toString().contains("Content-Type: application/json")); |
| 35 | + assertTrue(requestBody.toString().contains("sunny")); |
| 36 | + assertTrue(requestBody.toString().contains("24")); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testSerializeJsonFieldNull() { |
| 41 | + // Arrange |
| 42 | + RequestOptions multipartRequestOptions = new RequestOptions(); |
| 43 | + |
| 44 | + // Act |
| 45 | + MultipartFormDataHelper multipartRequest |
| 46 | + = new MultipartFormDataHelper(multipartRequestOptions).serializeJsonField("weather", null); |
| 47 | + |
| 48 | + BinaryData requestBody = multipartRequest.end().getRequestBody(); |
| 49 | + |
| 50 | + // Assert |
| 51 | + assertFalse(requestBody.toString().contains("Content-Disposition: form-data; name=\"weather\"")); |
| 52 | + assertFalse(requestBody.toString().contains("Content-Type: application/json")); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testSerializeFileFields() { |
| 57 | + // Arrange |
| 58 | + RequestOptions multipartRequestOptions = new RequestOptions(); |
| 59 | + MultipartFormDataHelper multipartRequestOne |
| 60 | + = new MultipartFormDataHelper(multipartRequestOptions).serializeJsonField("weather", "sunny"); |
| 61 | + MultipartFormDataHelper multipartRequestTwo |
| 62 | + = new MultipartFormDataHelper(multipartRequestOptions).serializeJsonField("weather", "rainy"); |
| 63 | + MultipartFormDataHelper multipartRequestThree |
| 64 | + = new MultipartFormDataHelper(multipartRequestOptions).serializeJsonField("weather", "cloudy"); |
| 65 | + BinaryData binaryDataOne = multipartRequestOne.end().getRequestBody(); |
| 66 | + BinaryData binaryDataTwo = multipartRequestTwo.end().getRequestBody(); |
| 67 | + BinaryData binaryDataThree = multipartRequestThree.end().getRequestBody(); |
| 68 | + |
| 69 | + List<BinaryData> files = Arrays.asList(binaryDataOne, binaryDataTwo, binaryDataThree); |
| 70 | + |
| 71 | + List<String> contentTypes = Arrays.asList(null, "", "application/mocktype"); |
| 72 | + |
| 73 | + List<String> filenames = Arrays.asList("Sunny", "Rainy", "Cloudy"); |
| 74 | + |
| 75 | + // Act |
| 76 | + MultipartFormDataHelper multipartFormDataHelper = new MultipartFormDataHelper(multipartRequestOptions) |
| 77 | + .serializeFileFields("weather", files, contentTypes, filenames); |
| 78 | + |
| 79 | + BinaryData requestBody = multipartFormDataHelper.end().getRequestBody(); |
| 80 | + |
| 81 | + // Assert |
| 82 | + assertTrue(requestBody.toString().contains("name=\"weather\"; filename=\"Sunny\"")); |
| 83 | + assertTrue(requestBody.toString().contains("name=\"weather\"; filename=\"Rainy\"")); |
| 84 | + assertTrue(requestBody.toString().contains("name=\"weather\"; filename=\"Cloudy\"")); |
| 85 | + assertTrue(requestBody.toString().contains("application/octet-stream")); |
| 86 | + assertTrue(requestBody.toString().contains("application/mocktype")); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testSerializeFileFieldsNullFile() { |
| 91 | + // Arrange |
| 92 | + RequestOptions multipartRequestOptions = new RequestOptions(); |
| 93 | + |
| 94 | + List<String> contentTypes = Arrays.asList(null, "", "application/mocktype"); |
| 95 | + |
| 96 | + List<String> filenames = Arrays.asList("Sunny", "Rainy", "Cloudy"); |
| 97 | + |
| 98 | + // Act |
| 99 | + MultipartFormDataHelper multipartFormDataHelper = new MultipartFormDataHelper(multipartRequestOptions) |
| 100 | + .serializeFileFields("weather", null, contentTypes, filenames); |
| 101 | + |
| 102 | + BinaryData requestBody = multipartFormDataHelper.end().getRequestBody(); |
| 103 | + |
| 104 | + // Assert |
| 105 | + assertFalse(requestBody.toString().contains("name=\"weather\"; filename=\"Sunny\"")); |
| 106 | + assertFalse(requestBody.toString().contains("name=\"weather\"; filename=\"Rainy\"")); |
| 107 | + assertFalse(requestBody.toString().contains("name=\"weather\"; filename=\"Cloudy\"")); |
| 108 | + assertFalse(requestBody.toString().contains("application/octet-stream")); |
| 109 | + assertFalse(requestBody.toString().contains("application/mocktype")); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testSerializeFileFieldNullContentType() { |
| 114 | + // Arrange |
| 115 | + RequestOptions multipartRequestOptions = new RequestOptions(); |
| 116 | + MultipartFormDataHelper multipartRequestSunny |
| 117 | + = new MultipartFormDataHelper(multipartRequestOptions).serializeJsonField("weather", "sunny"); |
| 118 | + BinaryData file = multipartRequestSunny.end().getRequestBody(); |
| 119 | + |
| 120 | + // Act |
| 121 | + MultipartFormDataHelper multipartFormDataHelper |
| 122 | + = new MultipartFormDataHelper(multipartRequestOptions).serializeFileField("weather", file, null, "Sunny"); |
| 123 | + |
| 124 | + BinaryData requestBody = multipartFormDataHelper.end().getRequestBody(); |
| 125 | + |
| 126 | + // Assert |
| 127 | + assertTrue(requestBody.toString().contains("name=\"weather\"; filename=\"Sunny\"")); |
| 128 | + assertTrue(requestBody.toString().contains("application/octet-stream")); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testSerializeFileFieldNullFile() { |
| 133 | + // Arrange |
| 134 | + RequestOptions multipartRequestOptions = new RequestOptions(); |
| 135 | + |
| 136 | + // Act |
| 137 | + MultipartFormDataHelper multipartFormDataHelper = new MultipartFormDataHelper(multipartRequestOptions) |
| 138 | + .serializeFileField("weather", null, "application/mocktype", "Sunny"); |
| 139 | + |
| 140 | + BinaryData requestBody = multipartFormDataHelper.end().getRequestBody(); |
| 141 | + |
| 142 | + // Assert |
| 143 | + assertFalse(requestBody.toString().contains("name=\"weather\"; filename=\"Sunny\"")); |
| 144 | + assertFalse(requestBody.toString().contains("application/mocktype")); |
| 145 | + } |
| 146 | +} |
0 commit comments