How to use assertFileUploaded method of com.github.kittinunf.fuel.core.requests.UploadRequestTest class

Best Fuel code snippet using com.github.kittinunf.fuel.core.requests.UploadRequestTest.assertFileUploaded

UploadRequestTest.kt

Source:UploadRequestTest.kt Github

copy

Full Screen

...22import java.io.FileNotFoundException23import java.net.HttpURLConnection24class UploadRequestTest : MockHttpTestCase() {25 private val currentDir = File(System.getProperty("user.dir"), "src/test/assets")26 private fun assertFileUploaded(file: File, result: ResponseResultOf<MockReflected>, name: String? = file.nameWithoutExtension, fileName: String? = file.name): ResponseResultOf<MockReflected> {27 val (request, response, wrapped) = result28 val (data, error) = wrapped29 assertThat("Expected request not to be null", request, notNullValue())30 assertThat("Expected response not to be null", response, notNullValue())31 assertThat("Expected data, actual error $error", data, notNullValue())32 val statusCode = HttpURLConnection.HTTP_OK33 assertThat(response.statusCode, equalTo(statusCode))34 // Assert the content-type35 assertThat(request[Headers.CONTENT_TYPE].lastOrNull(), startsWith("multipart/form-data"))36 assertThat(request[Headers.CONTENT_TYPE].lastOrNull(), containsString("boundary="))37 val body = data!!.body!!.string!!38 assertBodyFormat(body, request[Headers.CONTENT_TYPE].last().split("boundary=", limit = 2).last().trim('"'))39 val expectedContents = file.readText()40 assertThat(body, containsString(expectedContents))41 val contentDispositions = body.lines().filter { it.startsWith(Headers.CONTENT_DISPOSITION, true) }42 val contentDispositionParameters = contentDispositions.flatMap { it.split(";").map { it.trim() } }43 if (name != null) {44 val foundNames = contentDispositionParameters.filter { it.startsWith("name=") }45 .map { it.substringAfter("name=") }46 .map { it.trim('"') }47 assertThat("Expected $name to be the name, actual $foundNames", foundNames.contains(name), equalTo(true))48 }49 if (fileName != null) {50 val foundFileNames = contentDispositionParameters.filter { it.startsWith("filename=") }51 .map { it.substringAfter("filename=") }52 .map { it.trim('"') }53 assertThat("Expected $fileName to be the filename, actual $foundFileNames", foundFileNames.contains(fileName), equalTo(true))54 }55 return result56 }57 private fun assertBodyFormat(body: String, boundary: String) {58 val parts = body.split("--$boundary\r\n").toMutableList()59 if (parts.isEmpty()) {60 return61 }62 assertThat("Expected there to be at least one part, given there is at least one boundary", parts.size > 1, equalTo(true))63 assertThat("Expected body to start with boundary", parts.removeAt(0).isBlank(), equalTo(true))64 assertThat("Expected body to end with boundary EOF", parts.last(), endsWith("\r\n--$boundary--\r\n"))65 parts.forEach { part ->66 val lines = part.split("\r\n").toMutableList()67 val expected = mutableMapOf(Headers.CONTENT_DISPOSITION to false, Headers.CONTENT_TYPE to false, "\r\n" to false)68 val found = mutableMapOf(Headers.CONTENT_DISPOSITION to "", Headers.CONTENT_TYPE to "", "\r\n" to "")69 while (expected["\r\n"] == false && lines.isNotEmpty()) {70 val line = lines.removeAt(0)71 val match = expected.keys.find { line.startsWith(it, false) || (it == "\r\n" && line.isEmpty()) }72 if (match == null) {73 println("Nothing found for $line")74 } else {75 if (expected[match] == true) {76 fail("Expected $match to be present at most once")77 }78 expected[match] = true79 found[match] = line80 }81 }82 if (expected[Headers.CONTENT_DISPOSITION] == false) {83 fail("Expected ${Headers.CONTENT_DISPOSITION} to be present")84 }85 val contentDisposition = found[Headers.CONTENT_DISPOSITION]!!86 assertThat("Only form-data is allowed as content-disposition", contentDisposition, containsString(": form-data"))87 assertThat("Name parameter is required", contentDisposition, containsString("name="))88 }89 }90 @Test91 fun uploadFileAsDataPart() {92 val manager = FuelManager()93 mock.chain(94 request = mock.request().withMethod(Method.POST.value).withPath("/upload"),95 response = mock.reflect()96 )97 val file = File(currentDir, "lorem_ipsum_short.tmp")98 val triple = manager.upload(mock.path("upload"))99 .add(FileDataPart(file))100 .responseObject(MockReflected.Deserializer())101 assertFileUploaded(file, triple)102 }103 @Test104 fun uploadFileAndParameters() {105 val manager = FuelManager()106 mock.chain(107 request = mock.request().withMethod(Method.POST.value).withPath("/upload"),108 response = mock.reflect()109 )110 val file = File(currentDir, "lorem_ipsum_short.tmp")111 val triple = manager.upload(mock.path("upload"), parameters = listOf("foo" to "bar"))112 .add(FileDataPart(file, name = "file"))113 .responseObject(MockReflected.Deserializer())114 val (request, _, result) = assertFileUploaded(file, triple, name = "file")115 assertThat(request.url.toExternalForm(), not(containsString("foo")))116 val (data, _) = result117 assertThat(data!!.body!!.string, containsString("name=\"foo\""))118 assertThat(data.body!!.string, containsString("bar"))119 }120 @Test121 fun uploadFileUsingPut() {122 val manager = FuelManager()123 mock.chain(124 request = mock.request().withMethod(Method.PUT.value).withPath("/upload"),125 response = mock.reflect()126 )127 val file = File(currentDir, "lorem_ipsum_long.tmp")128 val triple = manager.upload(mock.path("upload"), Method.PUT)129 .add(FileDataPart(file))130 .responseObject(MockReflected.Deserializer())131 assertFileUploaded(file, triple)132 }133 @Test134 fun uploadFileUsingProgress() {135 val manager = FuelManager()136 mock.chain(137 request = mock.request().withMethod(Method.POST.value).withPath("/upload"),138 response = mock.reflect()139 )140 var read = -1L141 var total = -1L142 val file = File(currentDir, "lorem_ipsum_long.tmp")143 val triple = manager.upload(mock.path("upload"))144 .add(FileDataPart(file))145 .progress { readBytes, totalBytes -> read = readBytes; total = totalBytes }146 .responseObject(MockReflected.Deserializer())147 assertFileUploaded(file, triple)148 assertThat("Expected upload progress", read == total && read != -1L && total != -1L, equalTo(true))149 }150 @Test151 fun uploadToInvalidEndpoint() {152 val manager = FuelManager()153 mock.chain(154 request = mock.request().withMethod(Method.POST.value).withPath("/nope"),155 response = mock.response().withStatusCode(HttpURLConnection.HTTP_NOT_FOUND)156 )157 val (request, response, result) = manager.upload(mock.path("nope"))158 .add(FileDataPart(File(currentDir, "lorem_ipsum_short.tmp")))159 .responseString()160 val (data, error) = result161 assertThat("Expected request not to be null", request, notNullValue())162 assertThat("Expected response not to be null", response, notNullValue())163 assertThat("Expected error, actual data $data", error, notNullValue())164 val statusCode = HttpURLConnection.HTTP_NOT_FOUND165 assertThat(response.statusCode, equalTo(statusCode))166 }167 @Test168 fun uploadNonExistingFile() {169 val manager = FuelManager()170 mock.chain(171 request = mock.request().withMethod(Method.POST.value).withPath("/upload"),172 response = mock.reflect()173 )174 val (request, response, result) = manager.upload(mock.path("upload"))175 .add { FileDataPart(File(currentDir, "not_found_file.tmp")) }176 .responseString()177 val (data, error) = result178 assertThat("Expected request not to be null", request, notNullValue())179 assertThat("Expected response not to be null", response, notNullValue())180 assertThat("Expected error, actual data $data", error, notNullValue())181 assertThat(error?.exception as FileNotFoundException, isA(FileNotFoundException::class.java))182 val statusCode = -1183 assertThat(response.statusCode, equalTo(statusCode))184 }185 @Test186 fun uploadMultipleFilesUnderSameField() {187 val manager = FuelManager()188 mock.chain(189 request = mock.request().withMethod(Method.POST.value).withPath("/upload"),190 response = mock.reflect()191 )192 val shortFile = File(currentDir, "lorem_ipsum_short.tmp")193 val longFile = File(currentDir, "lorem_ipsum_long.tmp")194 val triple = manager.upload(mock.path("upload"), parameters = listOf("foo" to "bar"))195 .add(196 FileDataPart(shortFile, name = "file"),197 FileDataPart(longFile, name = "file")198 )199 .responseObject(MockReflected.Deserializer())200 assertFileUploaded(shortFile, triple, name = "file")201 assertFileUploaded(longFile, triple, name = "file")202 assertThat(triple.third.component1()!!.body!!.string, not(containsString("multipart/mixed")))203 }204 @Test205 fun uploadMultipleFilesUnderSameFieldArray() {206 val manager = FuelManager()207 mock.chain(208 request = mock.request().withMethod(Method.POST.value).withPath("/upload"),209 response = mock.reflect()210 )211 val shortFile = File(currentDir, "lorem_ipsum_short.tmp")212 val longFile = File(currentDir, "lorem_ipsum_long.tmp")213 val triple = manager.upload(mock.path("upload"), parameters = listOf("foo" to "bar"))214 .add(215 FileDataPart(shortFile, name = "file[]"),216 FileDataPart(longFile, name = "file[]")217 )218 .responseObject(MockReflected.Deserializer())219 assertFileUploaded(shortFile, triple, name = "file[]")220 assertFileUploaded(longFile, triple, name = "file[]")221 assertThat(triple.third.component1()!!.body!!.string, not(containsString("multipart/mixed")))222 }223 @Test224 fun uploadMultipleFilesAsMultipleFields() {225 val manager = FuelManager()226 mock.chain(227 request = mock.request().withMethod(Method.POST.value).withPath("/upload"),228 response = mock.reflect()229 )230 val shortFile = File(currentDir, "lorem_ipsum_short.tmp")231 val longFile = File(currentDir, "lorem_ipsum_long.tmp")232 val triple = manager.upload(mock.path("upload"), parameters = listOf("foo" to "bar"))233 .add(FileDataPart(shortFile, contentType = "image/jpeg"))234 .add(FileDataPart(longFile, name = "second-file", contentType = "image/jpeg"))235 .responseObject(MockReflected.Deserializer())236 assertFileUploaded(shortFile, triple)237 assertFileUploaded(longFile, triple, name = "second-file")238 }239 @Test240 fun uploadBlob() {241 val file = File(currentDir, "lorem_ipsum_short.tmp")242 val blob = BlobDataPart(file.inputStream(), contentLength = file.length(), filename = file.name, name = "coolblob")243 val manager = FuelManager()244 mock.chain(245 request = mock.request().withMethod(Method.POST.value).withPath("/upload"),246 response = mock.reflect()247 )248 val triple = manager.upload(mock.path("upload"), parameters = listOf("foo" to "bar"))249 .add { blob }250 .responseObject(MockReflected.Deserializer())251 assertFileUploaded(file, triple, name = "coolblob", fileName = file.name)252 }253 @Test254 fun uploadWithCustomBoundary() {255 val manager = FuelManager()256 mock.chain(257 request = mock.request().withMethod(Method.POST.value).withPath("/upload"),258 response = mock.reflect()259 )260 val boundary = "160f77ec3eff"261 val file = File(currentDir, "lorem_ipsum_short.tmp")262 val triple = manager.upload(mock.path("upload"), parameters = listOf("foo" to "bar"))263 .add(FileDataPart(file))264 .header(Headers.CONTENT_TYPE, "multipart/form-data; boundary=\"$boundary\"")265 .responseObject(MockReflected.Deserializer())266 val (_, _, result) = assertFileUploaded(file, triple)267 val (data, _) = result268 val body = data!!.body!!.string269 assertThat(body, containsString("--$boundary--"))270 }271 @Test272 fun uploadWithInvalidBoundary() {273 val manager = FuelManager()274 mock.chain(275 request = mock.request().withMethod(Method.POST.value).withPath("/upload"),276 response = mock.reflect()277 )278 val (request, response, result) = manager.upload(mock.path("upload"), parameters = listOf("foo" to "bar"))279 .add(FileDataPart(File(currentDir, "lorem_ipsum_short.tmp")))280 .header(Headers.CONTENT_TYPE, "multipart/form-data")281 .responseObject(MockReflected.Deserializer())282 val (data, error) = result283 assertThat("Expected request not to be null", request, notNullValue())284 assertThat("Expected response not to be null", response, notNullValue())285 assertThat("Expected error, actual data $data", error, notNullValue())286 assertThat(error?.exception as? IllegalArgumentException, isA(IllegalArgumentException::class.java))287 }288 @Test289 fun uploadInlineDataPart() {290 val manager = FuelManager()291 mock.chain(292 request = mock.request().withMethod(Method.POST.value).withPath("/upload"),293 response = mock.reflect()294 )295 val shortFile = File(currentDir, "lorem_ipsum_short.tmp")296 val longFile = File(currentDir, "lorem_ipsum_long.tmp")297 val metadata = longFile.readText()298 val triple = manager.upload(mock.path("upload"), parameters = listOf("foo" to "bar"))299 .add(300 FileDataPart(shortFile, name = "file"),301 InlineDataPart(metadata, name = "metadata", contentType = "application/json", filename = "metadata.json")302 )303 .responseObject(MockReflected.Deserializer())304 assertFileUploaded(shortFile, triple, name = "file")305 assertFileUploaded(longFile, triple, name = "metadata", fileName = "metadata.json")306 assertThat(triple.third.component1()!!.body!!.string, not(containsString("multipart/mixed")))307 }308}...

Full Screen

Full Screen

assertFileUploaded

Using AI Code Generation

copy

Full Screen

1 fun testAssertFileUploaded() {2 val file = File("src/test/resources/lorem.txt")3 val (request, response, result) = Fuel.upload("/post")4 .source { request, url ->5 file.inputStream()6 }7 .responseString()8 request.assertFileUploaded(file)9 }10 fun testAssertFileNotUploaded() {11 val file = File("src/test/resources/lorem.txt")12 val (request, response, result) = Fuel.upload("/post")13 .source { request, url ->14 file.inputStream()15 }16 .responseString()17 request.assertFileNotUploaded()18 }19 fun testAssertFileNotUploaded2() {20 val file = File("src/test/resources/lorem.txt")21 val (request, response, result) = Fuel.upload("/post")22 .source { request, url ->23 file.inputStream()24 }25 .responseString()26 request.assertFileNotUploaded(file)27 }28 fun testAssertFileNotUploaded3() {29 val file = File("src/test/resources/lorem.txt")30 val (request, response, result) = Fuel.upload("/post")31 .source { request, url ->32 file.inputStream()33 }34 .responseString()35 request.assertFileNotUploaded(file, "lorem.txt")36 }37 fun testAssertFileNotUploaded4() {38 val file = File("src/test/resources/lorem.txt")39 val (request, response, result) = Fuel.upload("/post")40 .source { request, url ->41 file.inputStream()42 }43 .responseString()44 request.assertFileNotUploaded(file, "lorem.txt", "lorem2.txt")45 }

Full Screen

Full Screen

assertFileUploaded

Using AI Code Generation

copy

Full Screen

1public void testAssertFileUploaded() {2 val file = File("src/test/resources/lorem_ipsum.txt")3 val request = Fuel.upload("/post")4 request.source { _, _ -> file.inputStream() }5 request.response { _, _, _ -> }6 request.assertFileUploaded(file)7}8public void testAssertFileUploaded() {9 val file = File("src/test/resources/lorem_ipsum.txt")10 val request = Fuel.upload("/post")11 request.source { _, _ -> file.inputStream() }12 request.response { _, _, _ -> }13 request.assertFileUploaded(file)14}15public void testAssertFileUploaded() {16 val file = File("src/test/resources/lorem_ipsum.txt")17 val request = Fuel.upload("/post")18 request.source { _, _ -> file.inputStream() }19 request.response { _, _, _ -> }20 request.assertFileUploaded(file)21}22public void testAssertFileUploaded() {23 val file = File("src/test/resources/lorem_ipsum.txt")24 val request = Fuel.upload("/post")25 request.source { _, _ -> file.inputStream() }26 request.response { _, _, _ -> }27 request.assertFileUploaded(file)28}29public void testAssertFileUploaded() {30 val file = File("src/test/resources/lorem_ipsum.txt")31 val request = Fuel.upload("/post")32 request.source { _, _ -> file.inputStream() }33 request.response { _, _, _ -> }34 request.assertFileUploaded(file)35}36public void testAssertFileUploaded() {37 val file = File("src/test/resources/lorem_ipsum.txt")38 val request = Fuel.upload("/post")39 request.source { _, _ -> file.inputStream() }40 request.response { _, _,

Full Screen

Full Screen

assertFileUploaded

Using AI Code Generation

copy

Full Screen

1public void testUploadFile() throws Exception {2 val file = File.createTempFile("tempfile", ".tmp");3 val os = new FileOutputStream(file);4 os.write("test".getBytes());5 os.close();6 Fuel.upload("/post").source { _, _ -> file }.response { _, _, result ->7 result.fold({ data ->8 val response = String(data)9 assertEquals("test", response)10 }, { err ->11 fail(err.message)12 })13 }14}15public void testUploadFile() throws Exception {16 val file = File.createTempFile("tempfile", ".tmp");17 val os = new FileOutputStream(file);18 os.write("test".getBytes());19 os.close();20 Fuel.upload("/post").source { _, _ -> file }.response { _, _, result ->21 result.fold({ data ->22 val response = String(data)23 assertEquals("test", response)24 }, { err ->25 fail(err.message)26 })27 }28}29public void testUploadFile() throws Exception {30 val file = File.createTempFile("tempfile", ".tmp");31 val os = new FileOutputStream(file);32 os.write("test".getBytes());33 os.close();34 Fuel.upload("/post").source { _, _ -> file }.response { _, _, result ->35 result.fold({ data ->36 val response = String(data)37 assertEquals("test", response)38 }, { err ->39 fail(err.message)40 })41 }42}43public void testUploadFile() throws Exception {44 val file = File.createTempFile("tempfile", ".tmp");45 val os = new FileOutputStream(file);46 os.write("test".getBytes());47 os.close();48 Fuel.upload("/post").source { _, _ -> file }.response { _, _, result ->49 result.fold({ data ->50 val response = String(data)51 assertEquals("test", response)52 }, { err ->53 fail(err.message)54 })55 }56}

Full Screen

Full Screen

assertFileUploaded

Using AI Code Generation

copy

Full Screen

1 fun testAssertFileUploaded() {2 val file = File("src/test/resources/1.txt")3 request.body = file.readBytes()4 }5 request.responseString().third.fold({ success ->6 request.assertFileUploaded(file)7 }, { error ->8 fail(error.message)9 })10 }11 fun testAssertFileNotUploaded() {12 val file = File("src/test/resources/1.txt")13 request.body = file.readBytes()14 }15 request.responseString().third.fold({ success ->16 request.assertFileNotUploaded(File("src/test/resources/2.txt"))17 }, { error ->18 fail(error.message)19 })20 }21 fun testAssertFileNotUploaded() {22 val file = File("src/test/resources/1.txt")23 request.body = file.readBytes()24 }25 request.responseString().third.fold({ success ->26 request.assertFileNotUploaded(File("src/test/resources/2.txt"))27 }, { error ->28 fail(error.message)29 })30 }31 fun testAssertFileNotUploaded() {32 val file = File("src/test/resources/1.txt")33 request.body = file.readBytes()34 }35 request.responseString().third.fold({ success ->36 request.assertFileNotUploaded(File("src/test/resources/2.txt"))37 }, { error ->38 fail(error.message)39 })40 }41 fun testAssertFileNotUploaded() {42 val file = File("src/test

Full Screen

Full Screen

assertFileUploaded

Using AI Code Generation

copy

Full Screen

1val (request, response, result) = FuelManager.instance.upload("/").file("file", file).responseString()2assertFileUploaded(request, file)3public void testUpload() throws Exception {4 File file = new File("testfile.txt");5 file.createNewFile();6 file.deleteOnExit();7 try {8 String response = upload(uploadUrl, file);9 System.out.println(response);10 } catch (Exception e) {11 e.printStackTrace();12 }13}14public String upload(String uploadUrl, File file) throws Exception {15 String response = null;16 try {17 response = Unirest.post(uploadUrl)18 .field("file", file)19 .asString().getBody();20 } catch (UnirestException e) {21 e.printStackTrace();22 }23 return response;24}25public void testUpload() throws Exception {26 File file = new File("testfile.txt");27 file.createNewFile();28 file.deleteOnExit();29 try {30 String response = upload(uploadUrl, file);31 System.out.println(response);32 assertThat(response, containsString("testfile.txt"));33 } catch (Exception e) {34 e.printStackTrace();35 }36}37public void testUpload() throws Exception {38 File file = new File("testfile.txt");39 file.createNewFile();40 file.deleteOnExit();41 try {42 String response = upload(uploadUrl, file);43 System.out.println(response);44 assertThat(response, containsString("testfile.txt"));45 } catch (Exception e) {46 e.printStackTrace();47 }48}49public void testUpload() throws Exception {50 File file = new File("testfile.txt");51 file.createNewFile();52 file.deleteOnExit();53 try {54 String response = upload(uploadUrl, file);55 System.out.println(response);56 assertThat(response, containsString("testfile.txt"));57 }

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful