<commit_before>/*
 * MUCtool Web Toolkit
 *
 * Copyright 2019 <NAME> <<EMAIL>>. All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.loxal.muctool

import io.ktor.application.Application
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.server.testing.handleRequest
import io.ktor.server.testing.withTestApplication
import org.junit.Test
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.util.*
import kotlin.test.*

const val ipAddressWithInfo = "172.16.17.32"

class AppTest {
    @Test
    fun curl() = withTestApplication(Application::module) {
        with(handleRequest(HttpMethod.Get, "curl?url=https://en.wikipedia.org/wiki/Main_Page")) {
            val contentFragment = "Main page"
            val status = HttpStatusCode.OK
            assertEquals(status, response.status())
            assert(response.content!!.isNotEmpty())
            assert(response.content?.contains(contentFragment)!!)

            val curl = mapper.readValue(response.byteContent, Curl::class.java)
            assertEquals(status.value, curl.statusCode)
            assert(curl.body!!.contains(contentFragment))
        }

        with(handleRequest(HttpMethod.Get, "curl?url=https://api.muctool.invalid")) {
            assertEquals(HttpStatusCode.NotFound, response.status())
            assertNull(response.content)
        }

        with(handleRequest(HttpMethod.Get, "curl?url=https://en.wikipedia.org/missing-resource")) {
            assertEquals(HttpStatusCode.OK, response.status())
            assertNotNull(response.content)

            val curl = mapper.readValue(response.byteContent, Curl::class.java)
            assertEquals(HttpStatusCode.NotFound.value, curl.statusCode)
            assertFalse(curl.body!!.isEmpty())
        }

        with(handleRequest(HttpMethod.Get, "curl?url=http://api.muctool.de")) {
            assertEquals(HttpStatusCode.OK, response.status(), response.status().toString())
            assertNotNull(response.content)

            val curl = mapper.readValue(response.byteContent, Curl::class.java)
            assertEquals(HttpStatusCode.PermanentRedirect.value, curl.statusCode)
            assert(curl.body!!.contains("<title>301 Moved Permanently</title>"))
        }
    }

    @Test
    fun stats() = withTestApplication(Application::module) {
        with(handleRequest(HttpMethod.Get, "stats")) {
            assertEquals(HttpStatusCode.OK, response.status())
            assertTrue(response.content!!.isNotEmpty())
            if (System.getenv("BUILD_NUMBER") === null) {
                assertEquals(-552594242, response.content?.hashCode())
            }
        }
    }

    @Test
    fun encoding() = withTestApplication(Application::module) {
        with(handleRequest(HttpMethod.Get, "encoding?value=https://example.com")) {
            assertEquals(HttpStatusCode.OK, response.status())
            LOG.info("response.content: ${response.content}")
            assertEquals(408897637, response.content?.hashCode())
        }

        with(handleRequest(HttpMethod.Get, "encoding?value=https%3A%2F%2Fexample.com")) {
            assertEquals(HttpStatusCode.OK, response.status())
            LOG.info("response.content: ${response.content}")
            assertEquals(408897637, response.content?.hashCode())
        }

        with(handleRequest(HttpMethod.Get, "encoding?value=aHR0cHM6Ly9leGFtcGxlLmNvbQ==")) {
            assertEquals(HttpStatusCode.OK, response.status())
            LOG.info("response.content: ${response.content}")<commit_msg>update test<commit_after>/*
 * MUCtool Web Toolkit
 *
 * Copyright 2019 <NAME> <<EMAIL>>. All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.loxal.muctool

import io.ktor.application.Application
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.server.testing.handleRequest
import io.ktor.server.testing.withTestApplication
import org.junit.Test
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.util.*
import kotlin.test.*

const val ipAddressWithInfo = "172.16.17.32"

class AppTest {
    @Test
    fun curl() = withTestApplication(Application::module) {
        with(handleRequest(HttpMethod.Get, "curl?url=https://en.wikipedia.org/wiki/Main_Page")) {
            val contentFragment = "Main page"
            val status = HttpStatusCode.OK
            assertEquals(status, response.status())
            assert(response.content!!.isNotEmpty())
            assert(response.content?.contains(contentFragment)!!)

            val curl = mapper.readValue(response.byteContent, Curl::class.java)
            assertEquals(status.value, curl.statusCode)
            assert(curl.body!!.contains(contentFragment))
        }

        with(handleRequest(HttpMethod.Get, "curl?url=https://api.muctool.invalid")) {
            assertEquals(HttpStatusCode.NotFound, response.status())
            assertNull(response.content)
        }

        with(handleRequest(HttpMethod.Get, "curl?url=https://en.wikipedia.org/missing-resource")) {
            assertEquals(HttpStatusCode.OK, response.status())
            assertNotNull(response.content)

            val curl = mapper.readValue(response.byteContent, Curl::class.java)
            assertEquals(HttpStatusCode.NotFound.value, curl.statusCode)
            assertFalse(curl.body!!.isEmpty())
        }

        with(handleRequest(HttpMethod.Get, "curl?url=http://api.muctool.de")) {
            assertEquals(HttpStatusCode.OK, response.status(), response.status().toString())
            assertNotNull(response.content)

            val curl = mapper.readValue(response.byteContent, Curl::class.java)
            assertEquals(HttpStatusCode.PermanentRedirect.value, curl.statusCode)
            assert(curl.body!!.contains("<title>308 Permanent Redirect</title>"))
        }
    }

    @Test
    fun stats() = withTestApplication(Application::module) {
        with(handleRequest(HttpMethod.Get, "stats")) {
            assertEquals(HttpStatusCode.OK, response.status())
            assertTrue(response.content!!.isNotEmpty())
            if (System.getenv("BUILD_NUMBER") === null) {
                assertEquals(-552594242, response.content?.hashCode())
            }
        }
    }

    @Test
    fun encoding() = withTestApplication(Application::module) {
        with(handleRequest(HttpMethod.Get, "encoding?value=https://example.com")) {
            assertEquals(HttpStatusCode.OK, response.status())
            LOG.info("response.content: ${response.content}")
            assertEquals(408897637, response.content?.hashCode())
        }

        with(handleRequest(HttpMethod.Get, "encoding?value=https%3A%2F%2Fexample.com")) {
            assertEquals(HttpStatusCode.OK, response.status())
            LOG.info("response.content: ${response.content}")
            assertEquals(408897637, response.content?.hashCode())
        }

        with(handleRequest(HttpMethod.Get, "encoding?value=aHR0cHM6Ly9leGFtcGxlLmNvbQ==")) {
            assertEquals(HttpStatusCode.OK, response.status())
            LOG.info("response.content: ${response.content}")
{'fraction_non_alphanumeric': 0.11749316208823879, 'fraction_numerical': 0.012367701272446189, 'mean_word_length': 3.122549019607843, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 8, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, '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': '5521824', 'n_tokens_mistral': 2322, 'n_tokens_neox': 2262, 'n_words': 474}