libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

ParamsTest.kt (2466B)


      1 /*
      2  * This file is part of LibEuFin.
      3  * Copyright (C) 2024 Taler Systems S.A.
      4 
      5  * LibEuFin is free software; you can redistribute it and/or modify
      6  * it under the terms of the GNU Affero General Public License as
      7  * published by the Free Software Foundation; either version 3, or
      8  * (at your option) any later version.
      9 
     10  * LibEuFin is distributed in the hope that it will be useful, but
     11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
     13  * Public License for more details.
     14 
     15  * You should have received a copy of the GNU Affero General Public
     16  * License along with LibEuFin; see the file COPYING.  If not, see
     17  * <http://www.gnu.org/licenses/>
     18  */
     19 
     20 import io.ktor.http.*
     21 import org.junit.Test
     22 import tech.libeufin.common.*
     23 import kotlin.test.*
     24 
     25 class ParamsTest {
     26     @Test
     27     fun parse() {
     28         fun String.check(timeout_ms: Long, limit: Int, offset: Long) {
     29             val parameters = this.parseUrlEncodedParameters()
     30             val parsed = HistoryParams.extract(parameters)
     31             assertEquals(HistoryParams(PageParams(limit, offset), PollingParams(timeout_ms)), parsed)
     32         }
     33         fun String.fail(msg: String) {
     34             val parameters = this.parseUrlEncodedParameters()
     35             val e = assertFailsWith<ApiException> {
     36                 HistoryParams.extract(parameters)
     37             }
     38             assertEquals(HttpStatusCode.BadRequest, e.httpStatus)
     39             assertEquals(msg, e.message)
     40         }
     41         sequenceOf(
     42             "long_poll_ms=1&delta=2&offset=3",
     43             "timeout_ms=1&limit=2&start=3",
     44             "long_poll_ms=1&delta=2&offset=3&timeout_ms=1&limit=2&start=3"
     45         ).forEach { case -> case.check(1, 2, 3) }
     46         "".check(0, -20, Long.MAX_VALUE)
     47         "limit=1".check(0, 1, 0)
     48         "limit=${MAX_PAGE_SIZE}".check(0, MAX_PAGE_SIZE, 0)
     49         "limit=0".fail("Param 'limit' must be non-zero")
     50         "limit=${MAX_PAGE_SIZE+1}".fail("Param 'limit' must be <= ${MAX_PAGE_SIZE}")
     51         "offset=-1".fail("Param 'offset' must be a positive number")
     52         "long_poll_ms=${MAX_TIMEOUT_MS+1}&limit=10".check(MAX_TIMEOUT_MS, 10, 0)
     53         "long_poll_ms=1&timeout_ms=2".fail("Param 'timeout_ms' cannot be used with param 'long_poll_ms'")
     54         "limit=1&delta=2".fail("Param 'limit' cannot be used with param 'delta'")
     55         "offset=1&start=2".fail("Param 'offset' cannot be used with param 'start'")
     56     }
     57 }