How to use createCustomMapper method of com.github.kittinunf.fuel.FuelJacksonTest class

Best Fuel code snippet using com.github.kittinunf.fuel.FuelJacksonTest.createCustomMapper

FuelJacksonTest.kt

Source:FuelJacksonTest.kt Github

copy

Full Screen

...51 .withBody("""{ "user_agent": "test" }""")52 .withStatusCode(HttpURLConnection.HTTP_OK)53 )54 Fuel.get(mock.path("user-agent"))55 .responseObject(jacksonDeserializerOf<HttpBinUserAgentModel>(createCustomMapper())) { _, _, result ->56 assertThat(result, instanceOf(Result.Success::class.java))57 with(result as Result.Success) {58 assertThat(value, instanceOf(HttpBinUserAgentModel::class.java))59 assertThat(value.userAgent, not(""))60 }61 }62 .get()63 }64 @Test65 fun jacksonTestResponseObjectError() {66 mock.chain(67 request = mock.request().withPath("/user-agent"),68 response = mock.response().withStatusCode(HttpURLConnection.HTTP_NOT_FOUND)69 )70 Fuel.get(mock.path("user-agent"))71 .responseObject(jacksonDeserializerOf<HttpBinUserAgentModel>()) { _, _, result ->72 assertThat(result, instanceOf(Result.Failure::class.java))73 with(result as Result.Failure) {74 assertThat(error, notNullValue())75 }76 }77 .get()78 }79 @Test80 fun jacksonTestResponseObjectErrorWithCustomMapper() {81 mock.chain(82 request = mock.request().withPath("/user-agent"),83 response = mock.response().withStatusCode(HttpURLConnection.HTTP_NOT_FOUND)84 )85 Fuel.get(mock.path("user-agent"))86 .responseObject(jacksonDeserializerOf<HttpBinUserAgentModel>(createCustomMapper())) { _, _, result ->87 assertThat(result, instanceOf(Result.Failure::class.java))88 with(result as Result.Failure) {89 assertThat(error, notNullValue())90 }91 }92 .get()93 }94 @Test95 fun jacksonTestResponseDeserializerObject() {96 mock.chain(97 request = mock.request().withPath("/user-agent"),98 response = mock.reflect()99 )100 Fuel.get(mock.path("user-agent"))101 .responseObject<HttpBinUserAgentModel> { _, _, result ->102 assertThat(result, instanceOf(Result.Success::class.java))103 with(result as Result.Success) {104 assertThat(value, instanceOf(HttpBinUserAgentModel::class.java))105 assertThat(value.userAgent, not(""))106 }107 }108 .get()109 }110 @Test111 fun jacksonTestResponseDeserializerObjectWithCustomMapper() {112 mock.chain(113 request = mock.request().withPath("/user-agent"),114 response = mock.response()115 .withBody("""{ "user_agent": "test" }""")116 .withStatusCode(HttpURLConnection.HTTP_OK)117 )118 Fuel.get(mock.path("user-agent"))119 .responseObject<HttpBinUserAgentModel>(createCustomMapper()) { _, _, result ->120 assertThat(result, instanceOf(Result.Success::class.java))121 with(result as Result.Success) {122 assertThat(value, instanceOf(HttpBinUserAgentModel::class.java))123 assertThat(value.userAgent, not(""))124 }125 }126 .get()127 }128 @Test129 fun jacksonTestResponseDeserializerObjectError() {130 mock.chain(131 request = mock.request().withPath("/user-agent"),132 response = mock.response().withStatusCode(HttpURLConnection.HTTP_NOT_FOUND)133 )134 Fuel.get(mock.path("user-agent"))135 .responseObject<HttpBinUserAgentModel> { _, _, result ->136 assertThat(result, instanceOf(Result.Failure::class.java))137 with(result as Result.Failure) {138 assertThat(error, notNullValue())139 }140 }141 .get()142 }143 @Test144 fun jacksonTestResponseHandlerObject() {145 mock.chain(146 request = mock.request().withPath("/user-agent"),147 response = mock.reflect()148 )149 Fuel.get(mock.path("user-agent"))150 .responseObject(object : ResponseHandler<HttpBinUserAgentModel> {151 override fun success(request: Request, response: Response, value: HttpBinUserAgentModel) {152 assertThat(value, notNullValue())153 }154 override fun failure(request: Request, response: Response, error: FuelError) {155 fail("Request shouldn't have failed")156 }157 })158 .get()159 }160 @Test161 fun jacksonTestResponseHandlerObjectWithCustomMapper() {162 mock.chain(163 request = mock.request().withPath("/user-agent"),164 response = mock.reflect()165 )166 Fuel.get(mock.path("user-agent"))167 .responseObject(createCustomMapper(), object : ResponseHandler<HttpBinUserAgentModel> {168 override fun success(request: Request, response: Response, value: HttpBinUserAgentModel) {169 assertThat(value, notNullValue())170 }171 override fun failure(request: Request, response: Response, error: FuelError) {172 fail("Request shouldn't have failed")173 }174 })175 .get()176 }177 @Test178 fun jacksonTestResponseHandlerObjectError() {179 mock.chain(180 request = mock.request().withPath("/user-agent"),181 response = mock.response().withStatusCode(HttpURLConnection.HTTP_NOT_FOUND)182 )183 Fuel.get(mock.path("user-agent"))184 .responseObject(createCustomMapper(), object : ResponseHandler<HttpBinUserAgentModel> {185 override fun success(request: Request, response: Response, value: HttpBinUserAgentModel) {186 fail("Request should have failed")187 }188 override fun failure(request: Request, response: Response, error: FuelError) {189 assertThat(error.exception, instanceOf(HttpException::class.java))190 }191 })192 .get()193 }194 @Test195 fun jacksonTestResponseSyncObject() {196 mock.chain(197 request = mock.request().withPath("/issues/1"),198 response = mock.response().withBody(199 "{ \"id\": 1, \"title\": \"issue 1\", \"number\": null }"200 ).withStatusCode(HttpURLConnection.HTTP_OK)201 )202 val (_, res, result) = Fuel.get(mock.path("issues/1")).responseObject<IssueInfo>()203 assertThat(res, notNullValue())204 assertThat(result.get(), notNullValue())205 assertThat(result.get(), isA(IssueInfo::class.java))206 assertThat(result, notNullValue())207 }208 @Test209 fun jacksonTestResponseSyncObjectWithCustomMapper() {210 mock.chain(211 request = mock.request().withPath("/issues/1"),212 response = mock.response().withBody(213 "{ \"id\": 1, \"title\": \"issue 1\", \"number\": null, \"snake_property\": 10 }"214 ).withStatusCode(HttpURLConnection.HTTP_OK)215 )216 val (_, res, result) = Fuel.get(mock.path("issues/1")).responseObject<IssueInfo>(createCustomMapper())217 assertThat(res, notNullValue())218 assertThat(result.get(), notNullValue())219 assertThat(result.get(), isA(IssueInfo::class.java))220 assertThat(result.get().snakeProperty, equalTo(10))221 assertThat(result, notNullValue())222 }223 @Test224 fun jacksonTestResponseSyncObjectError() {225 mock.chain(226 request = mock.request().withPath("/issues/1"),227 response = mock.response().withStatusCode(HttpURLConnection.HTTP_NOT_FOUND)228 )229 val (_, res, result) = Fuel.get(mock.path("issues/1")).responseObject<IssueInfo>()230 assertThat(res, notNullValue())231 assertThat(result, notNullValue())232 val (value, error) = result233 assertThat(value, nullValue())234 assertThat(error, notNullValue())235 assertThat((error as FuelError).response.statusCode, equalTo(HttpURLConnection.HTTP_NOT_FOUND))236 }237 data class IssueInfo(val id: Int, val title: String, val number: Int, val snakeProperty: Int)238 @Test239 fun testProcessingGenericList() {240 mock.chain(241 request = mock.request().withPath("/issues"),242 response = mock.response()243 .withBody(244 """245 [246 {247 "id": 1,248 "title": "issue 1",249 "number": null250 },251 {252 "id": 2,253 "title": "issue 2",254 "number": 32255 }256 ]257 """258 )259 .withStatusCode(HttpURLConnection.HTTP_OK)260 )261 Fuel.get(mock.path("issues"))262 .responseObject<List<IssueInfo>> { _, _, result ->263 val issues = result.get()264 assertNotEquals(issues.size, 0)265 assertThat(issues[0], isA(IssueInfo::class.java))266 }267 .get()268 }269 @Test270 fun manualDeserializationShouldWork() {271 mock.chain(272 request = mock.request().withPath("/issues"),273 response = mock.response()274 .withBody(275 """276 [277 {278 "id": 1,279 "title": "issue 1",280 "number": null281 },282 {283 "id": 2,284 "title": "issue 2",285 "number": 32286 }287 ]288 """289 )290 .withStatusCode(HttpURLConnection.HTTP_OK),291 times = Times.exactly(5)292 )293 Fuel.get(mock.path("issues"))294 .response { _: Request, response: Response, _ ->295 val issueList = jacksonDeserializerOf<List<IssueInfo>>().deserialize(response)296 assertThat(issueList[0], isA(IssueInfo::class.java))297 }298 .get()299 Fuel.get(mock.path("issues"))300 .response { _: Request, response: Response, _ ->301 val issueList = jacksonDeserializerOf<List<IssueInfo>>().deserialize(response.body().toStream())!!302 assertThat(issueList[0], isA(IssueInfo::class.java))303 }304 .get()305 Fuel.get(mock.path("issues"))306 .response { _: Request, response: Response, _ ->307 val issueList = jacksonDeserializerOf<List<IssueInfo>>().deserialize(response.body().toStream().reader())!!308 assertThat(issueList[0], isA(IssueInfo::class.java))309 }310 .get()311 Fuel.get(mock.path("issues"))312 .response { _: Request, _, result: Result<ByteArray, FuelError> ->313 val issueList = jacksonDeserializerOf<List<IssueInfo>>().deserialize(result.get())!!314 assertThat(issueList[0], isA(IssueInfo::class.java))315 }316 .get()317 Fuel.get(mock.path("issues"))318 .responseString { _: Request, _: Response, result: Result<String, FuelError> ->319 val issueList = jacksonDeserializerOf<List<IssueInfo>>().deserialize(result.get())!!320 assertThat(issueList[0], isA(IssueInfo::class.java))321 }322 .get()323 }324 private fun createCustomMapper(): ObjectMapper {325 val mapper = ObjectMapper().registerKotlinModule()326 .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)327 mapper.propertyNamingStrategy = PropertyNamingStrategy.SNAKE_CASE328 return mapper329 }330}...

Full Screen

Full Screen

createCustomMapper

Using AI Code Generation

copy

Full Screen

1val mapper = createCustomMapper()2.responseObject(mapper)3val (data, error) = result4println(data)5println(error)6}7}

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