From 6739f115bb4e3f696ae504370481415ab28f7bbb Mon Sep 17 00:00:00 2001 From: jaguililla Date: Mon, 13 Nov 2023 21:03:32 +0100 Subject: [PATCH] Improve mutable model --- .../http/mutable/model/HttpRequestTest.kt | 92 ++++++++++--------- .../http/mutable/model/HttpResponseTest.kt | 44 ++++----- 2 files changed, 69 insertions(+), 67 deletions(-) diff --git a/http/http_mutable/src/test/kotlin/com/hexagonkt/http/mutable/model/HttpRequestTest.kt b/http/http_mutable/src/test/kotlin/com/hexagonkt/http/mutable/model/HttpRequestTest.kt index b7e88a28e3..34a80ce484 100644 --- a/http/http_mutable/src/test/kotlin/com/hexagonkt/http/mutable/model/HttpRequestTest.kt +++ b/http/http_mutable/src/test/kotlin/com/hexagonkt/http/mutable/model/HttpRequestTest.kt @@ -189,52 +189,54 @@ internal class HttpRequestTest { } @Test fun `HTTP Request operators work ok`() { - httpRequestData().let { - val o = it.copy() - val header = Header("h", "v") - it + header - assertEquals(it, o.copy(headers = it.headers + header)) + val header = Header("h", "v") + httpRequestCopy().let { (o, r) -> + r + header + assertEquals(r, o.copy(headers = o.headers + header)) + } + httpRequestCopy().let { (o, r) -> + r + Headers(header) + assertEquals(r, o.copy(headers = o.headers + header)) + } + + val queryParameter = QueryParameter("h", "v") + httpRequestCopy().let { (o, r) -> + r + queryParameter + assertEquals(r, o.copy(queryParameters = o.queryParameters + queryParameter)) + } + httpRequestCopy().let { (o, r) -> + r + QueryParameters(queryParameter) + assertEquals(r, o.copy(queryParameters = o.queryParameters + queryParameter)) + } + + val httpPart = HttpPart("h", "v") + httpRequestCopy().let { (o, r) -> + r + httpPart + assertEquals(r, o.copy(parts = o.parts + httpPart)) + } + httpRequestCopy().let { (o, r) -> + r + listOf(httpPart) + assertEquals(r, o.copy(parts = o.parts + httpPart)) + } + + val formParameter = FormParameter("h", "v") + httpRequestCopy().let { (o, r) -> + r + formParameter + assertEquals(r, o.copy(formParameters = o.formParameters + formParameter)) + } + httpRequestCopy().let { (o, r) -> + r + FormParameters(formParameter) + assertEquals(r, o.copy(formParameters = o.formParameters + formParameter)) + } + + val cookie = Cookie("n", "v") + httpRequestCopy().let { (o, r) -> + r + cookie + assertEquals(r, o.copy(cookies = o.cookies + cookie)) } -// assertEquals( -// httpRequest + Headers(header), -// httpRequest.copy(headers = httpRequest.headers + header) -// ) -// -// val queryParameter = QueryParameter("h", "v") -// assertEquals( -// httpRequest + queryParameter, -// httpRequest.copy(queryParameters = httpRequest.queryParameters + queryParameter) -// ) -// assertEquals( -// httpRequest + QueryParameters(queryParameter), -// httpRequest.copy(queryParameters = httpRequest.queryParameters + queryParameter) -// ) -// -// val httpPart = HttpPart("h", "v") -// assertEquals( -// httpRequest + httpPart, -// httpRequest.copy(parts = httpRequest.parts + httpPart) -// ) -// assertEquals( -// httpRequest + listOf(httpPart), -// httpRequest.copy(parts = httpRequest.parts + httpPart) -// ) -// -// val formParameter = FormParameter("h", "v") -// assertEquals( -// httpRequest + formParameter, -// httpRequest.copy(formParameters = httpRequest.formParameters + formParameter) -// ) -// assertEquals( -// httpRequest + FormParameters(formParameter), -// httpRequest.copy(formParameters = httpRequest.formParameters + formParameter) -// ) -// -// val cookie = Cookie("n", "v") -// assertEquals( -// httpRequest + cookie, -// httpRequest.copy(cookies = httpRequest.cookies + cookie) -// ) } + + private fun httpRequestCopy(): Pair = + httpRequestData().let { it.copy() to it } } diff --git a/http/http_mutable/src/test/kotlin/com/hexagonkt/http/mutable/model/HttpResponseTest.kt b/http/http_mutable/src/test/kotlin/com/hexagonkt/http/mutable/model/HttpResponseTest.kt index 92cbfdebdf..67167b8579 100644 --- a/http/http_mutable/src/test/kotlin/com/hexagonkt/http/mutable/model/HttpResponseTest.kt +++ b/http/http_mutable/src/test/kotlin/com/hexagonkt/http/mutable/model/HttpResponseTest.kt @@ -1,11 +1,9 @@ package com.hexagonkt.http.mutable.model import com.hexagonkt.core.media.TEXT_HTML -import com.hexagonkt.core.media.TEXT_RICHTEXT import org.junit.jupiter.api.Test import kotlin.test.assertEquals import kotlin.test.assertFalse -import kotlin.test.assertNotEquals internal class HttpResponseTest { @@ -33,26 +31,28 @@ internal class HttpResponseTest { } @Test fun `HTTP Response operators work ok`() { - val httpResponse = httpResponseData() -// val header = Header("h", "v") -// assertEquals( -// httpResponse + header, -// httpResponse.copy(headers = httpResponse.headers + header) -// ) -// assertEquals( -// httpResponse + Headers(header), -// httpResponse.copy(headers = httpResponse.headers + header) -// ) -// -// val cookie = Cookie("n", "v") -// assertEquals( -// httpResponse + cookie, -// httpResponse.copy(cookies = httpResponse.cookies + cookie) -// ) -// assertEquals( -// httpResponse + listOf(cookie), -// httpResponse.copy(cookies = httpResponse.cookies + cookie) -// ) + val header = Header("h", "v") + httpResponseCopy().let { (o, r) -> + r + header + assertEquals(r, o.copy(headers = o.headers + header)) + } + httpResponseCopy().let { (o, r) -> + r + Headers(header) + assertEquals(r, o.copy(headers = o.headers + header)) + } + + val cookie = Cookie("n", "v") + httpResponseCopy().let { (o, r) -> + r + cookie + assertEquals(r, o.copy(cookies = o.cookies + cookie)) + } + httpResponseCopy().let { (o, r) -> + r + listOf(cookie) + assertEquals(r, o.copy(cookies = o.cookies + cookie)) + } } + + private fun httpResponseCopy(): Pair = + httpResponseData().let { it.copy() to it } }