|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2021. |
| 3 | + * |
| 4 | + * This file is part of Imposter. |
| 5 | + * |
| 6 | + * "Commons Clause" License Condition v1.0 |
| 7 | + * |
| 8 | + * The Software is provided to you by the Licensor under the License, as |
| 9 | + * defined below, subject to the following condition. |
| 10 | + * |
| 11 | + * Without limiting other conditions in the License, the grant of rights |
| 12 | + * under the License will not include, and the License does not grant to |
| 13 | + * you, the right to Sell the Software. |
| 14 | + * |
| 15 | + * For purposes of the foregoing, "Sell" means practicing any or all of |
| 16 | + * the rights granted to you under the License to provide to third parties, |
| 17 | + * for a fee or other consideration (including without limitation fees for |
| 18 | + * hosting or consulting/support services related to the Software), a |
| 19 | + * product or service whose value derives, entirely or substantially, from |
| 20 | + * the functionality of the Software. Any license notice or attribution |
| 21 | + * required by the License must also include this Commons Clause License |
| 22 | + * Condition notice. |
| 23 | + * |
| 24 | + * Software: Imposter |
| 25 | + * |
| 26 | + * License: GNU Lesser General Public License version 3 |
| 27 | + * |
| 28 | + * Licensor: Peter Cornish |
| 29 | + * |
| 30 | + * Imposter is free software: you can redistribute it and/or modify |
| 31 | + * it under the terms of the GNU Lesser General Public License as published by |
| 32 | + * the Free Software Foundation, either version 3 of the License, or |
| 33 | + * (at your option) any later version. |
| 34 | + * |
| 35 | + * Imposter is distributed in the hope that it will be useful, |
| 36 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | + * GNU Lesser General Public License for more details. |
| 39 | + * |
| 40 | + * You should have received a copy of the GNU Lesser General Public License |
| 41 | + * along with Imposter. If not, see <https://www.gnu.org/licenses/>. |
| 42 | + */ |
| 43 | +package io.gatehill.imposter.server |
| 44 | + |
| 45 | +import io.gatehill.imposter.ImposterConfig |
| 46 | +import io.gatehill.imposter.plugin.PluginManager |
| 47 | +import io.gatehill.imposter.plugin.test.TestPluginImpl |
| 48 | +import io.gatehill.imposter.util.CryptoUtil.DEFAULT_KEYSTORE_PASSWORD |
| 49 | +import io.gatehill.imposter.util.CryptoUtil.DEFAULT_KEYSTORE_PATH |
| 50 | +import io.gatehill.imposter.util.CryptoUtil.getDefaultKeystore |
| 51 | +import io.gatehill.imposter.util.FileUtil.CLASSPATH_PREFIX |
| 52 | +import io.gatehill.imposter.util.HttpUtil |
| 53 | +import io.gatehill.imposter.util.InjectorUtil |
| 54 | +import io.restassured.RestAssured |
| 55 | +import io.vertx.core.Vertx |
| 56 | +import io.vertx.junit5.VertxTestContext |
| 57 | +import org.hamcrest.Matchers |
| 58 | +import org.junit.jupiter.api.Assertions |
| 59 | +import org.junit.jupiter.api.BeforeEach |
| 60 | +import org.junit.jupiter.api.Test |
| 61 | + |
| 62 | +/** |
| 63 | + * Tests HTTPS support. |
| 64 | + */ |
| 65 | +class HttpsTest : BaseVerticleTest() { |
| 66 | + override val pluginClass = TestPluginImpl::class.java |
| 67 | + |
| 68 | + @BeforeEach |
| 69 | + @Throws(Exception::class) |
| 70 | + override fun setUp(vertx: Vertx, testContext: VertxTestContext) { |
| 71 | + super.setUp(vertx, testContext) |
| 72 | + |
| 73 | + // set up trust store for TLS |
| 74 | + RestAssured.trustStore(getDefaultKeystore(HttpsTest::class.java).toFile(), DEFAULT_KEYSTORE_PASSWORD) |
| 75 | + RestAssured.baseURI = "https://$host:$listenPort" |
| 76 | + } |
| 77 | + |
| 78 | + override val testConfigDirs = listOf( |
| 79 | + "/simple-config" |
| 80 | + ) |
| 81 | + |
| 82 | + @Throws(Exception::class) |
| 83 | + override fun configure(imposterConfig: ImposterConfig) { |
| 84 | + super.configure(imposterConfig) |
| 85 | + |
| 86 | + // enable TLS |
| 87 | + imposterConfig.isTlsEnabled = true |
| 88 | + imposterConfig.keystorePath = CLASSPATH_PREFIX + DEFAULT_KEYSTORE_PATH |
| 89 | + imposterConfig.keystorePassword = DEFAULT_KEYSTORE_PASSWORD |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + fun testPluginLoadAndConfig() { |
| 94 | + val pluginManager = InjectorUtil.getInstance<PluginManager>() |
| 95 | + val plugin = pluginManager.getPlugin<TestPluginImpl>(TestPluginImpl::class.java.canonicalName) |
| 96 | + Assertions.assertNotNull(plugin) |
| 97 | + Assertions.assertNotNull(plugin!!.configs) |
| 98 | + Assertions.assertEquals(1, plugin.configs.size) |
| 99 | + |
| 100 | + val pluginConfig = plugin.configs[0] |
| 101 | + Assertions.assertEquals("/example", pluginConfig.path) |
| 102 | + Assertions.assertEquals("test-plugin-data.json", pluginConfig.responseConfig.file) |
| 103 | + Assertions.assertEquals("testValue", pluginConfig.customProperty) |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + fun testRequestSuccess() { |
| 108 | + RestAssured.given().`when`() |
| 109 | + .get("/example") |
| 110 | + .then() |
| 111 | + .statusCode(Matchers.equalTo(HttpUtil.HTTP_OK)) |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + fun testRequestNotFound() { |
| 116 | + RestAssured.given().`when`() |
| 117 | + .get("/does_not_match") |
| 118 | + .then() |
| 119 | + .statusCode(Matchers.equalTo(HttpUtil.HTTP_NOT_FOUND)) |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + fun testResponseFileNotFound() { |
| 124 | + RestAssured.given().`when`() |
| 125 | + .get("/static-file-example") |
| 126 | + .then() |
| 127 | + .statusCode(Matchers.equalTo(HttpUtil.HTTP_NOT_FOUND)) |
| 128 | + } |
| 129 | +} |
0 commit comments