<commit_before>package org.http4k.lens

import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import com.natpryce.hamkrest.throws
import org.http4k.core.Body
import org.http4k.core.ContentType
import org.http4k.core.ContentType.Companion.TEXT_PLAIN
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.with
import org.http4k.lens.Header.CONTENT_TYPE
import org.junit.jupiter.api.Test

class BodyTest {

    private val emptyRequest = Request(Method.GET, "")

    @Test
    fun `can get string body when lax`() {
        val laxContentType = Body.string(TEXT_PLAIN).toLens()
        assertThat(laxContentType(emptyRequest.body("some value")), equalTo("some value"))
        assertThat(laxContentType(emptyRequest.header("Content-type", TEXT_PLAIN.toHeaderValue()).body("some value")), equalTo("some value"))
    }

    @Test
    fun `can get regex body`() {
        val regexBody = Body.regex("bob(.+)alice").toLens()
        assertThat(regexBody(emptyRequest.body("bobritaalice")), equalTo("rita"))
        assertThat({ regexBody(emptyRequest.body("foobaralice")) }, throws(lensFailureWith<Request>(Invalid(Meta(true, "body", ParamMeta.StringParam, "body")), overallType = Failure.Type.Invalid)))
    }

    @Test
    fun `non empty string`() {
        val nonEmpty = Body.nonEmptyString(TEXT_PLAIN).toLens()
        assertThat(nonEmpty(emptyRequest.body("123")), equalTo("123"))
        assertThat({ nonEmpty(emptyRequest.body("")) }, throws(lensFailureWith<Request>(Invalid(Meta(true, "body", ParamMeta.StringParam, "body")), overallType = Failure.Type.Invalid)))
    }

    @Test
    fun `rejects invalid or missing content type when ContentNegotiation Strict`() {
        val strictBody = Body.string(TEXT_PLAIN, contentNegotiation = ContentNegotiation.Strict).toLens()
        assertThat({ strictBody(emptyRequest.body("some value")) }, throws(lensFailureWith<Any?>(Unsupported(CONTENT_TYPE.meta), overallType = Failure.Type.Unsupported)))
        assertThat({ strictBody(emptyRequest.header("content-type", "text/bob;charset=not-utf-8").body("some value")) }, throws(lensFailureWith<ContentType>(Unsupported(CONTENT_TYPE.meta), overallType = Failure.Type.Unsupported)))
        assertThat(strictBody(emptyRequest.header("content-type", "text/plain;  charset  = utf-8  ").body("some value")), equalTo("some value"))
    }

    @Test
    fun `rejects invalid or missing content type when ContentNegotiation StrictNoDirective`() {<commit_msg>remove duff test<commit_after>package org.http4k.lens

import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import com.natpryce.hamkrest.throws
import org.http4k.core.Body
import org.http4k.core.ContentType
import org.http4k.core.ContentType.Companion.TEXT_PLAIN
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.with
import org.http4k.lens.Header.CONTENT_TYPE
import org.junit.jupiter.api.Test

class BodyTest {

    private val emptyRequest = Request(Method.GET, "")

    @Test
    fun `can get string body when lax`() {
        val laxContentType = Body.string(TEXT_PLAIN).toLens()
        assertThat(laxContentType(emptyRequest.body("some value")), equalTo("some value"))
        assertThat(laxContentType(emptyRequest.header("Content-type", TEXT_PLAIN.toHeaderValue()).body("some value")), equalTo("some value"))
    }

    @Test
    fun `can get regex body`() {
        val regexBody = Body.regex("bob(.+)alice").toLens()
        assertThat(regexBody(emptyRequest.body("bobritaalice")), equalTo("rita"))
        assertThat({ regexBody(emptyRequest.body("foobaralice")) }, throws(lensFailureWith<Request>(Invalid(Meta(true, "body", ParamMeta.StringParam, "body")), overallType = Failure.Type.Invalid)))
    }

    @Test
    fun `non empty string`() {
        val nonEmpty = Body.nonEmptyString(TEXT_PLAIN).toLens()
        assertThat(nonEmpty(emptyRequest.body("123")), equalTo("123"))
        assertThat({ nonEmpty(emptyRequest.body("")) }, throws(lensFailureWith<Request>(Invalid(Meta(true, "body", ParamMeta.StringParam, "body")), overallType = Failure.Type.Invalid)))
    }

    @Test
    fun `rejects invalid or missing content type when ContentNegotiation Strict`() {
        val strictBody = Body.string(TEXT_PLAIN, contentNegotiation = ContentNegotiation.Strict).toLens()
        assertThat({ strictBody(emptyRequest.body("some value")) }, throws(lensFailureWith<Any?>(Unsupported(CONTENT_TYPE.meta), overallType = Failure.Type.Unsupported)))
        assertThat({ strictBody(emptyRequest.header("content-type", "text/bob;charset=not-utf-8").body("some value")) }, throws(lensFailureWith<ContentType>(Unsupported(CONTENT_TYPE.meta), overallType = Failure.Type.Unsupported)))
    }

    @Test
    fun `rejects invalid or missing content type when ContentNegotiation StrictNoDirective`() {
{'fraction_non_alphanumeric': 0.13720262510254308, 'fraction_numerical': 0.006357670221493027, 'mean_word_length': 4.97671568627451, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4936051', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1500, 'n_words': 264}