How to use DefaultRequest class of com.github.kittinunf.fuel.core.requests package

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

ParameterEncoderTest.kt

Source:ParameterEncoderTest.kt Github

copy

Full Screen

1package com.github.kittinunf.fuel.core.interceptors2import com.github.kittinunf.fuel.core.BlobDataPart3import com.github.kittinunf.fuel.core.Headers4import com.github.kittinunf.fuel.core.Method5import com.github.kittinunf.fuel.core.requests.DefaultRequest6import com.github.kittinunf.fuel.core.requests.upload7import org.hamcrest.CoreMatchers.equalTo8import org.hamcrest.CoreMatchers.not9import org.hamcrest.MatcherAssert.assertThat10import org.hamcrest.core.StringContains.containsString11import org.junit.Test12import java.io.ByteArrayInputStream13import java.net.URL14class ParameterEncoderTest {15 @Test16 fun encodeRequestParametersInUrlWhenBodyDisallowed() {17 val methods = listOf(Method.GET, Method.DELETE, Method.HEAD, Method.OPTIONS, Method.TRACE)18 methods.forEach { method ->19 val testRequest = DefaultRequest(20 method,21 URL("https://test.fuel.com"),22 parameters = listOf("foo" to "bar")23 )24 var executed = false25 ParameterEncoder { request ->26 assertThat(request.url.toExternalForm(), containsString("?"))27 assertThat(request.url.query, containsString("foo=bar"))28 assertThat("Expected parameters to be cleared", request.parameters.isEmpty(), equalTo(true))29 executed = true30 request31 }(testRequest)32 assertThat("Expected encoder \"next\" to be called", executed, equalTo(true))33 }34 }35 @Test36 fun encodeAppendsRequestParametersInUrl() {37 val methods = listOf(Method.GET, Method.DELETE, Method.HEAD, Method.OPTIONS, Method.TRACE)38 methods.forEach { method ->39 val testRequest = DefaultRequest(40 method,41 URL("https://test.fuel.com?a=b"),42 parameters = listOf("foo" to "bar")43 )44 var executed = false45 ParameterEncoder { request ->46 assertThat(request.url.toExternalForm(), containsString("?"))47 assertThat(request.url.query, containsString("&"))48 assertThat(request.url.query, containsString("a=b"))49 assertThat(request.url.query, containsString("foo=bar"))50 assertThat("Expected parameters to be cleared", request.parameters.isEmpty(), equalTo(true))51 executed = true52 request53 }(testRequest)54 assertThat("Expected encoder \"next\" to be called", executed, equalTo(true))55 }56 }57 @Test58 fun encodeRequestParametersInBodyWhenBodyAllowed() {59 val methods = listOf(Method.POST, Method.PATCH, Method.PUT)60 methods.forEach { method ->61 val testRequest = DefaultRequest(62 method,63 URL("https://test.fuel.com"),64 parameters = listOf("foo" to "bar")65 )66 var executed = false67 ParameterEncoder { request ->68 assertThat(request.url.toExternalForm(), not(containsString("?")))69 assertThat(request.url.query, not(containsString("foo=bar")))70 val contentType = request[Headers.CONTENT_TYPE].lastOrNull()?.split(';')?.first()71 assertThat(contentType, equalTo("application/x-www-form-urlencoded"))72 assertThat(request.body.asString(contentType), equalTo("foo=bar"))73 assertThat("Expected parameters to be cleared", request.parameters.isEmpty(), equalTo(true))74 executed = true75 request76 }(testRequest)77 assertThat("Expected encoder \"next\" to be called", executed, equalTo(true))78 }79 }80 @Test81 fun encodeRequestParametersInUrlWhenBodyExists() {82 val methods = listOf(Method.POST, Method.PATCH, Method.PUT)83 methods.forEach { method ->84 val testRequest = DefaultRequest(85 method,86 URL("https://test.fuel.com"),87 parameters = listOf("foo" to "bar")88 ).body("my current body")89 var executed = false90 ParameterEncoder { request ->91 assertThat(request.url.toExternalForm(), containsString("?"))92 assertThat(request.url.query, containsString("foo=bar"))93 val contentType = request[Headers.CONTENT_TYPE].lastOrNull()?.split(';')?.first()94 assertThat(contentType, equalTo("text/plain"))95 assertThat(request.body.asString(contentType), equalTo("my current body"))96 assertThat("Expected parameters to be cleared", request.parameters.isEmpty(), equalTo(true))97 executed = true98 request99 }(testRequest)100 assertThat("Expected encoder \"next\" to be called", executed, equalTo(true))101 }102 }103 @Test104 fun ignoreParameterEncodingForMultipartFormDataRequests() {105 val methods = listOf(Method.POST, Method.PATCH, Method.PUT)106 methods.forEach { method ->107 val testRequest = DefaultRequest(108 method,109 URL("https://test.fuel.com"),110 parameters = listOf("foo" to "bar")111 ).header(Headers.CONTENT_TYPE, "multipart/form-data")112 .upload()113 .add { BlobDataPart(ByteArrayInputStream("12345678".toByteArray()), name = "test", contentLength = 8L) }114 var executed = false115 ParameterEncoder { request ->116 assertThat(request.url.toExternalForm(), not(containsString("?")))117 assertThat(request.url.query, not(containsString("foo=bar")))118 val contentType = request[Headers.CONTENT_TYPE].lastOrNull()?.split(';')?.first()119 assertThat(contentType, equalTo("multipart/form-data"))120 val body = String(request.body.toByteArray())121 assertThat(body, containsString("foo"))122 assertThat(body, containsString("bar"))123 assertThat(body, containsString("test"))124 assertThat(body, containsString("12345678"))125 assertThat("Expected parameters not to be cleared", request.parameters.isEmpty(), equalTo(false))126 executed = true127 request128 }(testRequest)129 assertThat("Expected encoder \"next\" to be called", executed, equalTo(true))130 }131 }132 @Test133 fun encodeMultipleParameters() {134 val methods = listOf(Method.GET, Method.DELETE, Method.HEAD, Method.OPTIONS, Method.TRACE)135 methods.forEach { method ->136 val testRequest = DefaultRequest(137 method,138 URL("https://test.fuel.com"),139 parameters = listOf("foo" to "bar", "baz" to "q")140 )141 var executed = false142 ParameterEncoder { request ->143 assertThat(request.url.toExternalForm(), containsString("?"))144 assertThat(request.url.query, containsString("foo=bar"))145 assertThat(request.url.query, containsString("baz=q"))146 assertThat("Expected parameters to be cleared", request.parameters.isEmpty(), equalTo(true))147 executed = true148 request149 }(testRequest)150 assertThat("Expected encoder \"next\" to be called", executed, equalTo(true))151 }152 }153 @Test154 fun encodeParameterWithoutValue() {155 val methods = listOf(Method.GET, Method.DELETE, Method.HEAD, Method.OPTIONS, Method.TRACE)156 methods.forEach { method ->157 val testRequest = DefaultRequest(158 method,159 URL("https://test.fuel.com"),160 parameters = listOf("foo" to "bar", "baz" to null, "q" to "")161 )162 var executed = false163 ParameterEncoder { request ->164 assertThat(request.url.toExternalForm(), containsString("?"))165 assertThat(request.url.query, containsString("foo=bar"))166 assertThat(request.url.query, not(containsString("baz")))167 assertThat(request.url.query, containsString("q"))168 assertThat("Expected parameters to be cleared", request.parameters.isEmpty(), equalTo(true))169 executed = true170 request171 }(testRequest)172 assertThat("Expected encoder \"next\" to be called", executed, equalTo(true))173 }174 }175 @Test176 fun encodeParametersWithListValues() {177 val methods = listOf(Method.GET, Method.DELETE, Method.HEAD, Method.OPTIONS, Method.TRACE)178 methods.forEach { method ->179 val testRequest = DefaultRequest(180 method,181 URL("https://test.fuel.com"),182 parameters = listOf("foo" to "bar", "baz" to listOf("x", "y", "z"))183 )184 var executed = false185 ParameterEncoder { request ->186 assertThat(request.url.toExternalForm(), containsString("?"))187 assertThat(request.url.query, containsString("foo=bar"))188 assertThat(request.url.query, containsString("baz[]=x"))189 assertThat(request.url.query, containsString("baz[]=y"))190 assertThat(request.url.query, containsString("baz[]=z"))191 assertThat("Expected parameters to be cleared", request.parameters.isEmpty(), equalTo(true))192 executed = true193 request194 }(testRequest)195 assertThat("Expected encoder \"next\" to be called", executed, equalTo(true))196 }197 }198 @Test199 fun encodeParametersWithArrayValues() {200 val methods = listOf(Method.GET, Method.DELETE, Method.HEAD, Method.OPTIONS, Method.TRACE)201 methods.forEach { method ->202 val testRequest = DefaultRequest(203 method,204 URL("https://test.fuel.com"),205 parameters = listOf("foo" to "bar", "baz" to arrayOf("x", "y", "z"))206 )207 var executed = false208 ParameterEncoder { request ->209 assertThat(request.url.toExternalForm(), containsString("?"))210 assertThat(request.url.query, containsString("foo=bar"))211 assertThat(request.url.query, containsString("baz[]=x"))212 assertThat(request.url.query, containsString("baz[]=y"))213 assertThat(request.url.query, containsString("baz[]=z"))214 assertThat("Expected parameters to be cleared", request.parameters.isEmpty(), equalTo(true))215 executed = true216 request...

Full Screen

Full Screen

RequestHeadersTest.kt

Source:RequestHeadersTest.kt Github

copy

Full Screen

1package com.github.kittinunf.fuel2import com.github.kittinunf.fuel.core.requests.DefaultRequest3import com.github.kittinunf.fuel.core.Headers4import com.github.kittinunf.fuel.core.Method5import org.hamcrest.CoreMatchers.equalTo6import org.hamcrest.MatcherAssert.assertThat7import org.junit.Assert.fail8import org.junit.Test9import java.net.URL10class RequestHeadersTest {11 @Test12 fun requestHeadersFromReadmeIntegrity() {13 val request = DefaultRequest(method = Method.POST, url = URL("https://test.fuel.com/my-post-path"))14 .header(Headers.ACCEPT, "text/html, */*; q=0.1")15 .header(Headers.CONTENT_TYPE, "image/png")16 .header(Headers.COOKIE to "basic=very")17 .appendHeader(Headers.COOKIE to "value_1=foo", Headers.COOKIE to "value_2=bar", Headers.ACCEPT to "application/json")18 .appendHeader("MyFoo" to "bar", "MyFoo" to "baz")19 val recordedSets = mutableMapOf<String, String>()20 request.headers.transformIterate(21 { key: String, value: String -> recordedSets.put(key, value) },22 { k, v -> fail("Expected only header `set` and `add` with $k and $v") }23 )24 assertThat(recordedSets[Headers.ACCEPT], equalTo("text/html, */*; q=0.1, application/json"))25 assertThat(recordedSets[Headers.CONTENT_TYPE], equalTo("image/png"))26 assertThat(recordedSets[Headers.COOKIE], equalTo("basic=very; value_1=foo; value_2=bar"))27 assertThat(recordedSets["MyFoo"], equalTo("bar, baz"))28 }29 @Test30 fun requestHasHeaderGetter() {31 val request = DefaultRequest(method = Method.GET, url = URL("https://test.fuel.com/"))32 .header(Headers.CONTENT_TYPE, "text/html")33 val values = request[Headers.CONTENT_TYPE]34 assertThat(values, equalTo(request.header(Headers.CONTENT_TYPE)))35 assertThat(values.lastOrNull(), equalTo("text/html"))36 }37 @Test38 fun requestHasHeaderSetter() {39 val request = DefaultRequest(method = Method.GET, url = URL("https://test.fuel.com/"))40 request[Headers.CONTENT_TYPE] = "text/html"41 val values = request.header(Headers.CONTENT_TYPE)42 assertThat(values.lastOrNull(), equalTo("text/html"))43 assertThat(values.size, equalTo(1))44 }45 @Test46 fun setHeaderSetterSingleReplacesOldValue() {47 val request = DefaultRequest(method = Method.GET, url = URL("https://test.fuel.com/"))48 .header(Headers.CONTENT_TYPE, "text/html")49 request[Headers.CONTENT_TYPE] = "text/plain"50 val values = request.header(Headers.CONTENT_TYPE)51 assertThat(values.lastOrNull(), equalTo("text/plain"))52 assertThat(values.size, equalTo(1))53 }54 @Test55 fun setHeaderFunctionSingleReplacesOldValue() {56 val request = DefaultRequest(method = Method.GET, url = URL("https://test.fuel.com/"))57 .header(Headers.CONTENT_TYPE, "text/html")58 request.header(Headers.CONTENT_TYPE, "application/json")59 val values = request.header(Headers.CONTENT_TYPE)60 assertThat(values.lastOrNull(), equalTo("application/json"))61 assertThat(values.size, equalTo(1))62 }63 @Test64 fun setHeaderSetterMultipleReplacesOldValue() {65 val request = DefaultRequest(method = Method.GET, url = URL("https://test.fuel.com/"))66 .header(Headers.ACCEPT, "text/html")67 request[Headers.ACCEPT] = listOf("text/plain", "*/*; q=0.2")68 val values = request.header(Headers.ACCEPT)69 assertThat(values.firstOrNull(), equalTo("text/plain"))70 assertThat(values.lastOrNull(), equalTo("*/*; q=0.2"))71 assertThat(values.size, equalTo(2))72 }73 @Test74 fun setHeaderFunctionMultipleReplacesOldValue() {75 val request = DefaultRequest(method = Method.GET, url = URL("https://test.fuel.com/"))76 .header(Headers.ACCEPT, "text/html")77 request.header(Headers.ACCEPT, listOf("text/plain", "*/*; q=0.2"))78 val values = request.header(Headers.ACCEPT)79 assertThat(values.firstOrNull(), equalTo("text/plain"))80 assertThat(values.lastOrNull(), equalTo("*/*; q=0.2"))81 assertThat(values.size, equalTo(2))82 }83 @Test84 fun setHeaderFunctionMapReplacesOldValue() {85 val request = DefaultRequest(method = Method.GET, url = URL("https://test.fuel.com/"))86 .header(Headers.ACCEPT, "text/html")87 request.header(mapOf(Headers.ACCEPT to listOf("text/plain", "*/*; q=0.2")))88 val values = request.header(Headers.ACCEPT)89 assertThat(values.firstOrNull(), equalTo("text/plain"))90 assertThat(values.lastOrNull(), equalTo("*/*; q=0.2"))91 assertThat(values.size, equalTo(2))92 }93 @Test94 fun setHeaderFunctionPairsReplacesOldValue() {95 val request = DefaultRequest(method = Method.GET, url = URL("https://test.fuel.com/"))96 .header(Headers.ACCEPT, "text/html")97 request.header(Headers.ACCEPT to listOf("text/plain", "*/*; q=0.2"))98 val values = request.header(Headers.ACCEPT)99 assertThat(values.firstOrNull(), equalTo("text/plain"))100 assertThat(values.lastOrNull(), equalTo("*/*; q=0.2"))101 assertThat(values.size, equalTo(2))102 }103 @Test104 fun setHeaderFunctionVarArgsReplacesOldValue() {105 val request = DefaultRequest(method = Method.GET, url = URL("https://test.fuel.com/"))106 .header(Headers.ACCEPT, "text/html")107 request.header(Headers.ACCEPT, "text/plain", "*/*; q=0.2")108 val values = request.header(Headers.ACCEPT)109 assertThat(values.firstOrNull(), equalTo("text/plain"))110 assertThat(values.lastOrNull(), equalTo("*/*; q=0.2"))111 assertThat(values.size, equalTo(2))112 }113 @Test114 fun setHeaderWithPairsPreservesDuplicateKeys() {115 val request = DefaultRequest(method = Method.GET, url = URL("https://test.fuel.com/"))116 .header(Headers.ACCEPT_ENCODING, "identity")117 request.header(Headers.ACCEPT_ENCODING to "br", Headers.ACCEPT_ENCODING to "gzip")118 val values = request.header(Headers.ACCEPT_ENCODING)119 assertThat(values.firstOrNull(), equalTo("br"))120 assertThat(values.lastOrNull(), equalTo("gzip"))121 assertThat(values.size, equalTo(2))122 }123 @Test124 fun setHeaderWithMapTakesLastKey() {125 val request = DefaultRequest(method = Method.GET, url = URL("https://test.fuel.com/"))126 .header(Headers.ACCEPT_ENCODING, "identity")127 request.header(mapOf(Headers.ACCEPT_ENCODING to "br", Headers.ACCEPT_ENCODING to "gzip"))128 val values = request.header(Headers.ACCEPT_ENCODING)129 assertThat(values.firstOrNull(), equalTo("gzip"))130 assertThat(values.size, equalTo(1))131 }132 @Test133 fun appendHeader() {134 val request = DefaultRequest(method = Method.GET, url = URL("https://test.fuel.com/"))135 .header(Headers.ACCEPT_LANGUAGE, "en-US", "nl-NL; q=0.9")136 request.appendHeader(Headers.ACCEPT_LANGUAGE, "fr-FR; q=0.8")137 val values = request.header(Headers.ACCEPT_LANGUAGE)138 assertThat(values.firstOrNull(), equalTo("en-US"))139 assertThat(values.lastOrNull(), equalTo("fr-FR; q=0.8"))140 assertThat(values.size, equalTo(3))141 }142 @Test143 fun appendHeaderWithVarArgs() {144 val request = DefaultRequest(method = Method.GET, url = URL("https://test.fuel.com/"))145 .header(Headers.ACCEPT_LANGUAGE, "en-US")146 request.appendHeader(Headers.ACCEPT_LANGUAGE, "nl-NL; q=0.9", "fr-FR; q=0.8")147 val values = request.header(Headers.ACCEPT_LANGUAGE)148 assertThat(values.firstOrNull(), equalTo("en-US"))149 assertThat(values.lastOrNull(), equalTo("fr-FR; q=0.8"))150 assertThat(values.size, equalTo(3))151 }152 @Test153 fun appendHeaderWithPairs() {154 val request = DefaultRequest(method = Method.GET, url = URL("https://test.fuel.com/"))155 .header(Headers.ACCEPT_LANGUAGE, "en-US")156 request.appendHeader(Headers.ACCEPT_LANGUAGE to "nl-NL; q=0.9", Headers.ACCEPT_LANGUAGE to "fr-FR; q=0.8")157 val values = request.header(Headers.ACCEPT_LANGUAGE)158 assertThat(values.firstOrNull(), equalTo("en-US"))159 assertThat(values.lastOrNull(), equalTo("fr-FR; q=0.8"))160 assertThat(values.size, equalTo(3))161 }162}...

Full Screen

Full Screen

BodyTest.kt

Source:BodyTest.kt Github

copy

Full Screen

1package com.github.kittinunf.fuel.core2import com.github.kittinunf.fuel.core.requests.DefaultBody3import com.github.kittinunf.fuel.core.requests.DefaultRequest4import org.hamcrest.CoreMatchers.containsString5import org.hamcrest.CoreMatchers.equalTo6import org.hamcrest.MatcherAssert.assertThat7import org.junit.Assert.assertArrayEquals8import org.junit.Test9import java.io.ByteArrayInputStream10import java.io.ByteArrayOutputStream11import java.io.File12import java.net.URL13class BodyTest {14 @Test15 fun bodyIsEmptyByDefault() {16 val body = DefaultBody()17 assertThat(body.isEmpty(), equalTo(true))18 assertThat(body.isConsumed(), equalTo(false))19 val request = DefaultRequest(Method.POST, URL("https://test.fuel.com/"))20 assertThat(request.toString(), containsString("(empty)"))21 }22 @Test23 fun bodyIsConsumedAfterWriting() {24 val body = DefaultBody.from({ ByteArrayInputStream("body".toByteArray()) }, { 4 })25 assertThat(body.isConsumed(), equalTo(false))26 body.writeTo(ByteArrayOutputStream())27 assertThat(body.isConsumed(), equalTo(true))28 }29 @Test30 fun bodyFromString() {31 val value = "String Body ${Math.random()}"32 DefaultRequest(Method.POST, URL("https://test.fuel.com/"))33 .body(value)34 .apply {35 val output = ByteArrayOutputStream(value.length)36 assertThat(body.length?.toInt(), equalTo(value.length))37 assertThat(body.toByteArray(), equalTo(value.toByteArray()))38 body.writeTo(output)39 assertThat(output.toString(), equalTo(value))40 }41 }42 @Test43 fun bodyFromByteArray() {44 val value = ByteArray(32).apply {45 for (i in 0..(this.size - 1)) {46 this[i] = ('A'..'z').random().toByte()47 }48 }49 DefaultRequest(Method.POST, URL("https://test.fuel.com/"))50 .body(value)51 .apply {52 val output = ByteArrayOutputStream(value.size)53 assertThat(body.length?.toInt(), equalTo(value.size))54 assertArrayEquals(value, body.toByteArray())55 body.writeTo(output)56 assertArrayEquals(value, output.toByteArray())57 }58 }59 @Test60 fun bodyFromFile() {61 val value = "String Body ${Math.random()}"62 val file = File.createTempFile("BodyTest", ".txt")63 file.writeText(value)64 DefaultRequest(Method.POST, URL("https://test.fuel.com/"))65 .body(file)66 .apply {67 val output = ByteArrayOutputStream(value.length)68 assertThat(body.length?.toInt(), equalTo(value.length))69 assertThat(body.toByteArray(), equalTo(value.toByteArray()))70 body.writeTo(output)71 assertThat(output.toString(), equalTo(value))72 }73 }74 @Test75 fun bodyFromStream() {76 val value = "String Body ${Math.random()}"77 val stream = ByteArrayInputStream(value.toByteArray())78 DefaultRequest(Method.POST, URL("https://test.fuel.com/"))79 .body(stream)80 .apply {81 val output = ByteArrayOutputStream(value.length)82 assertThat(body.toByteArray(), equalTo(value.toByteArray()))83 body.writeTo(output)84 assertThat(output.toString(), equalTo(value))85 }86 }87 @Test(expected = FuelError::class)88 fun bodyFromCallbackCanOnlyBeReadOnce() {89 val body = DefaultBody.from({ ByteArrayInputStream("body".toByteArray()) }, { 4 })90 body.writeTo(ByteArrayOutputStream())91 body.writeTo(ByteArrayOutputStream())92 }93 @Test94 fun bodyToByteArrayLoadsItIntoMemory() {95 val value = "String Body ${Math.random()}"96 val body = DefaultBody.from({ ByteArrayInputStream(value.toByteArray()) }, { value.length.toLong() })97 body.toByteArray()98 val output = ByteArrayOutputStream(value.length)99 body.writeTo(output)100 assertThat(output.toString(), equalTo(value))101 }102 @Test103 fun requestWithBodyIsPrintableAfterConsumption() {104 val value = { ByteArrayInputStream("String Body ${Math.random()}".toByteArray()) }105 DefaultRequest(Method.POST, URL("https://test.fuel.com/"))106 .body(value)107 .apply {108 val output = ByteArrayOutputStream()109 body.writeTo(output)110 assertThat(this.toString(), containsString("(consumed)"))111 }112 }113}...

Full Screen

Full Screen

CategoriesTest.kt

Source:CategoriesTest.kt Github

copy

Full Screen

1package api2import com.github.kittinunf.fuel.Fuel3import com.github.kittinunf.fuel.core.Response4import com.github.kittinunf.fuel.core.ResponseResultOf5import com.github.kittinunf.fuel.core.requests.DefaultRequest6import com.github.kittinunf.fuel.jackson.responseObject7import com.github.kittinunf.result.Result8import io.mockk.every9import io.mockk.junit5.MockKExtension10import io.mockk.mockk11import io.mockk.mockkObject12import org.junit.jupiter.api.Assertions.assertNotNull13import org.junit.jupiter.api.Assertions.assertTrue14import org.junit.jupiter.api.BeforeEach15import org.junit.jupiter.api.Test16import org.junit.jupiter.api.extension.ExtendWith17import placy.api.Categories18import placy.dto.Category19import placy.dto.requests.DefaultRequestDTO20import java.net.URL21@ExtendWith(MockKExtension::class)22class CategoriesTest {23 lateinit var categoriesRequest: DefaultRequestDTO24 lateinit var categoriesArray: Array<Category>25 @Test26 fun `categories could be retrieved`() {27 givenDefaultCategories()28 whenRetrieveCategories()29 thenCategoriesArrayNotNull()30 }31 @Test32 fun `retrieved categories are not empty`() {33 givenDefaultCategories()34 whenRetrieveCategories()35 thenReturnedCategoriesAreCorrect()36 }37 @Test38 fun `categories with russian language could be retrieved`() {39 givenCategoriesWithRussianLanguage()40 whenRetrieveCategories()41 thenReturnedCategoriesAreCorrect()42 }43 @Test44 fun `categories with english language could be retrieved`() {45 givenCategoriesWithEnglishLanguage()46 whenRetrieveCategories()47 thenReturnedCategoriesAreCorrect()48 }49 @Test50 fun `categories with fields name and id could be retrieved`() {51 givenCategoriesWithNameAndIdFields()52 whenRetrieveCategories()53 thenCategoriesArrayNotNull()54 }55 @Test56 fun `categories with fields name and id are retrieved without slug`() {57 givenCategoriesWithNameAndIdFields()58 whenRetrieveCategories()59 thenReturnedCategoriesAreCorrect()60 }61 @Test62 fun `categories with ordering by name asc could be retrieved`() {63 givenCategoriesWithOrderingByNameAsc()64 whenRetrieveCategories()65 thenReturnedCategoriesAreCorrect()66 }67 @Test68 fun `categories could be retrieved with asc ordering by name`() {69 givenCategoriesWithOrderingByNameAsc()70 whenRetrieveCategories()71 thenReturnedCategoriesAreCorrect()72 }73 @Test74 fun `categories could be retrieved with desc ordering by id `() {75 givenCategoriesWithOrderingByIdDesc()76 whenRetrieveCategories()77 thenReturnedCategoriesAreCorrect()78 }79 @BeforeEach80 private fun givenDefaultCategories() {81 categoriesRequest = DefaultRequestDTO()82 }83 private fun givenCategoriesWithOrderingByNameAsc() {84 categoriesRequest.orderBy = "name"85 }86 private fun givenCategoriesWithOrderingByIdDesc() {87 categoriesRequest.orderBy = "-id"88 }89 private fun givenCategoriesWithNameAndIdFields() {90 categoriesRequest.fields = arrayOf("name", "id")91 }92 private fun givenCategoriesWithRussianLanguage() {93 categoriesRequest.language = "ru"94 }95 private fun givenCategoriesWithEnglishLanguage() {...

Full Screen

Full Screen

FormattingTest.kt

Source:FormattingTest.kt Github

copy

Full Screen

1package com.github.kittinunf.fuel.core.extensions2import com.github.kittinunf.fuel.core.requests.DefaultRequest3import com.github.kittinunf.fuel.core.Headers4import com.github.kittinunf.fuel.core.Method5import org.hamcrest.CoreMatchers6import org.hamcrest.MatcherAssert7import org.junit.Test8import java.net.URL9class FormattingTest {10 @Test11 fun httpGetCurlString() {12 val request = DefaultRequest(13 method = Method.GET,14 url = URL("http://httpbin.org/get"),15 headers = Headers.from("Authentication" to "Bearer xxx"),16 parameters = listOf("foo" to "xxx")17 )18 MatcherAssert.assertThat(request.cUrlString(), CoreMatchers.equalTo("curl -i -H \"Authentication:Bearer xxx\" http://httpbin.org/get"))19 }20 @Test21 fun httpPostCurlString() {22 val request = DefaultRequest(23 method = Method.POST,24 url = URL("http://httpbin.org/post"),25 headers = Headers.from("Authentication" to "Bearer xxx"),26 parameters = listOf("foo" to "xxx")27 )28 MatcherAssert.assertThat(request.cUrlString(), CoreMatchers.equalTo("curl -i -X POST -H \"Authentication:Bearer xxx\" http://httpbin.org/post"))29 }30 @Test31 fun httpStringWithOutParams() {32 val request = DefaultRequest(33 Method.GET,34 url = URL("http://httpbin.org/post"),35 headers = Headers.from("Content-Type" to "text/html")36 )37 MatcherAssert.assertThat(request.httpString(), CoreMatchers.startsWith("GET http"))38 MatcherAssert.assertThat(request.httpString(), CoreMatchers.containsString("Content-Type"))39 }40 @Test41 fun httpStringWithParams() {42 val request = DefaultRequest(43 Method.POST,44 url = URL("http://httpbin.org/post"),45 headers = Headers.from("Content-Type" to "text/html"),46 parameters = listOf("foo" to "xxx")47 ).body("it's a body")48 MatcherAssert.assertThat(request.httpString(), CoreMatchers.startsWith("POST http"))49 MatcherAssert.assertThat(request.httpString(), CoreMatchers.containsString("Content-Type"))50 MatcherAssert.assertThat(request.httpString(), CoreMatchers.containsString("body"))51 }52}...

Full Screen

Full Screen

ObjectBodyTest.kt

Source:ObjectBodyTest.kt Github

copy

Full Screen

...4import com.fasterxml.jackson.databind.PropertyNamingStrategy5import com.fasterxml.jackson.module.kotlin.registerKotlinModule6import com.github.kittinunf.fuel.core.Headers7import com.github.kittinunf.fuel.core.Method8import com.github.kittinunf.fuel.core.requests.DefaultRequest9import com.github.kittinunf.fuel.jackson.objectBody10import org.hamcrest.CoreMatchers.equalTo11import org.hamcrest.MatcherAssert.assertThat12import org.junit.Test13import java.net.URL14class ObjectBodyTest {15 @Test16 fun setsBodyCorrectly() {17 val expectedBody = "{\"foo\":42,\"bar\":\"foo bar\",\"fooBar\":\"foo bar\"}"18 val bodyObject = FakeObject()19 val request = DefaultRequest(Method.POST, URL("https://test.fuel.com/body"))20 .objectBody(bodyObject)21 assertThat(expectedBody, equalTo(String(request.body.toByteArray())))22 }23 @Test24 fun setsContentTypeCorrectly() {25 val bodyObject = listOf(26 42,27 mapOf("foo" to "bar")28 )29 val request = DefaultRequest(Method.POST, URL("https://test.fuel.com/body"))30 .objectBody(bodyObject)31 assertThat(request[Headers.CONTENT_TYPE].lastOrNull(), equalTo("application/json"))32 }33 @Test34 fun setsBodyCorrectlyWithCustomMapper() {35 val mapper = createCustomMapper()36 val expectedBody = "{\"foo\":42,\"bar\":\"foo bar\",\"foo_bar\":\"foo bar\"}"37 val bodyObject = FakeObject()38 val request = DefaultRequest(Method.POST, URL("https://test.fuel.com/body"))39 .objectBody(bodyObject, mapper = mapper)40 assertThat(expectedBody, equalTo(String(request.body.toByteArray())))41 }42 private fun createCustomMapper(): ObjectMapper {43 val mapper = ObjectMapper().registerKotlinModule()44 .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)45 mapper.propertyNamingStrategy = PropertyNamingStrategy.SNAKE_CASE46 return mapper47 }48}49data class FakeObject(50 val foo: Int = 42,51 val bar: String = "foo bar",52 val fooBar: String = "foo bar"...

Full Screen

Full Screen

AuthenticationTest.kt

Source:AuthenticationTest.kt Github

copy

Full Screen

1package com.github.kittinunf.fuel.core.extensions2import com.github.kittinunf.fuel.core.requests.DefaultRequest3import com.github.kittinunf.fuel.core.Headers4import com.github.kittinunf.fuel.core.Method5import com.github.kittinunf.fuel.core.Request6import com.github.kittinunf.fuel.util.encodeBase64ToString7import org.hamcrest.CoreMatchers.equalTo8import org.hamcrest.CoreMatchers.isA9import org.hamcrest.MatcherAssert.assertThat10import org.junit.Test11import java.net.URL12class AuthenticationTest {13 @Test14 fun basicAuthentication() {15 val request = DefaultRequest(Method.GET, URL("https://test.fuel.com/authentication"))16 .authentication()17 .basic("username", "password")18 val encodedCredentials = "username:password".encodeBase64ToString()19 assertThat(request[Headers.AUTHORIZATION].lastOrNull(), equalTo("Basic $encodedCredentials"))20 }21 @Test22 fun bearerAuthentication() {23 val request = DefaultRequest(Method.GET, URL("https://test.fuel.com/authentication"))24 .authentication()25 .bearer("token")26 assertThat(request[Headers.AUTHORIZATION].lastOrNull(), equalTo("Bearer token"))27 }28 @Test29 fun authenticationFeatureActsLikeRequest() {30 val request = DefaultRequest(Method.GET, URL("https://test.fuel.com/authentication"))31 val requestWithAuthentication = request.authentication()32 assertThat(requestWithAuthentication, isA(Request::class.java))33 assertThat(requestWithAuthentication.request, equalTo(requestWithAuthentication))34 }35}...

Full Screen

Full Screen

JsonBodyTest.kt

Source:JsonBodyTest.kt Github

copy

Full Screen

1package com.github.kittinunf.fuel.core.extensions2import com.github.kittinunf.fuel.core.requests.DefaultRequest3import com.github.kittinunf.fuel.core.Headers4import com.github.kittinunf.fuel.core.Method5import org.hamcrest.CoreMatchers.equalTo6import org.hamcrest.MatcherAssert.assertThat7import org.junit.Test8import java.net.URL9class JsonBodyTest {10 @Test11 fun setsBodyCorrectly() {12 val body = "[42, { \"foo\": \"bar\" }]"13 val request = DefaultRequest(Method.POST, URL("https://test.fuel.com/body"))14 .jsonBody(body)15 assertThat(body, equalTo(String(request.body.toByteArray())))16 }17 @Test18 fun setsContentTypeCorrectly() {19 val request = DefaultRequest(Method.POST, URL("https://test.fuel.com/body"))20 .jsonBody("[42, { \"foo\": \"bar\" }]")21 assertThat(request[Headers.CONTENT_TYPE].lastOrNull(), equalTo("application/json"))22 }23}...

Full Screen

Full Screen

DefaultRequest

Using AI Code Generation

copy

Full Screen

1val request = DefaultRequest()2request.body = "Hello World".toByteArray()3Fuel.upload(request).responseString { _, _, result ->4println(result)5}6val request = Request()7request.body = "Hello World".toByteArray()8Fuel.upload(request).responseString { _, _, result ->9println(result)10}11.parameters("foo" to "bar")12.headers("Content-Type" to "application/json", "User-Agent" to "Fuel")13.body("Hello World".toByteArray())14.responseString { _, _, result ->15println(result)16}17.parameters("foo" to "bar")18.headers("Content-Type" to "application/json", "User-Agent" to "Fuel")19.body("Hello World".toByteArray())20.responseString { _, _, result ->21println(result)22}23.parameters("foo" to "bar")24.headers("Content-Type" to "application/json", "User-Agent" to "Fuel")25.body("Hello World".toByteArray())26.responseString { _, _, result ->27println(result)28}29.parameters("foo" to "bar")30.headers("Content-Type" to "application/json", "User-Agent" to "Fuel")31.body("Hello World".toByteArray())32.responseString { _, _, result ->33println(result)34}35.parameters("foo" to "bar")36.headers("Content-Type" to "application/json", "User-Agent" to "Fuel")37.body("Hello World".toByteArray())38.responseString { _, _, result ->39println(result)40}41Fuel.upload(path = "https

Full Screen

Full Screen

DefaultRequest

Using AI Code Generation

copy

Full Screen

1val request = DefaultRequest().apply {2}3val response = DefaultHttpClient().executeRequest(request)4println(response.body().asString("application/json"))5println(response.statusCode)6println(response.headers)7 println(request)8 println(response)9 println(result)10}11{"args":{},"headers":{"Accept-Encoding":"gzip","Content-Type":"application/json","Host":"httpbin.org","User-Agent":"Fuel/1.16.0"},"origin":"

Full Screen

Full Screen

DefaultRequest

Using AI Code Generation

copy

Full Screen

1val request = DefaultRequest().apply {2headers = mapOf("Content-Type" to "application/json")3}4val (request, response, result) = FuelManager.instance.request(request).response()5val request = DefaultRequest().apply {6headers = mapOf("Content-Type" to "application/json")7}8val (request, response, result) = FuelManager.instance.request(request).response()9val request = DefaultRequest().apply {10headers = mapOf("Content-Type" to "application/json")11}12val (request, response, result) = FuelManager.instance.request(request).response()13val request = DefaultRequest().apply {14headers = mapOf("Content-Type" to "application/json")15}16val (request, response, result) = FuelManager.instance.request(request).response()17val request = DefaultRequest().apply {18headers = mapOf("Content-Type" to "application/json")19}20val (request, response, result) = FuelManager.instance.request(request).response()21val request = DefaultRequest().apply {22headers = mapOf("Content-Type" to "application/json")23}24val (request, response, result) = Fuel

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.

Run Fuel automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful